ansible常用命令模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ansible命令的常用模块使用-ad-hoc模式-批量操作实战

comman/shell模块 远程执行命令或脚本模块
copy 模块发送文件到远程主机模块
yum 模块远程安装软件(相当于到远端机器执行yum -y install xxx)
service 模块远程管理服务启停模块script模块本地编写脚本,到远程主机执行
file 模块远程传目录或文件,做软连接等
group 模块远程创建和管理用户组user模块远程创建和管理用户
cron 模块远程添加定时任务
mount 模块远程添加挂载
get_url 模块远程下载文件
systemd 模块通过systemd来管理服务启停,类似systemctl start httpd
selinux 模块远程控制selinux开启或关闭
setup 模块主机信息模块,获取主机的信息

ansible的相关概念和使用场景

1
2
3
4
5
6
7
8
ansible是一个自动化的配置管理工具, ansible集成了丰富的模块,以及强大的功能组件,可以通过一个命令行批量完成一系列的操作。进能减少我们重复性的工作,以提高工作的效率。

简单的可以理解为: ansible就是一个批量操作工具

使用场景:
比如: 批量部署服务,我们想有20台服务器,都需要安装nginx服务,用手动?如果100台呢,这种情况就体现到了ansible的作用
又如: 批量更新配置文件,我们有30台机器,配置文件需要统一进行修改,也会用到ansible
又如: 批量发版上线或重启服务,等等,只要需要批量操作时候都可以使用ansible

ansible的主要功能

1
2
3
4
5
1).批量执行远程命令,可以对N多台主机同时进行命令的执行。
2).批量配置软件服务,可以进行自动化的方式配置和管理服务。
3).实现软件开发功能,jumpserver堡垒机底层就是使用ansible来实现的自动化批量管理。
4).编排高级的IT任务, ansible的playbook是一门编程语言,可以用来描绘一套IT剧本架构,完成复杂的任务。类似shell脚本
5),通过roles角色定义,可以定义部署某一个角色任务或某一些角色任务

ansible的特点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1).学习成本低,学习容易
ansible不需要启动任何服务,仅需要安装对应的ansible工具即可,也不需要单独安装客户端, ssh相当于ansible的客户端不像salt既要学客户端与服务端,还需要学习客户端与服务端中间通讯协议

2).操作灵活
ansible有较多的模块,提供了丰富的功能,可根据需要灵活使用各个模块功能,playbook则提供类似于编程语言的复杂功能

3).简单易用
体现在ansible一个命令可以完成很多事情

4).安全可靠
因为ansible使用了ssh协议进行通讯,既稳定也安全;移植性高:可以将写好的playbook拷贝至任意机进行执行, tcp+ssh的方式传输

5).幂等性
一个任务执行1遍和执行n遍效果一样,不会因为重复执行带来意外情况

6).需要python环境
ansible依赖大量的python模块来实现批量管理,需要有python环境: python2.6/2.7/3.x

ansible的配置文件

1
2
3
[root@localhost ~]# rpm-qc ansible
/etc/ansible/ansible.cfg # 主配置文件
/etc/ansible/hosts # 主机清单文件

ansible的安装方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
1).rpm安装(一般使用yum安装) (推荐使用)
# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# yum -y install ansible
# 或着
# yum install epel-release
# yum -y install ansible

2).pip安装(比较麻烦)
# yum install python3 python3-devel python3-pip-y
# pip3 install --upgrade pip -i https://pypi.douban.com/simple/
# pip3 install ansible-i https://pypi.douban.com/simple/
# /usr/local/bin/ansible --version

推荐使用yum安装,因为使用ansible就是因为它的简单、方便,如果为了使用它,仅安装就大费周折,还不如不用

安装过程

1
2
3
4
5
6
7
8
9
[root@node1 ~]# yum install epel-release		#先安装epel-release
[root@node1 ~]# yum -y install ansible
[root@node1 ~]# ansible --version
ansible 2.9.27
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Aug 7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

ansible的基本配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1)管理端配置主机管理:在hosts文件中添加被管理主机的ip地址列表:
[root@node1 ~]# vim /etc/ansible/hosts
[test] # 添加一个组名
192.168.88.162 # 添加被管理的主机ip
192.168.88.163 # 添加被管理的主机ip

