设为首页 收藏本站
查看: 1541|回复: 0

[经验分享] 利用ansible modules模块来自定义集群管理

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-7-15 13:25:52 | 显示全部楼层 |阅读模式
前沿:
   在一些个特定环境下,用ansible做集群管理还是很棒的,这两天看了他的模块,官方提供了很多,就算不够,你也可以自定义定制。 话说我挺喜欢他的modules模块的,够直接 !!!  

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

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

1
2
3
4
5
6
7
[iyunv@devops-ruifengyun ~ ]$ ansible web -m service -a "name=nginx state=started"
10.150.145.53 | success >> {
    "changed": false,
    "name": "nginx",
    "state": "started"
}
[iyunv@devops-ruifengyun ~ ]$




紧接着我们想知道他是否真的启动了,调用command模块,用来执行系统的命令。 lsof -i :80   返回值告诉我们,nginx已经ok了。
ansible web -m command -a "你要推送的命令"

1
2
3
4
5
6
7
8
9
[iyunv@devops-ruifengyun ~ ]$ 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)
[iyunv@devops-ruifengyun ~ ]$




但是貌似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'




不信可以试试!
1
2
3
4
5
6
7
8
9
10
11
12
13
[iyunv@devops-ruifengyun ansible ]$ 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




除了自己写模块外,还可以调用shell、raw模块来解决这个问题。
1
2
3
4
5
6
7
8
9
[iyunv@devops-ruifengyun ansible ]$ 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




关于ping模块,其实这个没啥讲解的。。。

1
2
3
4
5
[iyunv@devops-ruifengyun ~ ]$ ansible web -m ping
10.150.145.53 | success >> {
    "changed": false,
    "ping": "pong"
}




关于业务的redis的模块

1
2
3
4
5
[iyunv@devops-ruifengyun ~ ]$ 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
[iyunv@devops-ruifengyun /var ]$ ansible-playbook l.yaml
PLAY [create user] ************************************************************
TASK: [cron name="check dirs" hour="5,2" job="ls -alh > /dev/null"] ***********
changed: [10.150.145.53]
TASK: [create xiaorui on web] *************************************************
ok: [10.150.145.53]
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
[iyunv@devops-ruifengyun ansible ]$ 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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-22077-1-1.html 上篇帖子: 关于ansible的playbook模板和facts的后续文档 下篇帖子: ansible通过cmdb资产接口动态创建hosts列表
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表