avatar

6.ansible服务

Ansible服务

第1章 Ansible介绍

1.1 什么是Ansible?

ansible是一种自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能,默认通过SSH协议管理机器。

ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架,不需要在远程主机上安装client/agents,因为它们是基于ssh来和远程主机通讯的。

1.2 为什么需要Ansible?

批量管理功能:

1)可以实现批量系统操作配置

2)可以实现批量软件服务部署

3)可以实现批量文件数据分发

4)可以实现批量系统信息收集

管理服务意义:

1)提高工作的效率(部署综合架构)

2)提高工作准确度

3)减少维护的成本

4)减少重复性工作

ansible 安装部署简单:

1)没有配置文件(不需要配置)

2)不需要启动服务

3)客户端没有需要部署任务

第2章 ansible 安装部署

Ansible的安装部署十分简单,只需要在管理机yum安装就行

1
[root@ssh-61 ~]# yum -y install ansible

第3章 ansible 主机清单

主机资产清单 /etc/ansible/hosts 文件,用于定义被管理主机的认证信息, 例如 ssh 登录用户名、密码以及 key相关信息。

1
2
3
4
1.主机支持主机名通配以及正则表达式,例如 web[1:3].oldboy.com 代表三台主机
2.主机支持基于非标准的 ssh 端口,例如 web1.oldboy.com:6666
3.主机支持指定变量,可对个别主机的特殊配置,如登陆用户,密码
4.主机组支持指定变量[group_name:vars],同时支持嵌套[game:children]

3.1 指定主机组相关配置

1
2
3
4
5
6
7
8
9
10
11
12
#主机组 第一种方式 (已经配置好ssh)
[root@ssh-61 ~]# cat /etc/ansible/hosts
[webserver]
172.16.1.31
172.16.1.41
#主机+端口+密码 第二种方式(ssh未分发公钥)
[webserver]
172.16.1.31 ansible_ssh_port=9999 ansible_ssh_user=root ansible_ssh_pass='123456'
172.16.1.41 ansible_ssh_port=9999 ansible_ssh_user=root ansible_ssh_pass='123456'
#对整个主机组都生效的变量
[webserver:vars]
ansible_ssh_pass='123456'

我们一般使用,先配置好ssh服务,分发公钥后,再指定主机组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@ssh-61 ~]# cat /etc/ansible/hosts
[webserver]
172.16.1.41
172.16.1.31

[webserver:vars]
ansible_ssh_port=9999
ansible_ssh_user=root
ansible_ssh_pass='123456'

[nfs]
172.16.1.31

[backup]
172.16.1.41

第4章 ansible 常用模块

ansible 官方网站 : https://docs.ansible.com

模块的应用语法格式:
ansible 主机名称/主机组名称/主机地址信息/all -m(指定应用的模块信息) 模块名称 -a(指定动作信息) “执行什么动作”

4.1 ping

应用场景:测试主机和ansible之间的连通性
举例:对webserver主机组测试是否连通

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@ssh-61 ~]# ansible webserver -m ping
172.16.1.41 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
172.16.1.31 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}

4.2 command 简单模块

应用场景:
类似shell,但是只能执行简单的命令,复杂的命令和有些符号并不能识别,用的比较少
01.查看主机名,可以执行成功

1
2
3
4
5
6
[root@ssh-61 ~]# ansible webserver -m command -a "hostname"
172.16.1.41 | CHANGED | rc=0 >>
backup

172.16.1.31 | CHANGED | rc=0 >>
nfs

02.使用awk拼接查看主机IP执行失败

1
2
3
4
5
6
7
8
[root@ssh-61 ~]# ansible webserver -m command -a "ifconfig eth1|awk 'NR==2{print $2}'"
172.16.1.41 | FAILED | rc=1 >>
NR==2{print }: Unknown host
ifconfig: `--help' gives usage information.non-zero return code

172.16.1.31 | FAILED | rc=1 >>
NR==2{print }: Unknown host
ifconfig: `--help' gives usage information.non-zero return code

4.3 shell 万能模块

万能模块,所有命令都可以执行,和本地执行效果一样

