利用ansible modules模块来自定义集群管理

本文涉及的产品
云数据库 Redis 版,标准版 2GB
推荐场景:
搭建游戏排行榜
云原生内存数据库 Tair,内存型 2GB
简介:

前沿:

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


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


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


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


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

ansible web -m command -a "你要推送的命令"


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


但是貌似command模块不识别管道,这个有点郁闷。

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


不信可以试试!

原文:http://rfyiamcool.blog.51cto.com/1030776/1414417

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@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
[root@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
[root@devops-ruifengyun ~ ]$ ansible web -m ping
10.150 . 145.53  | success >> {
     "changed" false ,
     "ping" "pong"
}


关于业务的redis的模块


1
2
3
4
5
[root@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计划任务模块的使用 !

原文:http://rfyiamcool.blog.51cto.com/1030776/1414417 


在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
[root@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
[root@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



 本文转自 rfyiamcool 51CTO博客,原文链接:http://blog.51cto.com/rfyiamcool/1414417,如需转载请自行联系原作者

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
11月前
|
运维 Shell Linux
Ansible自动化运维工具之常用模块使用实战(5)
Ansible自动化运维工具之常用模块使用实战(5)
228 0
|
网络协议 网络安全
Ansible模块介绍——防火墙模块
Ansible模块介绍——防火墙模块
232 0
|
21天前
|
运维 算法 调度
深入理解操作系统:进程调度与优先级自动化运维:使用Ansible实现服务器集群管理
【8月更文挑战第27天】在操作系统的众多奥秘中,进程调度无疑是一个既简单又复杂的主题。它就像是交响乐团中的指挥,协调着每一个音符,确保乐曲和谐而有序地进行。本文将带领读者走进进程调度的世界,探索其背后的原理和实现,同时通过代码示例揭示其精妙之处。让我们一起揭开进程调度的神秘面纱,理解它在操作系统中的重要性。
|
1月前
|
缓存 Shell Linux
[ansible]常用内置模块
[ansible]常用内置模块
|
2月前
|
Shell 应用服务中间件 Linux
Ansible的常用模块
Ansible的常用模块
43 6
|
2月前
|
Shell 数据安全/隐私保护
Ansible Ad-hoc,命令执行模块
Ansible Ad-hoc,命令执行模块
23 1
|
2月前
|
运维 Linux 应用服务中间件
Linux之自动化运维工具ansible、ansible模块(2)
Linux之自动化运维工具ansible、ansible模块(2)
|
2月前
|
运维 Linux Shell
Linux之自动化运维工具ansible、ansible模块(1)
Linux之自动化运维工具ansible、ansible模块(1)
|
4月前
|
算法 安全 Linux
Ansible 中的copy 复制模块应用详解
Ansible 中的copy 复制模块应用详解
277 1
|
11月前
|
运维 Linux
Ansible自动化运维工具之常用模块使用实战(6)
Ansible自动化运维工具之常用模块使用实战(6)
171 0