2)修改ansible的配置文件
[root@node1 ~]# vim /etc/ansible/ansible.cfg
....
host_key_checking = False #禁用每次执行ansbile命令检查ssh key host,默认注释,开启即可
#首次连接是否需要检查kev认证,建议放开注释设为False

log_path = /var/log/ansible.log # 开启日志记录,默认注释,开启即可
accelerate_port = 5099 # 加速连接端口,释放,默认注释,也可改变端口号,此处没改
accelerate_multi_key = yes # 多密钥释放,默认注释

ansible命令的常用模块使用-ad-hoc模式-批量操作实战

ansible的ad-hoc模式或命令模式简介

1
ansible在命今行中执行的命今,也称为ad-hoc模式,ad-hoc模式其实就是"临时命今"执行完即结束,不会保存用ansible命令模式批量管理主机,对复杂的不方便,需要使用playbook剧本模式。

ansible常用的模块使用-通过ad-hoc命令行使用

command或shell模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1).command或shell模块,执行远程命令,管理被管理端(ad-hoc模式,其实就是临时命令,执行完即结束,不会保存)(都是批量执行命令, shell更强大,什么都能干,如果需要一些管道等复杂命令的操作,则使用shell, command完成不了, shell还能执行脚木

执行远程命令:以下的command也可以用shell代替
(-m是指定模块)
(这里的组名是指在vim /etc/ansible/hosts下添加的[test] # 添加一个组名)
ansible 组名 -m command/shell -a "执行的远程命令" # 管理单独某个模块组名下机器,执行远程机器命令
# 实操
[root@node1 ~]# ansible test -m command -a "free -m" # 查询系统内存
192.168.88.162 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 3770 351 3191 15 227 3192
Swap: 3967 0 3967
192.168.88.163 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 3770 184 3418 11 167 3379
Swap: 3967 0 3967

######################
ansible all -m command -a "执行的远程命令" #管理所有模块下机器,执行远程机器命令
######################
ansible test -m command -a "ifconfig|grep ens33" -f 50 #command执行不了,-f 50一次显示50个主机
ansible test-m shell -a "ifconfig|grep ens33"-f 50 #shell可以执行,-f 50一次显示50个主机

copy模块

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
2).copy模块,批量发送文件到被管理端或向被管理端文件写内容
copy模块下常用参数:
src: 推送数据的源文件信息
dest: 推送数据的目录路径
backup: 对推送传送过去的文件,进行原文件备份,再接收新文件
content: 直接批量在皴管理端文件中添加内容
group: 将本地文件推送到远端,指定文件属组信息
owner: 将本地文件推送到远端,指定文件属主信息
mode: 将本地文件推动到远端,指定文件权限信息

(1).将管理端(ansible机器)上本地文件(/tmp/a.txt)批量发送给被管理端(/tmp/目录):
copy模块注意:所有被管理端需要安装: libselinux-python,此处为192.168.171.129和192.168.171.130上)[root@localhost ~]# yum install libselinux-python -y # 默认cent7.x已经安装,若没有安装,需要先安装该包a)批量发送文件:
管理端:
[root@node1 ~]# echo 11111 > /tmp/a.txt
[root@node1 ~]# cat /tmp/a.txt
11111
[root@node1 ~]# ansible test -m copy -a "src=/tmp/a.txt dest=/tmp/"
192.168.88.163 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "659f5f85766824a4290371a2abfaeb007cd0af4a",
"dest": "/tmp/a.txt",
"gid": 0,
"group": "root",
"md5sum": "fa8f294721ab3fbb37793c68ff2cf09b",
"mode": "0644",
"owner": "root",
"size": 6,
"src": "/root/.ansible/tmp/ansible-tmp-1692524423.9-3528-235526458743393/source",
"state": "file",
"uid": 0
}
192.168.88.162 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "659f5f85766824a4290371a2abfaeb007cd0af4a",
"dest": "/tmp/a.txt",
"gid": 0,
"group": "root",
"md5sum": "fa8f294721ab3fbb37793c68ff2cf09b",
"mode": "0644",
"owner": "root",
"size": 6,
"src": "/root/.ansible/tmp/ansible-tmp-1692524423.91-3526-156070185170456/source",
"state": "file",
"uid": 0
}

