2.安装Ansible
Ansible仓库默认不在yum仓库中,因此需要使用下面的命令启用epel仓库。
# 拉取epel扩展源
[root@server ~]# yum -y install epel-release
安装epel-release源后,安装Ansible,并检查是否安装成功
# 安装Ansible
[root@server ~]# yum -y install ansible
# 查看Ansible版本
[root@server ~]# ansible –version
3.配置免密登入(可使用脚本批量生成密钥发送)
其实就是配置从管理节点到远程主机之间基于key(无密码的方式)的SSH连接
(1).生成密钥对
在server节点上生成密钥对
[root@server ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:G/Ezen/e8puqFtJPG5jrUeBoQ4iCyUtR52MmYftRm6k root@server
The key's randomart image is:
+—[RSA 2048]—-+
| ..+ . . |
|. = = o = |
| = + B =.. . |
|. . * + .oo . |
| . E S+=.o. |
| .=.B.o |
| o o.= o |
| . +.oo..|
| ooo+o=+|
+—-[SHA256]—–+
(2).将公钥发给管制节点
三个受管制的节点依次发送
[root@server ~]# ssh-copy-id 192.168.77.172
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/.ssh/id_rsa.pub”
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed — if you are prompted now it is to install the new keys
root@192.168.77.172's password: #这里输入host1节点的密码
Number of key(s) added: 1
Now try logging into the machine, with: “ssh '192.168.77.172'”
and check to make sure that only the key(s) you wanted were added.
(1).备份主机清单文件
我们先备份一下主机清单文件,然后清空hosts文件
[root@server ~]# cd /etc/ansible/
[root@server ansible]# ls
ansible.cfg hosts roles
[root@server ansible]# cp -f hosts hosts.bak
[root@server ansible]# ls
ansible.cfg hosts hosts.bak roles
(2).单主机配置
# IP + 用户密码
[root@server ~]# vi /etc/ansible/hosts
[host01]
192.168.77.172 ansible_ssh_pass='root'
[host02]
192.168.77.173 ansible_ssh_pass='root'
#设置ansible远程主机组清单,远程端口,用户名,密码等
[web]
172.16.1.7 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123123
172.16.1.8 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123123
172.16.1.9 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123123
172.16.1.10 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123123
172.16.1.11 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123123
# 测试:ansible 主机名 -m 指定模块
[root@server ~]# ansible host01 -m ping
192.168.77.172 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}
(3).多主机配置
# IP + 密码
[root@server ~]# vi /etc/ansible/hosts
[host]
192.168.77.172 ansible_ssh_pass=’root’
192.168.77.173 ansible_ssh_pass=’root’
# 测试:ansible 主机名 -m 指定模块
[root@server ~]# ansible host -m ping
192.168.77.172 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}
192.168.77.173 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}
(4).主机组
对组定义变量 [组名:vars]
# 主机组变量 + 主机 + 密码
[root@server ~]# vi /etc/ansible/hosts
[host_group]
192.168.77.172
192.168.77.173
[host_group:vars]
ansible_ssh_pass='root'
# 测试:ansible 主机名 -m 指定模块、
ansible web -m command -a 'hostname'
命令 定义的主机组 制定模块 模块名字 制定执行动作 执行命令
ansible test -m command -a hostname
ansible test -a hostname
[root@server ~]# ansible host_group -m ping
192.168.77.172 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}
192.168.77.173 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}
1.Ansible参数
(1).Ansible参数
ansible 主机名 -m “模块” -a “具体命令”
# 常用参数
-m #指定要使用的模块名称,不指定默认使用command模块
-a #为指定的模块提供参数。不同的模块有不同的参数需求。
–syntax-check #验证语法
# 不常用参数
–version #ansible版本信息
-v #显示详细信息
-i #指定主机清单文件的位置,默认是在/etc/ansible/hosts
-k #提示输入ssh密码,而不使用基于ssh的密钥验证
-C #模拟执行测试,不会真的改变系统状态
-T #设置远程命令执行的最大超时时间,默认值是10秒
-f #控制Ansible同时并行处理多少台主机,默认情况下是5台
(2).Ansible帮助命令
ansible-doc #帮助命令
ansible-doc -l #列出所有的模块
ansible-doc 模块名 #查看模块的详细信息
ansible-doc 模块名 -s #查看模块的选项使用说明
2.ad-hoc模式执行流程
模块执行的工作流程:
主控端命令行执行命令
将模块拷贝到受控端
按照模块定义的操作在受控端执行
返回信息,删除受控端模块
Ansible执行命令的返回值颜色
绿色:执行命令成功,只查看了信息,没有修改
黄色:执行命令成功,并且对目标主机做了修改
红色:执行失败,报错
粉色:警告信息
紫色:表示对命令发错的操作有警告信息
3.Ansible中常见的返回值
返回值
返回值含义
changed
几乎所有的Ansible模块都会返回该变量,表示模块是否对远程主机执行了修改操作
Failed
如果模块未能执行完成,将返回failed为true
Msg
模块执行失败的原因,常见的错误如ssh连接失败
Rc
与命令行工具相关的模块会返回rc,表示执行Linux命令的状态码
stdout
与rc类似,返回的是标准输出的结果
stderr
与rc类似,返回的是标准错误的结果
backup_file
所有存在backup选项的模块,用来返回备份文件的路径
results
应用在playbook中存在循环的情况,返回多个结果
(1).ping模块
ping模块:用于检查指定节点机器是否连通,用法很简单
语法:
anshible 主机名 -m ping
案例:
# 查看是否能够ping通host1节点
[root@server ~]# ansible host1 -m ping
(2).command模块
command模块:用于在远程主机上执行命令,不支持管道
语法:
ansible 主机名 -m command -a “具体命令”
ansible 主机名 -a “具体命令”
# ansible默认就是使用command模块。
案例:
# 在受管制主机host1的/opt/目录下创建文件
[root@server ~]# ansible host1 -m command -a 'touch /opt/qzj'
(3).shell模块
shell模块:用于远程执行命令也可以执行受控机上的脚本,支持管道和重定向
语法:
ansible 主机名 -m shell -a “具体命令”
案例:
# 远程执行脚本
# 首先在host1节点上创建一个shell脚本1.sh,内容为echo `date`
# 赋予执行权限
chmod +x 1.sh
# 然后在server节点上执行该脚本
[root@server ~]# ansible host1 -m shell -a '/root/1.sh'
#执行cd /etc/ansible/host下配置的主机批量执行脚本
主机组 主机组下脚本
ansible host01 -m shell -a '/root/getcheck.sh'
# 查看host_group组远程主机的系统版本
[root@server ~]# ansible host_group -m shell -a 'cat /etc/os-release | grep PRETTY_NAME | cut -d “=” -f2'
(4).raw模块
raw模块:ssh登入,再执行,用于在远程主机上直接执行任意命令
语法:
ansible 主机名 -m raw -a “具体命令”
案例:
# 查看host组的root目录下
[root@server ~]# ansible host_group -m raw -a “ls /root/”
(5).script模块
Script模块用与远程执行本地脚本
语法:
ansible 主机名 -m script -a “脚本文件的本地路径”
案例:
# 在host组上远程执行脚本
# 我们先在server节点上创建一个脚本2.sh,内容为echo “测试”
# 赋予执行权限
[root@server ~]# ansible host_group -m script -a '/root/2.sh'
请注意,script模块会将脚本文件传输到目标主机,然后在目标主机上执行它。
因此,确保脚本文件在控制节点上可用,并且具有可执行权限(使用chmod +x命令添加执行权限)。
(6).yum模块
Yum模块:用于远程安装软件
语法:
ansible 主机名 -m yum -a “name=软件包名 state=安装/卸载/更新”
name=软件包名,多个软件包用逗号隔开
state=installed/present安装、removed/absent卸载、lastest更新
yum模块的更多使用可以使用ansible-doc yum来查看
案例:
# 我们在host1节点上安装一个nginx服务和netstat命令
[root@server ~]# ansible host1 -m yum -a 'name=nginx,net-tools state=installed'
(7).copy模块
copy模块:主要是将本地(执行 Ansible 任务的控制节点)的文件或目录复制到远程的host主机(被管理节点)。
语法:
ansible 主机名 -m copy -a “src=源文件路径及文件 dest=目标文件路径”
常用参数:
src=源文件路径
dest=目标文件路径
contest=指定文件内容,只有目标文件;如果文件不存在会创建
owner=指定属主
group=指定属组
mode=指定权限
copy模块的更多使用可以使用ansible-doc copy来查看
案例:
# 将server节点上的2.sh文件复制到host1上
[root@server ~]# ansible host1 -m copy -a 'src=/root/2.sh dest=/root/'
#将本机/mnt/目录下所有文件拷贝到主机组/root目录下
ansible test -m copy -a “src=/mnt/ dest=/root”
(8).file模块
file模块:主要用于设置远程主机上的文件、软链接和文件夹的权限,也可以用来创建和删除他们。
语法:
ansible 主机名 -m file -a “操作内容”
常用参数:
path:指定路径
src:源文件路径
recurse:递归授权
owner:指定属主
group:指定属组
mode:指定权限
state:指定文件的状态
directory:在远端创建目录
touch:在远端创建文件
link:创建软连接
hard:创建硬连接
absent:表示删除文件或目录
案例:
# 在host1上创建一个目录和一个文件
[root@server ~]# ansible host1 -m file -a 'path=/opt/123 state=directory'
[root@server ~]# ansible host1 -m file -a 'path=/opt/1.txt state=touch'
(9).service模块
service模块:用于管理系统服务(如启动、停止、重启服务等)。
语法:
ansible 主机名 -m service -a “name=服务名称 state=服务操作 enabled=是否设置开机自启”
name:定义要启动服务的名称
state:指定服务状态
常用参数:
started:启动服务(幂等)
stopped:停止服务(幂等)
restarted:重启服务
reloaded:重载配置
enabled:开机自启
案例:
# 启动host1上的nginx服务并设置开机自启
[root@server ~]# ansible host1 -m service -a 'name=nginx state=started enabled=yes'
# 查看host1节点的nginx状态
[root@server ~]# ansible host1 -m shell -a 'systemctl status nginx'
# 关闭nginx服务
[root@server ~]# ansible host1 -m service -a 'name=nginx state=stopped'
# 查看host1节点的nginx状态
[root@server ~]# ansible host1 -m shell -a 'systemctl status nginx'
(10).cron模块
cron模块:用于设置定时任务
语法:
ansible 主机名 -m cron -a “操作内容”
常用参数:
name:注释说明
minute、hour、day、month、weekday(分、时、日、月、周)
user:指定用户
job:操作的指令
state:进行的操作
present:创建
absent:删除
案例:
# 在host1节点上设置一个定时脚本然后删除掉
# 设置定时执行统计脚本(每一个小时执行一次,使用root用户)
[root@server ~]# ansible host1 -m cron -a “name=”每小时执行一次统计脚本” minute=0 hour=*/1 day=* month=* weekday=* user=root job='/bin/bash /root/3.sh' state=present”
# 查看定时任务
[root@server ~]# ansible host1 -m shell -a 'crontab -l'
# 删除指定的定时任务
[root@server ~]# ansible host1 -m cron -a “name=”每小时执行一次统计脚本” minute=0 hour=*/1 day=* month=* weekday=* user=root job='/bin/bash /root/3.sh' state=absent”
#查看定时任务
[root@server ~]# ansible host1 -m shell -a 'crontab -l'
(11).mount模块
mount模块:用于管理设备挂载与卸载
语法:
ansible 主机名 -m mount -a “操作内容”
常用参数:
src:指定挂载源
path:指定挂载点 (挂载点不存在会自动创建)
fstype:指定文件系统类型
opts:挂载参数,默认不写为:defaults
dump:是否备份:0表示不进行备份,默认为0
passno:文件系统检测:0表示不进行文件系统检测,默认为0
state:操作
present:写入fstab,但实际没有挂载,需要重启服务器
absent:取消临时挂载,并且删除fstab
mounted:写入fstab,并且直接挂载了(常用)
unmounted:临时取消挂载,但是没有删除fstab,重启服务器之后就会恢复(常用)
案例:
# 临时取消挂载在data的/dev/sdb(常用),不会删除fstab
[root@server ~]# ansible host1 -m mount -a “src=/dev/sdb path=/data/ state=unmounted”
# 取消挂载在data的/dev/sdb,并且直接删除fstab中的单独配置
[root@server ~]# ansible host1 -m mount -a “src=/dev/sdb path=/data/ state=absent”
# 添加一个挂载写入fstab,但实际没有挂载,需要重启服务器,重启完就会自动挂载上
[root@server ~]# ansible host1 -m mount -a “src=/dev/sdb path=/data fstype=xfs opts=defaults dump=0 passno=0 state=present”
# 添加一个挂载写入fstab,并且直接挂载上(常用)
[root@server ~]# ansible host1 -m mount -a “src=/dev/sdb path=/data fstype=xfs state=mounted”
(12).user/group模块
user模块:用于管理远程系统上的用户账户,包括创建、修改和删除用户。
group模块:用于在受控机上添加或删除组。
语法:
ansible 主机名 -m user -a “操作内容”
ansible 主机名 -m group -a “操作内容”
常用参数:
name:指定用户名
system:如果为 yes,则创建用户时设置用户为系统用户,默认为 no
uid:指定用户的用户UID
gid:指定用户的组GID
group:指定用户所属组
groups:指定用户所属的其他组
home:指定用户的家目录
shell:指定用户的登录shell
password:指定用户的密码(已加密的密码)
append:如果为yes,则添加用户到组而不是替换,默认为no
remove:当 state 为 absent 时,是否删除用户的家目录和邮件别名,默认为 no
move_home:如果为yes,则在更改用户的主目录时移动用户的文件,默认为no
create_home:如果为yes,则创建用户的主目录,默认为yes
update_password:如果为always,则始终更新密码,默认为on_create
state:指定用户账户的状态,可以是以下之一:
present:创建用户或组
absent:删除用户或组
locked:锁定用户账户
unlocked:解锁用户账户
password:仅更改用户的密码
案例:
# 创建用户,使用user模块创建一个名为qiuzj的新用户
[root@server ~]# ansible host1 -m user -a “name=qiuzj state=present”
# 查看
[root@server ~]# ansible host1 -m shell -a “tail -1 /etc/passwd”
# 删除用户和home家目录,使用 user 模块删除一个名为qiuzj的用户
[root@server ~]# ansible host1 -m user -a “name=qiuzj remove=yes state=absent”
# 查看
[root@server ~]# ansible host1 -m shell -a “tail -1 /etc/passwd”
# 创建组,使用 group 模块创建一个名为 mygroup 的新组:
[root@server ~]# ansible host1 -m group -a “name=mygroup state=present”
# 查看
[root@server ~]# ansible host1 -m shell -a “tail -1 /etc/group”
# 删除组,使用 group 模块删除一个名为 mygroup 的组:
[root@server ~]# ansible host1 -m group -a “name=mygroup state=absent”
# 查看
[root@server ~]# ansible host1 -m shell -a “tail -1 /etc/group”
# 再将上面的用户和用户组在创建一遍;
[root@server ~]# ansible host1 -m user -a “name=qiuzj state=present”
[root@server ~]# ansible host1 -m group -a “name=mygroup state=present”
# 添加用户到组,使用 user 模块将用户qiuzj添加到组mygroup:
[root@server ~]# ansible host1 -m user -a “name=qiuzj group=mygroup append=yes”
# 查看
[root@server ~]# ansible host1 -m shell -a “groups qiuzj”
(13).unarchive模块
unarchive模块:用于解压归档文件,它支持多种常见的归档格式,如.tar、.tar.gz、.zip等。
unarchive模块,这个模块有两种用法:
1、将ansible主机上的压缩包在本地解压缩后传到远程主机上,这种情况下,copy=yes.本地解压缩,解压缩位置不是默认的目录,没找到或传完删了后传到远程主机
2、将远程主机上的某个压缩包解压缩到指定路径下。这种情况下,需要设置copy=no远程主机上面的操作,不涉及ansible服务端
语法:
ansible 主机名 -m unarchive -a “src=源路径 dest=目标路径 其他参数”
常用参数:
copy:默认为no,当copy=yes,那么拷贝的文件是从ansible主机复制到远程主机上而不是在原地解压缩,如果设置为copy=no,那么会在远程主机上寻找src源文件
src:源路径,可以是ansible主机上的路径,也可以是远程主机上的路径,如果是远程主机上的路径,则需要设置copy=no
dest:指定了解压缩后文件的目标目录
mode:设置解压缩后的文件权限,可以使用数字或符号模式,例如 “0644” 或 “u=rw,go=r”
list_files:默认值为no,如果设置为yes,解压同时,返回压缩包的文件列表
remote_src:如果设置为 yes,则表示 src 参数是远程主机上的路径
extra_opts: 允许您指定解压缩命令的额外选项。这对于一些特定格式的存档文件非常有用
creates: 如果指定了此选项,只有在 creates 中指定的文件或目录不存在时,才会执行解压缩操作
owner: 设置解压缩后文件的所有者
group: 设置解压缩后文件的所属组
案例:
# 将jdk-8u271-linux-x64.tar.gz复制到server节点下的root目录下并解压
[root@server ~]# ansible host1 -m unarchive -a “src=/root/jdk-8u271-linux-x64.tar.gz dest=/root”
# 查看
[root@server ~]# ansible host1 -m shell -a “ls”