ansible之模块二及playbook

yum模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
yum源配置

yum查看安装包组
yum grouplist #查看
yum groupinstall 包名 #安装

#参数
disablerepo #禁用源
enablerepo #启动源
name #包名
state
install 或(present installd latest) #安装
remove 或(absent removed) #卸载

#yum安装nginx
ansible 192.168.8.32 -m yum -a 'name=nginx'

#安装python2 pip
ansible 192.168.8.32 -m yum -a 'name=python2-pip'

#yum安装包组 用@
ansible 192.168.8.32 -m yum -a 'name=@Cinnamon'

pip模块

1
2
3
4
5
6
7
8
pip install 包
pip freeze > ropon.txt #导出当前python环境依赖
pip install -r ropon.txt #安装文件中的包

#参数
requirements
#安装flask
ansible 192.168.8.32 -m pip -a 'name=flask'

service模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#参数
enabled #加入开机自启 noyes
name #服务名
state
started #启动服务
stopped #停止服务
restarted #重启服务
reloaded #重载服务
enabled #开机自启

#启动服务
ansible 192.168.8.32 -m service -a 'name=nginx state=started'
#查看
ansible 192.168.8.32 -m shell -a 'netstat -tunpl'
#停止服务
ansible 192.168.8.32 -m service -a 'name=nginx state=stopped'
#将nginx服务加入开机自启
ansible 192.168.8.32 -m service -a 'name=nginx enabled=yes'

cron模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#参数
day #天
disabled #禁用
hour #小时
minute #分组
month #月份
job #任务
name #任务名
weekday #周

#创建计划任务
ansible 192.168.8.32 -m cron -a 'minute=49 job="touch /root/cron_test.txt" name=crontest'
#删除计划任务
ansible 192.168.8.32 -m cron -a 'name=crontest state=absent'
#禁用计划任务 # 表示禁用
ansible 192.168.8.32 -m cron -a 'minute=49 job="touch /root/cron_test.txt" name=crontest2 disabled=yes'
#启动禁用任务
ansible 192.168.8.32 -m cron -a 'minute=49 job="touch /root/cron_test.txt" name=crontest2 disabled=no'

user模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
管理员
普通用户
系统用户

#参数
group #组
groups #附加组
home #家目录
name #用户名
password #密码
remove
shell
system
state #状态

#创建用户mysql 指定家目录/root/mysql 指定附加组root 指定uid 指定shell不能登录
ansible db -m user -a 'name=mysql uid=4000 home=/root/mysql groups=root shell=/sbin/nologin'

#删除mysql用户但不删除此用户家目录
ansible db -m user -a 'name=mysql state=absent'

#删除mysql用户并删除此用户家目录
ansible db -m user -a 'name=mysql state=absent remove=yes'

group模块

1
2
3
4
5
6
7
8
9
10
11
#参数
gid #组id
name #组名
system #系统组
state

#创建mysql系统组
ansible db -m group -a 'name=mysql system=yes'

#删除mysql系统组
ansible db -m group -a 'name=mysql state=absent'

ansible脚本

1
2
3
4
5
6
#yaml
#格式 严格缩进 严格对齐
#字典 key:value
#列表 -
#后缀名
#yaml、yml

ansible-playbook

1
2
3
4
5
6
7
8
#执行顺序 从上往下
#特性 不管执行多少次结果都一样

#参数
-C #检查 会执行一次脚本但不生效
-f #并发
--list-hosts #列出主机列表
--syntax-check #语法检查

简单例子

1
2
3
4
5
6
7
8
9
10
#创建用户例子
- hosts: 192.168.8.32
tasks:
- name: createuser
user: name=ropon01

#执行脚本
ansible-playbook p1.yml
#检查是否创建成功
ansible 192.168.8.32 -m shell -a 'id ropon01'

传参 动态执行脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
- hosts: 192.168.8.32
tasks:
- name: create{{ user }}
user: name={{ user }}

#方式一 -e
ansible-playbook -e 'user=ropon02' p2.yml
#检查是否创建成功
ansible 192.168.8.32 -m shell -a 'id ropon02'

#方式二 主机后面 指定参数 192.168.8.32 user=ropon03
ansible-playbook p2.yml
#检查是否创建成功
ansible 192.168.8.32 -m shell -a 'id ropon03'

#方式三 主机组[:vars] 组后面指定参数(新起一行写与之前的主机名)
[web:vars]
user=ropon04

cat p3.pml
- hosts: web
tasks:
- name: create{{ user }}
user: name={{ user }}

ansible-playbook p3.yml
#检查是否创建成功
ansible web -m shell -a 'id ropon04'

#方式四 yml文件vars指定参数
cat p3.pml
- hosts: web
vars:
- user: ropon05
tasks:
- name: create{{ user }}
user: name={{ user }}

ansible-playbook p4.yml
#检查是否创建成功
ansible web -m shell -a 'id ropon05'

#方式五 register
cat p5.yml
- hosts: web
tasks:
- name: sum
shell: echo 3+3bc
register: user
- name: createropon06
user: name=ropon0{{user.stdout}}

ansible-playbook p5.yml

#参数优先级
-e > playbook vars > 主机hosts文件