yun模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
3).yum模块,批量安装软件(相当于到远端机器执行yum -y install xxx)
格式: # ansible test -m yum-a "name=要安装的服务名state=installed"
例子: 如: ansible test-m yum -a "name=httpd state=installed"
使用详解:
name:指定要安装的软件包名称
name的常用参数:即是常用软件包的名称,如:httpd, ....state:指定使用yum的方法进行安装,卸载等操作
state的常用参数如下:
installed,present 安装软件包
removed,absent 移除软件包
latest 安装最新软件包
例子:
管理端:
[root@node1 ~]# ansible test -m yum -a "name=httpd state=installed" #安装Apache HTTP 服务器
[root@node1 ~]# ansible test -m command -a "systemctl start httpd" # 启动

service模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
4).service模块,启动,停止,重启,重载服务等
格式: # ansible test-m service-a "name=服务名state=stopped enabled=yes"
例子:如: ansible test -m service-a "name=httpd state=stopped enabled=yes"

使用详解:name:定义要启动服务的名称,参数即为各服务名
state:指定服务状态是停止或运行,或重载等,参数如下:
started 启动
stopped 停止
restarted 重启
reloaded 重载
enabled:是否让服务开机自启动
例子:
管理端:
# 启动httpd服务器,并且让其开机自启
[root@node1 ~]# ansible test -m service -a "name=httpd state=started enabled=yes"

script模块

1
2
3
4
5
6
7
8
9
10
5).script模块,编写脚本和执行脚本(本地编写脚本,本地运行,即可等同于在远程执行)
在本地运行模块,等同于在远程执行,不需要将脚本文件进行推送目标主机执行。
格式: # ansible test -m script -a"/.../本地编写的脚本.sh"
例子:
管理端:
[root@node1 ~]# cat yum_wget.sh
#!/bin/bash
yum install wget
[root@node1 ~]# chmod +x yum_wget.sh
[root@node1 ~]# ansible tes t-m script -a"/root/yum_wget.sh"

file模块

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
57
58
6).file模块,配置模块,远程创建目录,远程创建文件,远程做软硬链接文件
远程创建目录:
ansible test -m file -a "path=/tmp/shi state=directory"

远程创建文件:
ansible test -m file -a "path=/tmp/shi.txt state=touch mode=555 owner=root group=root"

远程做软连接:
ansible test -m file-a "src=/tmp/shi.txt path=/tmp/shi.txt_link state=link"

递归创建或更改目录权限:
# recurse=yes:递归地应用上述配置,确保所有子目录和文件都具有相同的权限和所有者。
ansible test -m file -a "path=/tmp/shi state=directory owner=root group=root mode=600 recurse=yes"

path:指定远程主机目录或文件目录
recurse: 递归授权
state:
directory: 在远端创建目录
touch: 在远端创建文件
link: link或hard表示创建链接文件
absent: 表示删除文件或目录
mode: 设置文件或目录权限
owner: 设置文件或目录属主信息
group: 设置文件或目录属组信息

例子:
管理端:
# 创建shi文件夹
[root@node1 ~]# ansible test -m file -a "path=/tmp/shi state=directory"
192.168.88.163 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/shi",
"size": 6,
"state": "directory",
"uid": 0
}
192.168.88.162 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/shi",
"size": 6,
"state": "directory",
"uid": 0
}

group模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
7).group模块,远程创建组格式: 
ansible test-m group-a "name=要创建的组名 gid=888 state=present" #创建组,指定gid

例子,如:
[root@node1 ~]# ansible test-m group -a "name=shi_group gid=888 state=present"

name: 指定创建的组名
gid: 指定组的gid
state:表示对组的操作状态,参数如下:
absent:删除远端的组
present: 创建远端的组(默认)

例子:
管理端:
# 创建一个组
[root@node1 ~]# ansible test-m group-a "name=shi_group gid=888 state=present"

