linre 发表于 2014-11-20 11:18:19

开发Ansible modules模块来自定义集群管理

前沿:

   在一些个特定环境下,用ansible做集群管理还是很棒的,这两天看了他的模块,官方提供了很多,就算不够,你也可以自定义定制。 话说我挺喜欢他的modules模块的,够直接 !!!

我这里就说些常见的ansible的modules吧。

下面的ansible service一看大家就懂了,就是服务状态的管理模块

Python
$ ansible web -m service -a "name=nginx state=started" 10.150.145.53 | success >> { "changed": false, "name": "nginx", "state": "started" } $

   

$ 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 “你要推送的命令”

Python
$ ansible web -a "lsof -i :80" 10.150.145.53 | success | rc=0 。。 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 17996 root 6u IPv4 130868107 0t0 TCP *:http (LISTEN) nginx 18030 nginx 6u IPv4 130868107 0t0 TCP *:http (LISTEN) nginx 18031 nginx 6u IPv4 130868107 0t0 TCP *:http (LISTEN) nginx 18032 nginx 6u IPv4 130868107 0t0 TCP *:http (LISTEN) nginx 18033 nginx 6u IPv4 130868107 0t0 TCP *:http (LISTEN) $



$ 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模块不识别管道,这个有点郁闷。

Python
Run cmd command: * variables like $HOME and operations like "<", ">", "|", and "&" will not work in command. ansible lamp -m command -a 'iptables -nL'

   

Run cmd
command:
*variables like $HOME and operations like "<", ">", "|", and "&" will not work in command.
ansible lamp -m command -a 'iptables -nL'


Python
$ 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 T all processes on this terminal -s processes in the sessions given a all w/ tty, including other users -t by tty g OBSOLETE -- DO NOT USE -u by effective user ID (supports names) r only running processes U processes for specified users

   

$ 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模块来解决这个问题

Python
$ ansible web -m shell -a "ps aux|grep nginx" 10.150.145.53 | success | rc=0 .. root 14526 0.0 0.0 106088 1144 ? S 10:04 0:00 /bin/sh -c ps aux|grep nginx root 14528 0.0 0.0 103240 844 ? S 10:04 0:00 grep nginx root 17996 0.0 0.2 99424 4752 ? Ss May14 0:00 nginx: master process nginx nginx 18030 0.5 0.3 103436 7228 ? S May14 14:41 nginx: worker process nginx 18031 0.5 0.3 103916 7284 ? S May14 14:50 nginx: worker process nginx 18032 0.5 0.3 103436 7240 ? S May14 14:49 nginx: worker process nginx 18033 0.5 0.3 104140 7328 ? S May14 14:54 nginx: worker process

   

$ 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模块,其实这个没啥讲解的。。。

Python
$ ansible web -m ping 10.150.145.53 | success 。。 { "changed": false, "ping": "pong" }

   

$ ansible web -m ping
10.150.145.53 | success 。。 {
    "changed": false,
    "ping": "pong"
}


关于业务的redis的模块



Python
$ ansible web -m redis -a "command=flush flush_mode=all" 10.150.145.53 | success { "changed": true, "flushed": true }

   

$ ansible web -m redis -a "command=flush flush_mode=all"
10.150.145.53 | success{
    "changed": true,
    "flushed": true
}


官方给的一些例子还是很全面的。

Python
# 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

   

# 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里面加入

Python
tasks: - cron: name="check dirs" hour="5,2" job="ls -alh > /dev/null" - name: create {{ user }} on web user: name="{{ user }}"

   

tasks:
- cron: name="check dirs" hour="5,2" job="ls -alh > /dev/null"
- name: create {{ user }} on web                           
user: name="{{ user }}"


如果咱们想看下 一个模块的使用方法,需要调用ansible-doc的命令。

Python
$ 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-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

一会会做相关的补充

Python
#添加一个用户,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 # 开启路由转发的功能 - sysctl: name="net.ipv4.ip_forward" value=1 sysctl_set=yes #下载 - name: download foo.conf get_url: url=http://xiaorui.cc/path/file.conf dest=/etc/foo.conf mode=0440

#添加一个用户,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

# 开启路由转发的功能
- sysctl: name="net.ipv4.ip_forward" value=1 sysctl_set=yes

#下载
- name: download foo.conf
get_url: url=http://www.iyunv.com/path/file.conf dest=/etc/foo.conf mode=0440



页: [1]
查看完整版本: 开发Ansible modules模块来自定义集群管理