利用ansible modules模块来自定义集群管理
前沿: 在一些个特定环境下,用ansible做集群管理还是很棒的,这两天看了他的模块,官方提供了很多,就算不够,你也可以自定义定制。 话说我挺喜欢他的modules模块的,够直接 !!!我这里就说些常见的ansible的modules吧。
下面的ansible service一看大家就懂了,就是服务状态的管理模块
1
2
3
4
5
6
7
$ ansible web -m service -a "name=nginx state=started"
10.150.145.53 | success >> {
"changed": false,
"name": "nginx",
"state": "started"
}
$
紧接着我们想知道他是否真的启动了,调用command模块,用来执行系统的命令。 lsof -i :80 返回值告诉我们,nginx已经ok了。ansible web -m command -a "你要推送的命令"
1
2
3
4
5
6
7
8
9
$ ansible web -a "lsof -i :80"
10.150.145.53 | success | rc=0 >>
COMMAND PIDUSER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 17996root 6uIPv4 130868107 0t0TCP *:http (LISTEN)
nginx 18030 nginx 6uIPv4 130868107 0t0TCP *:http (LISTEN)
nginx 18031 nginx 6uIPv4 130868107 0t0TCP *:http (LISTEN)
nginx 18032 nginx 6uIPv4 130868107 0t0TCP *:http (LISTEN)
nginx 18033 nginx 6uIPv4 130868107 0t0TCP *:http (LISTEN)
$
但是貌似command模块不识别管道,这个有点郁闷。
1
2
3
4
Run cmd
command:
*variables like $HOME and operations like "<", ">", "|", and "&" will not work in command.
ansible lamp -m command -a 'iptables -nL'
不信可以试试!原文:http://rfyiamcool.blog./1030776/1414417
1
2
3
4
5
6
7
8
9
10
11
12
13
$ ansible web -m command -a "ps aux|grep nginx"
10.150.145.53 | FAILED | rc=1 >>
ERROR: Unsupported option (BSD syntax)
********* simple selection ****************** selection by list *********
-A all processes -C by command name
-N negate selection -G by real group ID (supports names)
-a all w/ tty except session leaders-U by real user ID (supports names)
-d all except session leaders -g by session OR by effective group name
-e all processes -p by process ID
Tall processes on this terminal -s processes in the sessions given
aall w/ tty, including other users-t by tty
gOBSOLETE -- DO NOT USE -u by effective user ID (supports names)
ronly running processes Uprocesses for specified users
除了自己写模块外,还可以调用shell、raw模块来解决这个问题。
1
2
3
4
5
6
7
8
9
$ ansible web -m shell -a "ps aux|grep nginx"
10.150.145.53 | success | rc=0 >>
root 145260.00.0 1060881144 ? S 10:04 0:00 /bin/sh -c ps aux|grep nginx
root 145280.00.0 103240 844 ? S 10:04 0:00 grep nginx
root 179960.00.2994244752 ? Ss May14 0:00 nginx: master process nginx
nginx 180300.50.3 1034367228 ? S May1414:41 nginx: worker process
nginx 180310.50.3 1039167284 ? S May1414:50 nginx: worker process
nginx 180320.50.3 1034367240 ? S May1414:49 nginx: worker process
nginx 180330.50.3 1041407328 ? S May1414:54 nginx: worker process
关于ping模块,其实这个没啥讲解的。。。
1
2
3
4
5
$ ansible web -m ping
10.150.145.53 | success >> {
"changed": false,
"ping": "pong"
}
关于业务的redis的模块
1
2
3
4
5
$ ansible web -m redis -a "command=flush flush_mode=all"
10.150.145.53 | success{
"changed": true,
"flushed": true
}
官方给的一些例子还是很全面的。
1
2
3
4
5
6
7
8
9
10
11
12
# Set local redis instance to be slave of melee.island on port 6377
- redis: command=slave master_host=melee.island master_port=6377
# Deactivate slave mode
- redis: command=slave slave_mode=master
# Flush all the redis db
- redis: command=flush flush_mode=all
# Flush only one db in a redis instance
- redis: command=flush db=1 flush_mode=db
# Configure local redis to have 10000 max clients
- redis: command=config name=maxclients value=10000
# Configure local redis to have lua time limit of 100 ms
- redis: command=config name=lua-time-limit value=100
再来搞噶ansible cron计划任务模块的使用 !
在l.yaml里面加入
1
2
3
4
tasks:
- cron: name="check dirs" hour="5,2" job="ls -alh > /dev/null"
- name: create {{ user }} on web
user: name="{{ user }}"
咱们同步一下playbook
1
2
3
4
5
6
7
8
$ ansible-playbook l.yaml
PLAY ************************************************************
TASK: ***********
changed:
TASK: *************************************************
ok:
PLAY RECAP ********************************************************************
10.150.145.53 : ok=2 changed=1 unreachable=0 failed=0
还有下载文件的模块:
1
2
- name: download foo.conf
get_url: url=http://xiaorui.cc/path/file.conf dest=/etc/foo.conf mode=0440
如果咱们想看下 一个模块的使用方法,需要调用ansible-doc的命令。
1
2
3
4
5
6
7
$ ansible-doc ping
> PING
A trivial test module, this module always returns `pong' on
successful contact. It does not make sense in playbooks, but it is
useful from `/usr/bin/ansible'
Example:
ansible webservers -m ping
在这里有大量的模块,大家可以好好看看,当然ansible也是可以自己开发模块的。 他的写法和saltstack有些相像。。。。
这是ansible官方的modules模块index地址: (里面有很多很多的模块)http://docs.ansible.com/modules.html
一会会做相关的补充
1
2
3
4
5
6
#添加一个用户,shell环境指定到/bin/bash
- user: name=james shell=/bin/bash groups=admins,developers append=yes
# 删除一个用户
- user: name=johnd state=absent remove=yes
# 创建ssh key
- user: name=jsmith generate_ssh_key=yes ssh_key_bits=2048
1
2
# 开启路由转发的功能
- sysctl: name="net.ipv4.ip_forward" value=1 sysctl_set=yes
页:
[1]