被管理端:
# 查看是否创建成功
[root@node1 ~]# tail -2 /etc/group
apache:x:48:
shi_group:x:888:

user模块

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
8).user模块,远程创建用户创建用户:不加密码:
ansible test -m user -a "name=shi uid=88 group=shi_group shell=/sbin/nologin create_home=no state=present"

删除用户:
ansible test-m user -a "name=shi uid=88 group=shi_group shell=/sbin/nologin create_home=no state=absent"

创建普通用户并设置登录密码:
echo 'mima' |openssl passwd -1 -stdin #给指定的密码内容加密,注意需要加密,用户才能登录$1$PxrQduFH$0sqImb.R6gy80gm8qlUvco

ansible test -m user -a 'name=shi3 password="$1$PxrQduFH$0sqImb.R6gy80gm8qlUvc0"

name: 指定创建的用户名
uid:指定用户的uid
gruop:指定用户组名称
gruops:指定附加组名称
password: 给用户添加密码
shell:指定用户登录shell
create_home: 是否创建家目录
state:表示对用户的操作状态,参数如下:
absent: 删除远端的组
present: 创建远端的组(默认)

例子:
管理端:
# shell=/sbin/nologin是不允许ssh登陆
ansible test-m user-a "name=shi uid=88 group=shi-group shell=/sbin/nologin create-home=no state=present" #创建不加密码

所有被管理端即可创建用户shi:
[root@node1 ~]# id shi
uid=88(shi) gid=888(shi_group) groups=888(shi_group)

创建普通用户并设置登录密码:
管理端:
[root@node1 ~]# echo 'mima' |openssl passwd-1-stdin #给指定的密码内容加密,注意需要加密,用户才能登录
$1$PxrQduFH$0sqImb.R6gy80gm8qlUvc0
[root@node1 ~]#ansible test -m user -a 'name=shi3 password="$1$PxrQduFH$0sqImb.R6gy80gm8qlUvco"

cron模块

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
9).cron模块,远程添加定时任务 (下面:a.sh是远程机器上本地有的脚本)
远程添加定时任务,未设置注释信息:
ansible test -m cron -a "minute=00 hour=01 day=* month=* weekday=* job="/bin/sh /root/a.sh' state=present"

远程添加定时任务,并设置注释信息,防止定时任务重复:
ansible test-m cron-a "minute=00 hour=01 day=* month=* weekday=* namo='注释信息" job=/bin/sh /root/a.sh' state=present"

远程注释定时任务:
ansible test -m cron -a "minute=00 hour=01 day=* month=* weekday=* name='cron1' job='/bin/sh /root/a.sh' state=present disabled=yes"

远程删除定时任务:
ansible test -m cron -a "minute=00 hour=01 day=* month=* weekday=* name='cron1' job='/bin/sh /root/a.sh' state=absent"

例子:
管理端:
#远程添加定时任务,未设置注释信息
[root@localhost ~]# ansible test-m cron-a "minute=00 hour=01 day=* month=* weekday=* job='/bin/sh /root/a.sh' state=present"

所有被管理端
[root@localhost ~]# crontab -l
# Ansible: None
00 01 * ** /bin/sh /root/a.sh

管理端
#远程添加定时任务,并设置注释信息,防止定时任务重复
[root@localhost ~]# ansible test-m cron-a "minute=00 hour=01 day=* month=* weekday=* name='cron1' iob='/bin/sh /root/a.sh' state=present"

所有被管理端
[root@localhost ~]# crontab -l
# Ansible: cron1
00 01 * ** /bin/sh /root/a.sh

管理端:
#远程注释定时任务
[root@localhost ~]# ansible test-m cron-a "minute=00 hour=01 day=*month=* weekday=* name='cron1' job='/bin/sh /root/a.sh' state=present disabled=yes"

所有被管理端
[root@localhost ~]# crontab -l
# Ansible: cron1
00 01 * ** /bin/sh /root/a.sh

mount模块

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
10).mount模块,远程添加挂载
立刻挂载并写入/etc/fstab中:
ansible test -m mount -a "src=192.168.88.161:/data path=/opt fstype=nfs opts=defaults,noatime state=mounted"