4.3.1 使用管道查询IP地址

1
2
3
4
5
6
[root@ssh-61 ~]# ansible webserver -m shell -a "ifconfig eth1|awk 'NR==2'"
172.16.1.31 | CHANGED | rc=0 >>
inet 172.16.1.31 netmask 255.255.255.0 broadcast 172.16.1.255

172.16.1.41 | CHANGED | rc=0 >>
inet 172.16.1.41 netmask 255.255.255.0 broadcast 172.16.1.255

4.3.2 批量执行脚本

在其他主机上创建一个脚本,内容为打印主机名

1
2
3
4
cat > echo.sh << EOF 
#!/bin/bash
echo "$(hostname)"
EOF

然后使用ansible的shell模块批量执行

1
2
3
4
5
6
[root@ssh-61 ~]# ansible webserver -m shell -a "/bin/bash /root/echo.sh"
172.16.1.31 | CHANGED | rc=0 >>
nfs

172.16.1.41 | CHANGED | rc=0 >>
backup

4.4 copy 拷贝文件

1.拷贝ssh-61的/etc/hostname文件到其他主机的/tmp目录下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@ssh-61 ~]# ansible webserver -m copy -a "src=/etc/hostname dest=/tmp"
172.16.1.41 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "c5b01b59eec249e998dd82c375b269fe10cff9d3",
"dest": "/tmp/hostname",
"gid": 0,
"group": "root",
"md5sum": "d61060d5859d1e67541238e8c296938e",
"mode": "0644",
"owner": "root",
"size": 7,
"src": "/root/.ansible/tmp/ansible-tmp-1563897217.78-184562543546238/source",
"state": "file",
"uid": 0
}

2.在copy 文件时修改文件属主和属组信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@ssh-61 ~]# ansible webserver -m copy -a "src=/etc/hostname dest=/tmp owner=oldboy group=oldboy"
172.16.1.31 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "c5b01b59eec249e998dd82c375b269fe10cff9d3",
"dest": "/tmp/hostname",
"gid": 1000,
"group": "oldboy",
"mode": "0644",
"owner": "oldboy",
"path": "/tmp/hostname",
"size": 7,
"state": "file",
"uid": 1000
}

3.在copy文件时修改文件的权限信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@ssh-61 ~]# ansible webserver -m copy -a "src=/etc/hostname dest=/tmp mode=0666"
172.16.1.41 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "c5b01b59eec249e998dd82c375b269fe10cff9d3",
"dest": "/tmp/hostname",
"gid": 1000,
"group": "oldboy",
"mode": "0666",
"owner": "oldboy",
"path": "/tmp/hostname",
"size": 7,
"state": "file",
"uid": 1000
}
[root@backup ~]# ll /tmp/hostname
-rw-rw-rw- 1 oldboy oldboy 7 Jul 23 23:53 /tmp/hostname

4.创建文件并直接写入内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@ssh-61 ~]# ansible webserver -m copy -a "content='oldboy' dest=/root/oldboy.txt"
172.16.1.41 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "da3a7ab3551120059810d0c7156a8150a0bc245a",
"dest": "/root/oldboy.txt",
"gid": 0,
"group": "root",
"md5sum": "890b185727556f1be31d7fe5ee5ce4dc",
"mode": "0644",
"owner": "root",
"size": 6,
"src": "/root/.ansible/tmp/ansible-tmp-1563897755.59-265719354438343/source",
"state": "file",
"uid": 0
}
[root@backup ~]# cat oldboy.txt
oldboy

5.复制目录

src后面目录没有/: 将目录本身以及目录下面的内容都进行远程传输复制

1
ansible webservser -m copy -a "src=/data dest=/data"

src后面目录有/: 只将目录下面的内容都进行远程传输复制

1
ansible webservser -m copy -a "src=/data/ dest=/data"

参数说明:

1
2
3
4
5
6
7
src #推送数据的源文件信息
dest #推送数据的目标路径
backup #对推送传输过去的文件,进行备份
content #直接批量在被管理端文件中添加内容
group #将本地文件推送到远端,指定文件属组信息
owner #将本地文件推送到远端,指定文件属主信息
mode #将本地文件推送到远端,指定文件权限信息