立刻卸载并清除/etc/fstab中信息:
ansible test-m mount -a "src=192.168.88.161:/data path=/opt fstype=nfs opts=defaults,noatime state=absent"

src:要被挂载的原目录
path: 要挂载到的本地目录
fstype:要挂载的文件类型
state: 挂载或卸载的状态,常用参数如下:
present:开机挂载,不会直接挂载设备,仅将配置写入/etc/fstab,不会马上挂载
mounted:马上直接挂载设备,并将配置写入/etc/fstab
unmounted:马上直接卸载设备,不会清除/etc/fstab写入的配置
absent:马上直接卸载设备,会清理/etc/fstab写入的配置

例子
管理端:192.168.88.161
[root@node1 ~]# yum -y install nfs-utils # 被管理得挂载端也要安装,才能挂载
[root@node1 ~]# cat /etc/exports
/data *(rw,no_root_squash)
[root@node1 ~]# systemctl start nfs # 启动nfs
[root@node1 ~]# ansible test -m mount -a "src=192.168.88.161:/data path=/opt fstype=nfs opts=defaults,noatime state=mounted"
192.168.88.163 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dump": "0",
"fstab": "/etc/fstab",
"fstype": "nfs",
"name": "/opt",
"opts": "defaults,noatime",
"passno": "0",
"src": "192.168.88.161:/data"
}
192.168.88.162 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dump": "0",
"fstab": "/etc/fstab",
"fstype": "nfs",
"name": "/opt",
"opts": "defaults,noatime",
"passno": "0",
"src": "192.168.88.161:/data"
}

get_url模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@node2 ~]# ll /tmp/
总用量 24
-rw-r--r-- 1 root root 6 8月 20 17:40 a.txt
-rw-rw-rw- 1 root root 20124 8月 20 20:22 remi-release-6.rpm
11).get_url模块,下载模块
下载模块: get_url:
url:下载地址
dest: 下载到本地的路径;
mode:权限;
checksum:对资源做校验;
sha256:
md5:

例子:
管理端:192.168.88.161
[root@node1 ~]# ansible test -m get_url -a 'url=http://rpms.famillecollet.com/enterprise/remi-release-6.rpm dest=/tmp mode=0666'

被管理端:192.168.88.162
[root@node2 ~]# ll /tmp/
总用量 24
-rw-r--r-- 1 root root 6 8月 20 17:40 a.txt
-rw-rw-rw- 1 root root 20124 8月 20 20:22 remi-release-6.rpm

systemd模块

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
12).systemd模块,通过systemd来管理服务启停,类似systemctl start httpd
解释:
name 服务名称
state 服务状态
started 启动
stopped 停止
restarted 重启
reloaded 重载
enabled 开启自启动| yes 启 no 不
daemon_reload: yes 重载systemd整个的配置文件

例子:用systemd模块启动或停止服务,加入开机自启动或关闭开机自启
管理端:192.168.88.161
#先用别的命令,远程批量安装httpd
[root@localhost ~]# ansible test -m command -a 'yum -y install httpd"
#查看,刚安装的服务并未启动
[root@localhost ~]# ansible test -m command -a 'systemctl status httpd'
192.168.88.162 | FAILED | rc=3 >>
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: inactive (dead) since 日 2023-08-20 20:33:53 CST; 1min 1s ago
Docs: man:httpd(8)
man:apachectl(8)

192.168.88.163 | FAILED | rc=3 >>
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd(8)
man:apachectl(8)

# 使用systemd模块启动服务并加入开机自启
[root@node1 ~]# ansible test -m systemd -a 'name=httpd state=started enabled=yes'

# 使用systemd模块关闭服务并取消开机自启
[root@node1 ~]# ansible test -m systemd -a 'name=httpd state=stopped enabled=on'

selinux模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
13).selinux模块,控制selinux开启或关闭
- name: Enable SELinux
selinux:
policy: targeted
state: disabled

管理端:192.168.88.161
[root@node1 ~]# ansible test -m command -a 'getenforce' # 先远程查看被管理端得selinux是否开着
192.168.88.163 | CHANGED | rc=0 >>
Enforcing # 表示开着
192.168.88.162 | CHANGED | rc=0 >>
Enforcing

管理端:192.168.88.161
# 远程关闭被管理端selinux
[root@node1 ~]# ansible test -m selinux -a 'state=disabled'

playbook

playbook相关介绍

1
2
3
1).playbook是一个由yml语法编写的文本文件,它由play和task两部分组成。play:主要定义要操作主机或者主机组task:主要定义对主机或主机组具体执行的任务,可以是一个任务,也可以是多个任务(模块)

2).playbook是由一个或多个模块组成的,使用多个不同的模块,共同完成一件事情。Playbook通过yaml语法识别描述的状态文件,扩展名是yaml.

yaml三板斧

1
2
3
1).缩进:yaml使用一个固定的缩进风格表示层级结构,每个缩进由两个空格组成,不能使用tab键。
2).冒号:以冒号结尾的除外,其他所有冒号后面所有必须有空格。
3).短横线:表示列表项,使用一个短横线加一个空格作为一个列表项,多个项使用同样的缩进级别作为同一列表。

ansible-playbook得实战案例

案例1: 用ansible-playbook方式远程批量安装httpd-若修改完配置

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
案例1: 用ansible-playbook方式远程批量安装httpd-若修改完配置,重新推送后,配置改了但没重载服务,不生效

管理端:192.168.88.161
[root@localhost ~]# ls
httpd.conf httpd_install.yaml
[root@localhost ~]# vim httpd_install.yaml
#这是一个ansible的playbook
#第一步:找到谁,hosts:定义主机清单,ansible的hosts文件里定义的主机清单模块名
#第二步:大概做的任务:安装,配置,启动
#第三步:具体怎么做
#name:描述信息,task里有3个同级别的列表步骤
#yum: 远端安装服务,yum模块安装服务(installed)
#copy: 远端拷贝文件,copy模块传送文件到远端
#service:远端启动服务(started)#remote_user: root 是指定远程主机上使用的用户
#gather_facts: no是默认执行playbook时候,默认会收集目标主机的信息,禁用掉能提高效率
- hosts: test # 组名
remote_user: root # 用户
gather_facts: no
# tasks(任务)是一个必要的部分。它定义了在执行Playbook时需要完成的具体操作步骤。每个任务由一个或多个模块组成,这些模块用于执行特定的操作。在Playbook中按顺序列出的任务将按照顺序依次执行。
tasks:
- name: install httpd fuwu
yum: name=httpd,httpd-tools state=installed
- name: configure httpd fuwu
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: Restart httpd fuwu
- name: qidong httpd fuwu
service: name=httpd state=started enabled=yes
# handlers(处理程序)是一种特殊类型的任务,它定义了在Playbook中需要根据需要触发执行的操作
handlers:
- name: Restart httpd fuwu
service: name=httpd state=restarted

删除服务器
#这是一个ansible的playbook
#第一步:找到谁,hosts:定义主机清单,ansible的hosts文件里定义的主机清单模块名
#第二步:大概做的任务:安装,配置,启动
#第三步:具体怎么做#name:描述信息,task里有3个同级别的列表步骤
#yum:远端安装服务,yum模块安装服务远端拷贝文件,copy模块传送文件到远端
#notify:当该项中的配置文件内容有变更时候,会触发下而的handlers的重启操作(根据handler描述信息关联触发)
#handler:当被触发后执行的操作,重启httpd服务#copy:#service:远端启动服务
#remote user:指定远程主机上使用的用户
#gather_facts:默认执行playbook时候,默认会收集目标主机的信息,禁用掉能提高效率
- hosts: test
remote_user: root
gather_facts: no
tasks:
- name: remove httpd fufu
yum: name=httpd,httpd-tools state=absent
- name: remove apache user
user: name=apache state=absent
- name: remove data file
file: name=/etc/httpd state=absent