4.5 file 设置文件属性

4.5.1 创建文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@ssh-61 ~]# ansible webserver -m file -a "path=/root/oldboy state=directory"
172.16.1.31 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/root/oldboy",
"size": 6,
"state": "directory",
"uid": 0
}
[root@nfs ~]# ll -d oldboy
drwxr-xr-x 2 root root 6 Jul 24 00:10 oldboy

4.5.2 创建文件并更改属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@ssh-61 ~]# ansible webserver -m file -a "path=/tmp/oldboy.txt state=touch mode=666 owner=oldboy group=oldboy"
172.16.1.41 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/oldboy.txt",
"gid": 1000,
"group": "oldboy",
"mode": "0666",
"owner": "oldboy",
"size": 0,
"state": "file",
"uid": 1000
}
[root@backup ~]# ll /tmp/oldboy.txt
-rw-rw-rw- 1 oldboy oldboy 0 Jul 24 00:12 /tmp/oldboy.txt

4.5.3 创建软链接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@ssh-61 ~]# ansible webserver -m file -a "src=/root/oldboy.txt path=/root/oldboy.txt_link state=link"
172.16.1.41 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/root/oldboy.txt_link",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 16,
"src": "/root/oldboy.txt",
"state": "link",
"uid": 0
}
[root@backup ~]# ll
lrwxrwxrwx 1 root root 16 Jul 24 00:19 oldboy.txt_link -> /root/oldboy.txt

4.5.4 参数说明

1
2
3
4
5
6
7
8
9
10
path #指定远程主机目录或文件信息
recurse #递归授权
state
directory #在远端创建目录
touch #在远端创建文件
link #link 或 hard 表示创建链接文件
absent #表示删除文件或目录
mode #设置文件或目录权限
owner #设置文件或目录属主信息
group #设置文件或目录属组信息

4.6 script模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 编写脚本
[root@m01 ~]# mkdir -p /server/scripts
[root@m01 ~]# cat /server/scripts/echo.sh
#!/bin/bash
/usr/bin/echo "$(hostname)"
#在本地运行模块,等同于在远程执行,不需要将脚本文件进行推送目标主机执行
[root@ssh-61 ~]# ansible webserver -m script -a "/server/scripts/echo.sh"
172.16.1.41 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 172.16.1.41 closed.\r\n",
"stderr_lines": [
"Shared connection to 172.16.1.41 closed."
],
"stdout": "backup\r\n",
"stdout_lines": [
"backup"
]
}

4.7 cron定时任务模块

1.创建一条定时任务,添加定时任务时注释信息,防止重复,方便修改

1
2
3
4
5
[root@ssh-61 ~]# ansible webserver -m cron -a "name='主机名' job='/bin/bash /root/echo.sh >/dev/null 2>&1'"

[root@backup ~]# crontab -l
#Ansible: 主机名
* * * * * /bin/bash /root/echo.sh >/dev/null 2>&1

2.删除相应定时任务

1
[root@ssh-61 ~]# ansible webserver -m cron -a "name='主机名' state=absent"

3.注释相应定时任务,使定时任务失效

1
2
3
4
5
[root@ssh-61 ~]# ansible webserver -m cron -a "name='主机名' job='/bin/bash /root/echo.sh >/dev/null 2>&1' disabled=yes"

[root@backup ~]# crontab -l
#Ansible: 主机名
#* * * * * /bin/bash /root/echo.sh >/dev/null 2>&1

4.8 user 和group

创建用户组

1
[root@ssh-61 ~]# ansible webserver -m group -a "name=BBB gid=777 state=present"

创建用户,并属于组BBB

1
2
3
[root@ssh-61 ~]# ansible webserver -m user -a "name=BBB uid=777 group=777 shell=/sbin/nologin create_home=no"
[root@backup ~]# id BBB
uid=777(BBB) gid=777(BBB) groups=777(BBB)

user参数说明:

1
2
3
4
5
6
uid #指定用户的 uid
group #指定用户组名称
groups #指定附加组名称
password #给用户添加密码
shell #指定用户登录 shell
create_home #是否创建家目录

group参数说明:

1
2
3
4
5
name #指定创建的组名
gid #指定组的 gid
state
absent #移除远端主机的组
present #创建远端主机的组(默认)

4.9 yum

yum安装ntpdate服务

1
2
3
[root@ssh-61 ~]# ansible webserver -m yum -a "name=ntpdate state=installed"
[root@backup ~]# rpm -qa |grep ntpdate
ntpdate-4.2.6p5-28.el7.centos.x86_64

参数说明:

1
2
3
4
5
name       #指定要安装的软件包名称
state #指定使用 yum 的方法
installed, present #安装软件包
removed, absent #移除软件包
latest #安装最新软件包

4.10 service

启动或关闭服务

1
[root@ssh-61 ~]# ansible webserver -m  service -a "name=nfs state=restarted enabled=yes"

参数说明:

1
2
3
4
5
6
7
name # 定义要启动服务的名称
state # 指定服务状态
started #启动服务
stopped #停止服务
restarted #重启服务
reloaded #重载服务
enabled #开机自启

4.11 mount

1
2
3
4
5
6
7
8
ansible webservser -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults
state=present"
ansible webservser -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults
state=mounted"
ansible webservser -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults
state=unmounted"
ansible webservser -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults
state=absent"

参数说明:

1
2
3
4
present # 开机挂载,仅将挂载配置写入/etc/fstab
mounted # 挂载设备,并将配置写入/etc/fstab
unmounted # 卸载设备,不会清除/etc/fstab 写入的配置
absent # 卸载设备,会清理/etc/fstab 写入的配置

4.12 unarchive 解压

1.解压远程服务器的压缩包到指定目录

远程backup服务器创建压缩包:

1
2
3
4
[root@backup ~]# cd / && tar zcf /root/sys.tar.gz etc/fstab etc/hosts
[root@ssh-61 /]# cd
[root@ssh-61 ~]# ll
-rw-r--r-- 1 root root 561 Jul 24 01:24 sys.tar.gz

ansible本地执行命令

1
2
3
4
5
[root@ssh-61 ~]# ansible backup -m unarchive -a "src=/root/sys.tar.gz dest=/tmp/ remote_src=yes"
[root@backup /tmp/etc]# ll
total 8
-rw-r--r-- 1 root root 595 Jul 18 17:33 fstab
-rw-r--r-- 1 root root 423 Jul 16 04:43 hosts

2.把本地文件解压到目标机器指定目录
创建命令

1
[root@ssh-61 ~]# cd / && tar zcf /root/sys.tar.gz etc/fstab etc/hosts

执行命令

1
2
3
4
5
[root@ssh-61 ~]# ansible nfs -m unarchive -a "src=/root/sys.tar.gz dest=/backup/"
[root@nfs /backup]# tree etc/
etc/
├── fstab
└── hosts

4.13 archive 压缩

压缩单个文件

1
2
3
[root@ssh-61 ~]# ansible nfs -m archive -a "path=/root/1.txt dest=/tmp/1.tar.gz format=gz force_archive=true"
[root@nfs /tmp]# ll
-rw-r--r-- 1 root root 56 Jul 24 01:51 1.tar.gz

4.14 setup

1
[root@ssh-61 ~]# ansible webserver -m setup

第5章 查看帮助

1
2
ansible-doc -l  #查看所有模块说明信息
ansible-doc copy #查看copy模块信息

第6章 ansible输出信息颜色解释

1
2
3
4
5
01. 绿色信息:  查看主机信息/对主机未做改动
02. 黄色信息: 对主机数据信息做了修改
03. 红色信息: 命令执行出错了
04. 粉色信息: 忠告信息
05. 蓝色信息: 显示ansible命令执行的过程
文章作者: Wu Fei
文章链接: http://linuxwf.com/2020/04/13/6-ansible%E6%9C%8D%E5%8A%A1/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 WF's Blog
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论