案例2: 在管理端安装nfs服务,在被管理端批量挂载nfs的共享目录

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
[root@localhost ~]# cat /etc/ansible/hosts 
[test] #添加一个组名
192.168.88.163 #添加被管理主机的IP
192.168.88.162 #添加被管理主机的IP
[root@localhost ~]# yum-y install nfs-utils #管理端和被管理的挂载端都要安装,才能挂载
[root@localhost ~]# vim /etc/exports
/data *(rw,no_root_squash)
[root@localhost ~]# ls /data/
a.txt
[root@localhost ~]# cat /data/a.txt
111
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# cat web_mount.yaml
#test: 为/etc/ansible/hosts中的主机列表 #task: 执行的任务
#name:描述信息#mount: mount模块
#state=mounted:马上直接挂载设备,并将配置写入/etc/fstab
#remote_user: root是指定远程主机上使用的用户
#gather_facts: no是默认执行playbook时候,默认会收集目标主机的信息,禁用掉能提高效率
- hosts: test
remote_user: root
gather_facts: no
tasks:
- name: Mount nfs server share data
mount: src=192.168.171.128:/data path=/data fstype=nfs opts=defaults state=mounted
#若将state=absent,则立刻卸载并清除/etc/fstab中信息

案例3:远程批量安装源码nginx服务

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#test:为/etc/ansible/hosts中的主机列表		 #task:执行的任务
#name:描述信息 #yum: yum模块,安装服务的
#copy: copy模块,远程传递文件的 #file: file模块,远程创建目录的
#service: service模块,远程管理服务的
#remote_user: root是指定远程主机上使用的用户
#gather_facts: no是默认执行playbook时候,默认会收集目标主机的信息,禁用掉能提高效率
#使用前先将相关软件包: nginx压缩包和jdk压缩包上传到/root/ansible-playbook-deploy-nginx/目录中
- hosts: test
remote_user: root
gather_facts: no
vars:
src_nginx: /root/nginx-1.13.7.tar.gz
nginx_jieya_dir: /usr/local
nginx_install_dir: /usr/local/nginx
nginx_jieyahou_name: nginx-1.13.7

tasks:
#上传nginx压缩安装包到: /root/ansible-playbook-deploy-nginx/,提前操作
#安装编译工具和相关依赖
#- name: Install gcc gcc-c++ and yilai
# yum: name={{ item }} state=installed
# with_items:
# - gcc
# - gcc-c++
# - openssl-devel
# - openssl
# - zlib
# - zlib-devel
# - pcre
# - pcre-devel
#注意:下面yum安装依赖方式也可用上面方式安装
#安装编译工具和相关依赖
- name: Install gcc gcc-c++ and yilai
yum:
name: "gcc,gcc-c++,openssl-devel,openssl,zlib,zlib-devel,pcre,pcre-devel,vim,wget"
state: installed
#解压nginx压缩包
- name: Unarchive nginx package
unarchive:
src: "{{ src_nginx }}"
dest: "{{ nginx_jieya_dir }}"
# 配置编译nginx
- name: Config and bianyi nginx
shell: |
useradd -s /sbin/nologin nginx
cd {{ nginx_jieya_dir }}/{{ nginx_jieyahou_name }}

#./configure 是运行 Nginx 的配置脚本。
#--user=nginx 指定运行 Nginx 进程的用户为 nginx 用户。
#--group=nginx 指定运行 Nginx 进程的用户组为 nginx 组。
#--prefix={{ nginx_install_dir }} 指定安装 Nginx 的目录为 {{ nginx_install_dir }}。
#--with-http_stub_status_module 启用 Nginx 的 HTTP status 模块,用于获取服务器状态信息。
#--with-http_ssl_module 启用 Nginx 的 SSL 模块,支持通过 HTTPS 访问。

#make 命令用于编译 Nginx 源代码。
#make install 命令用于将编译好的 Nginx 可执行文件和相关文件安装到指定目录中,即 {{ nginx_install_dir }}。
./configure --user=nginx --group=nginx --prefix={{ nginx_install_dir }} --with-http_stub_status_module --with-http_ssl_module
make
make install

- name: Start nginx
shell: /usr/local/nginx/sbin/nginx

#注意上面解压也可用另一种方式: shell命令
# - name: Unarchive tomcat package
# copy: src=/root/ansible-playbook-deploy-tomcat/apache-tomcat-8.0.32.tar.gz dest=/tmp/
# - name: Unarchive tomcat
# shell: cd /tmp && tar -zxf apache-tomcat-8.0.32.tar.gzxxxxxxxxxx 


没注释的
- hosts: test
remote_user: root
gather_facts: no
vars:
src_nginx: /root/nginx-1.13.7.tar.gz
nginx_jieya_dir: /usr/local
nginx_install_dir: /usr/local/nginx
nginx_jieyahou_name: nginx-1.13.7

tasks:
- name: Install gcc gcc-c++ and yilai
yum:
name: "gcc,gcc-c++,openssl-devel,openssl,zlib,zlib-devel,pcre,pcre-devel,vim,wget"
state: installed

- name: Unarchive nginx package
unarchive:
src: "{{ src_nginx }}"
dest: "{{ nginx_jieya_dir }}"

- name: Config and bianyi nginx
shell: |
useradd -s /sbin/nologin nginx
cd {{ nginx_jieya_dir }}/{{ nginx_jieyahou_name }}
./configure --user=nginx --group=nginx --prefix={{ nginx_install_dir }} --with-http_stub_status_module --with-http_ssl_module
make
make install

- name: Start nginx
shell: /usr/local/nginx/sbin/nginx

案例4:远程批量安装二进制tomcat服务

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
#test: 为/etc/ansible/hosts中的主机列表 #task:执行的任务
#name:描述信息#yum: yum模块,安装服务的
#copy: copy模块,远程传递文件的#file: file模块,远程创建目录的
#service: service模块,远程管理服务的
#remote_user: root是指定远程主机上使用的用户
#gather_facts: no是默认执行playbook时候,默认会收集目标主机的信息,禁用掉能提高效率
#使用前先将相关软件包: tomcat压缩包和jdk压缩包上传到/root/ansible-playbook-deploy-tomcat/目录中
- hosts: test
remote_user: root
gather_facts: no
vars:
src_jdk: /root/ansible-playbook-deploy-tomcat/jdk-8u65-linux-x64.gz
jdk_install_dir: /usr/local
jdk_jieyahou_name: jdk1.8.0_65
src_tomcat: /root/ansible-playbook-deploy-tomcat/apache-tomcat-8.0.32.tar.gz
tomcat_install_dir: /usr/local
tomcat_jieyahou_name: apache-tomcat-8.0.32

tasks:
#上传jdk压缩安装包到: /root/ansible-playbook-deploy-tomcat/,提前操作
#解压jdk压缩包
- name: Unarchive JDK package
unarchive:
src: "{{ src_jdk }}"
dest: "{{ jdk_install_dir }}"
#配置jdk环境变量
- name: set jdk global env
shell: echo "export JAVA_HOME=/usr/local/{{ jdk_jieyahou_name }}" >> ~/.bashrc && echo ""export PATH=$JAVA_HOME/bin:$PATH"" >> ~/.bashrc &&
echo "export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar" >> ~/.bashrc &&
source ~/.bashrc
#有yum源时jdk也可采用下面方式安装
#安装jdk环境
#- name: Install jdk1.8yum: name=java-1.8.0-openjdk state=installed
#上传tomcat软件包到/root/ansible-playbook-deploy-tomcat/目录,提前操作
#解压tomcat软件包
- name: Unarchive tomcat package
unarchive:
src: "{{ src_tomcat }}"
dest: "{{ tomcat_install_dir }}"
#start tomcat,注意: tomcat首次启动需要用 nohup ./startup.sh & 或 nohup ./catalina.sh & 启动,如果直接使用/.../.../tomcat.../bin/startup.sh则启动不了
- name: Start tomcat
shell: cd "{{ tomcat_install_dir }}" && cd "{{ tomcat_jieyahou_name }}"/bin && nohup ./startup.sh &
#注意上面解压也可用另一种方式: shell命令
#- name: Unarchive tomcat package
# copy: src=/root/ansible-playbook-deploy-tomcat/apache-tomcat-8.0.32.tar.gz dest=/tmp/
#- name: Unarchive tomcat
# shell: cd /tmp && tar-zxf apache-tomcat-8.0.32.tar.gz

该文章湫日所著,如有错误请联系作者更改