ansible与awk
·
awk 示例
示例1:获取IP地址是10.1.8.10的网卡名
[root@centos7 ~ 10:24:46]# ip addr | grep '10.1.8.10' | awk '{print $NF}'
ens33
[root@centos7 ~ 10:25:15]# ip addr | awk '/10.1.8.10/ {print $NF}'
ens33
[root@centos7 ~ 10:26:50]# df -h | sed 's/%//' | awk '$5>10 {print $0}'
Filesystem Size Used Avail Use Mounted on
/dev/sda1 1014M 170M 845M 17 /boot
示例2:查看使用率超过10的文件系统
[root@shell ~ 10:24:04]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 2.0G 12M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/mapper/centos-root 50G 1.6G 49G 4% /
/dev/sda1 1014M 139M 876M 14% /boot
/dev/mapper/centos-home 147G 33M 147G 1% /home
tmpfs 394M 0 394M 0% /run/user/0
tmpfs 394M 0 394M 0% /run/user/1000
[root@shell ~ 10:24:06]# df -h | sed 's/%//' | awk '$5>10 {print $0}'
Filesystem Size Used Avail Use Mounted on
/dev/sda1 1014M 139M 876M 14 /boot
# 或者
[root@shell ~ 10:24:10]# df -h | awk '$5+1-1>10 {print $0}'
/dev/sda1 1014M 139M 876M 14% /boot
示例3:提前系统运行数据。
包括:
- CPU 使用率
- 内存 使用率
- 当前用户登录数
- 当前系统负载
- 系统运行进程数量
[root@shell bin 10:43:30]# pwd
/root/bin
[root@shell bin 10:43:33]# vim monitor_os.sh
#!/bin/bash
os_info_file=/tmp/os_info.txt
top -b -n 1 | head -n 5 > ${os_info_file}
cpu_usage=$(awk '/^%Cpu/ {print $2+$4}' ${os_info_file})
echo "CPU 使用率:${cpu_usage}"
memory_usage=$(awk '/^KiB Mem/ {print ($4-$6)/$4}' ${os_info_file})
echo "内存使用率:${memory_usage}"
user_count=$(awk '/^top/ {print $6}' ${os_info_file})
echo "在线用户数量:${user_count}"
load_usage=$(awk '/^top/ {print $(NF-2),$(NF-1),$NF}' ${os_info_file})
echo "当前系统负载:${load_usage}"
running_process_count=$(awk '/^Tasks/ {print $4}' ${os_info_file})
echo "系统运行进程数量:${running_process_count}"
rm -f ${os_info_file}
[root@centos7 bin 10:56:33]# vim monitor_os .sh
[root@centos7 bin 10:59:26]# bash monitor_os .sh
CPU 使用率:6.2%
内存使用率:11.71%
当前用户登录数:2
当前系统负载: 0.00, 0.03, 0.08
系统运行进程数量:144echo 系统运行进程数量:144
思考题
统计一篇文章中出现频率最高的10个单词,并按照数量降序排序。
ansible 环境准备
ansible 架构
- 控制节点:下发指令或文件到受控制节点。
- 受控制节点:接受控制节点发过来的指令,并执行。
ansible 配置
ansible 环境准备
ansible 架构
- 控制节点:下发指令或文件到受控制节点。
- 受控制节点:接受控制节点发过来的指令,并执行。
ansible 环境准备
准备虚拟机模版
准备5台Linux系统,并设置主机名和IP地址。
注意:模版虚拟机的CPU和内存的配置,建议设置为1CPU和2G内存。
开发脚本sethost:
- 不加参数执行sethost,则提示命令使用方法。
- 加参数执行sethost,则第一个参数范围是10-14。超出范围也提示命令使用方法。
- 正常执行示例:sethost 10,这设置正确的主机名和IP地址。
[root@shell ~ 13:34:31]# vim /usr/local/bin/sethost
#!/bin/bash
# test root user
(( UID!=0 )) && echo 'Please run as root.' && exit 1
usage="Usage: $0 10-14"
# test args number
(($# !=1 )) && echo $usage && exit 2
con_name=ens33
domain_name=my.cloud
host_id=$1
if ((host_id==10));then
HOSTNAME=controller.${domain_name}
elif ((11<=host_id<<14)) ;then
HOSTNAME=server$[host_id-10].${domain_name}
else
echo $usage
fi
hostnamectl set-hostname $HOSTNAME
nmcli connection modify ${con_name} ipv4.addresses 10.1.8.${host_id}/24
nmcli connection up ${con_name}
hostname
ip -br address
[root@shell ~ 13:34:31]# chmod +x /usr/local/bin/sethost
克隆虚拟机
根据模版虚拟机克隆出其他虚拟机,并使用sethost脚本设置主机名和IP地址。
以server1为例:
[root@shell ~ 13:34:31]# sethost 11
配置 ansible 基础环境
10.1.8.10 controller.my.cloud controller
10.1.8.11 server1.my.cloud server1
10.1.8.12 server2.my.cloud server2
10.1.8.13 server3.my.cloud server3
10.1.8.14 server4.my.cloud server4
使用以下命令生成主机清单:
for i in {0..4}
do
if ((i==0));then
echo "10.1.8.1$i controller.my.cloud controller"
else
echo "10.1.8.1$i server$i.my.cloud server$i"
fi
done
配置 ansible 基础环境
在模版虚拟机上配置/etc/hosts,添加ansible主机清单
[root@deploy ~ 14:51:11]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
################# ansible ####################
10.1.8.10 controller.laoma.cloud controller
10.1.8.11 server1.laoma.cloud server1
10.1.8.12 server2.laoma.cloud server2
10.1.8.13 server3.laoma.cloud server3
10.1.8.14 server4.laoma.cloud server4
################# ansible ####################
配置免密登录ansible节点:
[root@deploy ~ 14:52:58]# ssh-keygen
[root@deploy ~ 14:54:39]# \
for host in controller server1 server2 server3 server4
do
sshpass -p123 ssh-copy-id root@$host
done
[root@deploy ~ 14:56:02]# for host in controller server1 server2 server3 server4; do ssh $host 'hostname;ip -br a show ens33;echo'; done
controller.laoma.cloud
ens33 UP 10.1.8.10/24 fe80::20c:29ff:fe14:2f8b/64
server1.laoma.cloud
ens33 UP 10.1.8.11/24 fe80::20c:29ff:fe6a:559b/64
server2.laoma.cloud
ens33 UP 10.1.8.12/24 fe80::20c:29ff:feec:ac41/64
server3.laoma.cloud
ens33 UP 10.1.8.13/24 fe80::20c:29ff:fe19:59c1/64
server4.laoma.cloud
ens33 UP 10.1.8.14/24 fe80::20c:29ff:feaa:4763/64
在模版虚拟机上开发脚本weihu,用来集中管理其他机器。
- weihu cmd command,将会在ansible 5台设备上执行command。
- weihu copy src dest,将模版虚拟机上的src文件复制到ansible 5台设备dest位置。
[root@deploy ~ 14:49:42]# vim /usr/local/bin/weihu
#!/bin/bash
function usage () {
echo "Usage: weihu cmd COMMAND, 在集群中所有的机器上执行对应COMMAND命令"
echo "Usage: weihu copy source target,将本地source文件,推送到集群中所有的机器上"
exit
}
action=$1
HOSTLIST='controller server1 server2 server3 server4'
(( $#<=1 )) && usage
case "$action" in
"cmd")
# 删除参数1
shift
COMMAND="$*"
for host in $HOSTLIST
do
ssh root@$host "$COMMAND"
done
;;
"copy")
# 删除参数1
shift
for host in $HOSTLIST
do
num=$#
case $num in
2)
scp -r $1 root@$host:$2
;;
#[3-9]|[1-9][0-9])
[3-9])
last=$(echo $* | awk '{print $NF}')
args_exclude_last=$(echo $* | sed "s#$last##")
scp -r ${args_exclude_last} root@$host:${last}
;;
*)
usage
esac
done
;;
*)
usage
;;
esac
[root@deploy ~ 14:50:49]# chmod +x /usr/local/bin/weihu
在模版虚拟机上开发脚本weihu,用来集中管理其他机器。
- weihu cmd command,将会在ansible 5台设备上执行command。
- weihu copy src dest,将模版虚拟机上的src文件复制到ansible 5台设备dest位置。
[root@deploy ~ 14:49:42]# vim /usr/local/bin/weihu
#!/bin/bash
function usage () {
echo "Usage: weihu cmd COMMAND, 在集群中所有的机器上执行对应COMMAND命令"
echo "Usage: weihu copy source target,将本地source文件,推送到集群中所有的机器上"
exit
}
action=$1
HOSTLIST='controller server1 server2 server3 server4'
(( $#<=1 )) && usage
case "$action" in
"cmd")
# 删除参数1
shift
COMMAND="$*"
for host in $HOSTLIST
do
ssh root@$host "$COMMAND"
done
;;
"copy")
# 删除参数1
shift
for host in $HOSTLIST
do
num=$#
case $num in
2)
scp -r $1 root@$host:$2
;;
#[3-9]|[1-9][0-9])
[3-9])
last=$(echo $* | awk '{print $NF}')
args_exclude_last=$(echo $* | sed "s#$last##")
scp -r ${args_exclude_last} root@$host:${last}
;;
*)
usage
esac
done
;;
*)
usage
;;
esac
在模版虚拟机上配置/etc/hosts,添加ansible主机清单
配置免密登录ansible节点:
测试维护脚本
[root@deploy ~ 15:08:29]# vim /etc/hosts
[root@deploy ~ 15:10:02]# vim /etc/hosts
[root@deploy ~ 15:10:46]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:BlvC4MdtZqOi77bcvV0FV1h2sUnEpVVufs2tU/B2xDI root@deploy
The key's randomart image is:
+---[RSA 2048]----+
| . o*%|
| . + . oO+|
| . * B . Eo=|
| . X . o Xo|
| . o S . @|
| . . . . +o|
| . . o |
| o.. . . . . |
| o=.. o.. |
+----[SHA256]-----+
[root@deploy ~ 15:11:23]# \
> for host in controller server1 server2 server3 server4
> do
> sshpass -p123 ssh-copy-id root@$host
> done
/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
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@controller'"
and check to make sure that only the key(s) you wanted were added.
/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
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@server1'"
and check to make sure that only the key(s) you wanted were added.
/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
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@server2'"
and check to make sure that only the key(s) you wanted were added.
/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
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@server3'"
and check to make sure that only the key(s) you wanted were added.
/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
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@server4'"
and check to make sure that only the key(s) you wanted were added.
[root@deploy ~ 15:11:50]# for host in controller server1 server2 server3 server4; do ssh $host 'hostname;ip -br a show ens33;echo'; done
controller.my.cloud
ens33 UP 10.1.8.10/24 fe80::20c:29ff:fea1:ff93/64
server1.my.cloud
ens33 UP 10.1.8.11/24 fe80::20c:29ff:fefc:9917/64
server2.my.cloud
ens33 UP 10.1.8.12/24 fe80::20c:29ff:fed5:3c63/64
server3.my.cloud
ens33 UP 10.1.8.13/24 fe80::20c:29ff:fe12:b17f/64
server4.my.cloud
ens33 UP 10.1.8.14/24 fe80::20c:29ff:fe68:91d0/64
[root@deploy ~ 15:11:59]# vim /usr/local/bin/weihu
[root@deploy ~ 15:13:12]# chmod +x /usr/local/bin/weihu
[root@deploy ~ 15:13:26]# weihu cmd hostname
controller.my.cloud
server1.my.cloud
server2.my.cloud
server3.my.cloud
server4.my.cloud
[root@deploy ~ 15:13:46]# weihu copy /etc/hosts /etc/hosts
hosts 100% 433 131.3KB/s 00:00
hosts 100% 433 664.0KB/s 00:00
hosts 100% 433 594.4KB/s 00:00
hosts 100% 433 335.2KB/s 00:00
hosts 100% 433 250.0KB/s 00:00
[root@deploy ~ 15:13:53]# weihu cmd cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
################# ansible ####################
10.1.8.10 controller.my.cloud controller
10.1.8.11 server1.my.cloud server1
10.1.8.12 server2.my.cloud server2
10.1.8.13 server3.my.cloud server3
10.1.8.14 server4.my.cloud server4
################# ansible ####################
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
################# ansible ####################
10.1.8.10 controller.my.cloud controller
10.1.8.11 server1.my.cloud server1
10.1.8.12 server2.my.cloud server2
10.1.8.13 server3.my.cloud server3
10.1.8.14 server4.my.cloud server4
################# ansible ####################
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
################# ansible ####################
10.1.8.10 controller.my.cloud controller
10.1.8.11 server1.my.cloud server1
10.1.8.12 server2.my.cloud server2
10.1.8.13 server3.my.cloud server3
10.1.8.14 server4.my.cloud server4
################# ansible ####################
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
################# ansible ####################
10.1.8.10 controller.my.cloud controller
10.1.8.11 server1.my.cloud server1
10.1.8.12 server2.my.cloud server2
10.1.8.13 server3.my.cloud server3
10.1.8.14 server4.my.cloud server4
################# ansible ####################
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
################# ansible ####################
10.1.8.10 controller.my.cloud controller
10.1.8.11 server1.my.cloud server1
10.1.8.12 server2.my.cloud server2
10.1.8.13 server3.my.cloud server3
10.1.8.14 server4.my.cloud server4
################# ansible ####################
准备一个专用的账户devops,用于控制节点远程登录其他节点。
[root@deploy ~ 15:14:00]# weihu cmd useradd devops
[root@deploy ~ 15:53:11]# weihu cmd id devops
uid=1001(devops) gid=1001(devops) 组=1001(devops)
uid=1001(devops) gid=1001(devops) 组=1001(devops)
uid=1001(devops) gid=1001(devops) 组=1001(devops)
uid=1001(devops) gid=1001(devops) 组=1001(devops)
uid=1001(devops) gid=1001(devops) 组=1001(devops)
[root@deploy ~ 15:53:35]# weihu cmd 'echo 123 | passwd --stdin devops'
更改用户 devops 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 devops 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 devops 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 devops 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 devops 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@deploy ~ 15:53:52]# sshpass -p 123 ssh devops@server1 id
uid=1001(devops) gid=1001(devops) 组=1001(devops)
[root@deploy ~ 15:54:01]# weihu cmd "echo 'devops ALL=(ALL)NOPASSWD: ALL' > /etc/sudoers.d/devops"
[root@deploy ~ 15:55:20]# sshpass -p123 ssh devops@controller 'ssh-keygen -t rsa -N "" -f .ssh/id_rsa'
Generating public/private rsa key pair.
Saving key ".ssh/id_rsa" failed: No such file or directory
[root@deploy ~ 15:55:42]# sshpass -p123456 ssh devops@controller 'mkdir -p ~/.ssh && chmod 700 ~/.ssh && ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa'
Permission denied, please try again.
[root@deploy ~ 15:57:07]# sshpass -p123 ssh devops@controller 'mkdir -p ~/.ssh && chmod 700 ~/.ssh && ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa'
Generating public/private rsa key pair.
Your identification has been saved in /home/devops/.ssh/id_rsa.
Your public key has been saved in /home/devops/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:2eSivIHuuC2kAzBH09/oCSXFOFuVCFXbT5cBdsDKzQo devops@controller.my.cloud
The key's randomart image is:
+---[RSA 2048]----+
| ..*o+o. .+oo |
| o = = .o ... o |
| . . B o..o+. o |
|o . o o E=ooo. |
|.o o .S.o.. |
|. . oo. .. |
|.o . + |
|o .+ o |
| .o++ . |
+----[SHA256]-----+
[root@deploy ~ 15:57:23]# sshpass -p123 ssh devops@controller 'for host in controller server1 server2 server3 server4;do sshpass -p123 ssh-copy-id devops@$host;done'
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/devops/.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
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'devops@controller'"
and check to make sure that only the key(s) you wanted were added.
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/devops/.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
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'devops@server1'"
and check to make sure that only the key(s) you wanted were added.
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/devops/.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
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'devops@server2'"
and check to make sure that only the key(s) you wanted were added.
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/devops/.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
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'devops@server3'"
and check to make sure that only the key(s) you wanted were added.
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/devops/.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
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'devops@server4'"
and check to make sure that only the key(s) you wanted were added.
配置控制节点使用devops账户免密登录所有ansible节点。
[devops@controller my 16:02:20]$ for host in controller server1 server2 server3 server4;do ssh devops@$host id;done
uid=1001(devops) gid=1001(devops) 组=1001(devops)
uid=1001(devops) gid=1001(devops) 组=1001(devops)
uid=1001(devops) gid=1001(devops) 组=1001(devops)
uid=1001(devops) gid=1001(devops) 组=1001(devops)
uid=1001(devops) gid=1001(devops) 组=1001(devops)
[devops@controller my 16:02:34]$ for host in controller server1 server2 server3 server4;do ssh devops@$host sudo id;done
uid=0(root) gid=0(root) 组=0(root)
uid=0(root) gid=0(root) 组=0(root)
uid=0(root) gid=0(root) 组=0(root)
uid=0(root) gid=0(root) 组=0(root)
uid=0(root) gid=0(root) 组=0(root)
ansible配置
[devops@controller my 16:02:45]$ sudo yum install -y ansible
已加载插件:fastestmirror
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00
epel | 4.3 kB 00:00
extras | 2.9 kB 00:00
updates | 2.9 kB 00:00
正在解决依赖关系
--> 正在检查事务
---> 软件包 ansible.noarch.0.2.9.27-1.el7 将被 安装
--> 正在处理依赖关系 PyYAML,它被软件包 ansible-2.9.27-1.el7.noarch 需要
--> 正在处理依赖关系 python-httplib2,它被软件包 ansible-2.9.27-1.el7.noarch 需要
--> 正在处理依赖关系 python-jinja2,它被软件包 ansible-2.9.27-1.el7.noarch 需要
--> 正在处理依赖关系 python-paramiko,它被软件包 ansible-2.9.27-1.el7.noarch 需要
--> 正在处理依赖关系 python-setuptools,它被软件包 ansible-2.9.27-1.el7.noarch 需要
--> 正在处理依赖关系 python-six,它被软件包 ansible-2.9.27-1.el7.noarch 需要
--> 正在处理依赖关系 python2-cryptography,它被软件包 ansible-2.9.27-1.el7.noarch 需要
--> 正在处理依赖关系 python2-jmespath,它被软件包 ansible-2.9.27-1.el7.noarch 需要
--> 正在检查事务
---> 软件包 PyYAML.x86_64.0.3.10-11.el7 将被 安装
--> 正在处理依赖关系 libyaml-0.so.2()(64bit),它被软件包 PyYAML-3.10-11.el7.x86_64 需要
---> 软件包 python-jinja2.noarch.0.2.7.2-4.el7 将被 安装
--> 正在处理依赖关系 python-babel >= 0.8,它被软件包 python-jinja2-2.7.2-4.el7.noarch 需要
--> 正在处理依赖关系 python-markupsafe,它被软件包 python-jinja2-2.7.2-4.el7.noarch 需要
---> 软件包 python-paramiko.noarch.0.2.1.1-9.el7 将被 安装
--> 正在处理依赖关系 python2-pyasn1,它被软件包 python-paramiko-2.1.1-9.el7.noarch 需要
---> 软件包 python-setuptools.noarch.0.0.9.8-7.el7 将被 安装
--> 正在处理依赖关系 python-backports-ssl_match_hostname,它被软件包 python-setuptools-0.9.8-7.el7.noarch 需要
---> 软件包 python-six.noarch.0.1.9.0-2.el7 将被 安装
---> 软件包 python2-cryptography.x86_64.0.1.7.2-2.el7 将被 安装
--> 正在处理依赖关系 python-idna >= 2.0,它被软件包 python2-cryptography-1.7.2-2.el7.x86_64 需要
--> 正在处理依赖关系 python-cffi >= 1.4.1,它被软件包 python2-cryptography-1.7.2-2.el7.x86_64 需要
--> 正在处理依赖关系 python-ipaddress,它被软件包 python2-cryptography-1.7.2-2.el7.x86_64 需要
--> 正在处理依赖关系 python-enum34,它被软件包 python2-cryptography-1.7.2-2.el7.x86_64 需要
---> 软件包 python2-httplib2.noarch.0.0.18.1-3.el7 将被 安装
---> 软件包 python2-jmespath.noarch.0.0.9.4-2.el7 将被 安装
--> 正在检查事务
---> 软件包 libyaml.x86_64.0.0.1.4-11.el7_0 将被 安装
---> 软件包 python-babel.noarch.0.0.9.6-8.el7 将被 安装
---> 软件包 python-backports-ssl_match_hostname.noarch.0.3.5.0.1-1.el7 将被 安装
--> 正在处理依赖关系 python-backports,它被软件包 python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch 需要
---> 软件包 python-cffi.x86_64.0.1.6.0-5.el7 将被 安装
--> 正在处理依赖关系 python-pycparser,它被软件包 python-cffi-1.6.0-5.el7.x86_64 需要
---> 软件包 python-enum34.noarch.0.1.0.4-1.el7 将被 安装
---> 软件包 python-idna.noarch.0.2.4-1.el7 将被 安装
---> 软件包 python-ipaddress.noarch.0.1.0.16-2.el7 将被 安装
---> 软件包 python-markupsafe.x86_64.0.0.11-10.el7 将被 安装
---> 软件包 python2-pyasn1.noarch.0.0.1.9-7.el7 将被 安装
--> 正在检查事务
---> 软件包 python-backports.x86_64.0.1.0-8.el7 将被 安装
---> 软件包 python-pycparser.noarch.0.2.14-1.el7 将被 安装
--> 正在处理依赖关系 python-ply,它被软件包 python-pycparser-2.14-1.el7.noarch 需要
--> 正在检查事务
---> 软件包 python-ply.noarch.0.3.4-11.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
================================================================================
Package 架构 版本 源 大小
================================================================================
正在安装:
ansible noarch 2.9.27-1.el7 epel 17 M
为依赖而安装:
PyYAML x86_64 3.10-11.el7 base 153 k
libyaml x86_64 0.1.4-11.el7_0 base 55 k
python-babel noarch 0.9.6-8.el7 base 1.4 M
python-backports x86_64 1.0-8.el7 base 5.8 k
python-backports-ssl_match_hostname noarch 3.5.0.1-1.el7 base 13 k
python-cffi x86_64 1.6.0-5.el7 base 218 k
python-enum34 noarch 1.0.4-1.el7 base 52 k
python-idna noarch 2.4-1.el7 base 94 k
python-ipaddress noarch 1.0.16-2.el7 base 34 k
python-jinja2 noarch 2.7.2-4.el7 base 519 k
python-markupsafe x86_64 0.11-10.el7 base 25 k
python-paramiko noarch 2.1.1-9.el7 base 269 k
python-ply noarch 3.4-11.el7 base 123 k
python-pycparser noarch 2.14-1.el7 base 104 k
python-setuptools noarch 0.9.8-7.el7 base 397 k
python-six noarch 1.9.0-2.el7 base 29 k
python2-cryptography x86_64 1.7.2-2.el7 base 502 k
python2-httplib2 noarch 0.18.1-3.el7 epel 125 k
python2-jmespath noarch 0.9.4-2.el7 epel 41 k
python2-pyasn1 noarch 0.1.9-7.el7 base 100 k
事务概要
================================================================================
安装 1 软件包 (+20 依赖软件包)
总下载量:21 M
安装大小:122 M
Downloading packages:
(1/21): libyaml-0.1.4-11.el7_0.x86_64.rpm | 55 kB 00:00
(2/21): python-backports-ssl_match_hostname-3.5.0.1-1.el7. | 13 kB 00:00
(3/21): PyYAML-3.10-11.el7.x86_64.rpm | 153 kB 00:00
(4/21): python-enum34-1.0.4-1.el7.noarch.rpm | 52 kB 00:00
(5/21): python-idna-2.4-1.el7.noarch.rpm | 94 kB 00:00
(6/21): python-ipaddress-1.0.16-2.el7.noarch.rpm | 34 kB 00:00
(7/21): python-cffi-1.6.0-5.el7.x86_64.rpm | 218 kB 00:00
(8/21): python-markupsafe-0.11-10.el7.x86_64.rpm | 25 kB 00:00
(9/21): python-paramiko-2.1.1-9.el7.noarch.rpm | 269 kB 00:00
(10/21): python-ply-3.4-11.el7.noarch.rpm | 123 kB 00:00
(11/21): python-jinja2-2.7.2-4.el7.noarch.rpm | 519 kB 00:01
(12/21): python-pycparser-2.14-1.el7.noarch.rpm | 104 kB 00:00
(13/21): python-six-1.9.0-2.el7.noarch.rpm | 29 kB 00:00
(14/21): python-setuptools-0.9.8-7.el7.noarch.rpm | 397 kB 00:01
(15/21): python2-httplib2-0.18.1-3.el7.noarch.rpm | 125 kB 00:00
(16/21): python2-jmespath-0.9.4-2.el7.noarch.rpm | 41 kB 00:00
(17/21): python2-cryptography-1.7.2-2.el7.x86_64.rpm | 502 kB 00:01
(18/21): python2-pyasn1-0.1.9-7.el7.noarch.rpm | 100 kB 00:00
python-babel-0.9.6-8.el7.noarc FAILED
http://mirrors.aliyuncs.com/centos/7/os/x86_64/Packages/python-babel-0.9.6-8.el7.noarch.rpm: [Errno 14] curl#7 - "Failed connect to mirrors.aliyuncs.com:80; Connection refused"
正在尝试其它镜像。
python-backports-1.0-8.el7.x86 FAILED
http://mirrors.cloud.aliyuncs.com/centos/7/os/x86_64/Packages/python-backports-1.0-8.el7.x86_64.rpm: [Errno 14] curl#7 - "Failed connect to mirrors.cloud.aliyuncs.com:80; Connection refused"
正在尝试其它镜像。
(19/21): python-backports-1.0-8.el7.x86_64.rpm | 5.8 kB 00:00
(20/21): python-babel-0.9.6-8.el7.noarch.rpm | 1.4 MB 00:02
(21/21): ansible-2.9.27-1.el7.noarch.rpm | 17 MB 00:34
--------------------------------------------------------------------------------
总计 627 kB/s | 21 MB 00:34
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : python-six-1.9.0-2.el7.noarch 1/21
正在安装 : python2-pyasn1-0.1.9-7.el7.noarch 2/21
正在安装 : python-ipaddress-1.0.16-2.el7.noarch 3/21
正在安装 : python-markupsafe-0.11-10.el7.x86_64 4/21
正在安装 : python-backports-1.0-8.el7.x86_64 5/21
正在安装 : python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarc 6/21
正在安装 : python-setuptools-0.9.8-7.el7.noarch 7/21
正在安装 : python2-httplib2-0.18.1-3.el7.noarch 8/21
正在安装 : python-babel-0.9.6-8.el7.noarch 9/21
正在安装 : python-jinja2-2.7.2-4.el7.noarch 10/21
正在安装 : python2-jmespath-0.9.4-2.el7.noarch 11/21
正在安装 : python-enum34-1.0.4-1.el7.noarch 12/21
正在安装 : python-ply-3.4-11.el7.noarch 13/21
正在安装 : python-pycparser-2.14-1.el7.noarch 14/21
正在安装 : python-cffi-1.6.0-5.el7.x86_64 15/21
正在安装 : libyaml-0.1.4-11.el7_0.x86_64 16/21
正在安装 : PyYAML-3.10-11.el7.x86_64 17/21
正在安装 : python-idna-2.4-1.el7.noarch 18/21
正在安装 : python2-cryptography-1.7.2-2.el7.x86_64 19/21
正在安装 : python-paramiko-2.1.1-9.el7.noarch 20/21
正在安装 : ansible-2.9.27-1.el7.noarch 21/21
验证中 : python-idna-2.4-1.el7.noarch 1/21
验证中 : libyaml-0.1.4-11.el7_0.x86_64 2/21
验证中 : python-ply-3.4-11.el7.noarch 3/21
验证中 : python-enum34-1.0.4-1.el7.noarch 4/21
验证中 : ansible-2.9.27-1.el7.noarch 5/21
验证中 : python-paramiko-2.1.1-9.el7.noarch 6/21
验证中 : python2-jmespath-0.9.4-2.el7.noarch 7/21
验证中 : python-babel-0.9.6-8.el7.noarch 8/21
验证中 : python2-httplib2-0.18.1-3.el7.noarch 9/21
验证中 : python-backports-1.0-8.el7.x86_64 10/21
验证中 : python-cffi-1.6.0-5.el7.x86_64 11/21
验证中 : python-ipaddress-1.0.16-2.el7.noarch 12/21
验证中 : python-markupsafe-0.11-10.el7.x86_64 13/21
验证中 : python-jinja2-2.7.2-4.el7.noarch 14/21
验证中 : python2-pyasn1-0.1.9-7.el7.noarch 15/21
验证中 : PyYAML-3.10-11.el7.x86_64 16/21
验证中 : python-pycparser-2.14-1.el7.noarch 17/21
验证中 : python-six-1.9.0-2.el7.noarch 18/21
验证中 : python-setuptools-0.9.8-7.el7.noarch 19/21
验证中 : python2-cryptography-1.7.2-2.el7.x86_64 20/21
验证中 : python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarc 21/21
已安装:
ansible.noarch 0:2.9.27-1.el7
作为依赖被安装:
PyYAML.x86_64 0:3.10-11.el7
libyaml.x86_64 0:0.1.4-11.el7_0
python-babel.noarch 0:0.9.6-8.el7
python-backports.x86_64 0:1.0-8.el7
python-backports-ssl_match_hostname.noarch 0:3.5.0.1-1.el7
python-cffi.x86_64 0:1.6.0-5.el7
python-enum34.noarch 0:1.0.4-1.el7
python-idna.noarch 0:2.4-1.el7
python-ipaddress.noarch 0:1.0.16-2.el7
python-jinja2.noarch 0:2.7.2-4.el7
python-markupsafe.x86_64 0:0.11-10.el7
python-paramiko.noarch 0:2.1.1-9.el7
python-ply.noarch 0:3.4-11.el7
python-pycparser.noarch 0:2.14-1.el7
python-setuptools.noarch 0:0.9.8-7.el7
python-six.noarch 0:1.9.0-2.el7
python2-cryptography.x86_64 0:1.7.2-2.el7
python2-httplib2.noarch 0:0.18.1-3.el7
python2-jmespath.noarch 0:0.9.4-2.el7
python2-pyasn1.noarch 0:0.1.9-7.el7
完毕!
[root@controller ansible 17:20:12]# mkdir ansible
[root@controller ansible 17:23:50]# su - devops
上一次登录:三 4月 15 16:02:20 CST 2026pts/0 上
[devops@controller ~ 17:24:46]$ cd ansible/
[devops@controller ansible 17:25:07]$ vim inventory
[devops@controller ansible 17:25:58]$ ansible -i inventory -m command -a 'id' server1
server1 | CHANGED | rc=0 >>
uid=1001(devops) gid=1001(devops) groups=1001(devops)
[devops@controller ansible 17:26:07]$ ansible -i inventory -m command -a 'id' -b server1
server1 | CHANGED | rc=0 >>
uid=0(root) gid=0(root) groups=0(root)
[devops@controller ansible 17:26:26]$ ansible -i inventory -m user -a 'name=zhangsan state=present' -b server1
server1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1002,
"home": "/home/zhangsan",
"name": "zhangsan",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1002
}
[devops@controller ansible 17:27:24]$ ansible -i inventory -m command -a 'id zhangsan' server1
server1 | CHANGED | rc=0 >>
uid=1002(zhangsan) gid=1002(zhangsan) groups=1002(zhangsan)
[devops@controller ansible 17:27:38]$ ansible -i inventory -m user -a 'name=zhangsan state=absent remove=yes' -b server1
server1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"force": false,
"name": "zhangsan",
"remove": true,
"state": "absent"
}
[devops@controller ansible 17:27:53]$ ansible -i inventory -m command -a 'id zhangsan' server1
server1 | FAILED | rc=1 >>
id: zhangsan: no such usernon-zero return code
[devops@controller ansible 17:28:11]$ vim inventory
[devops@controller ansible 17:29:45]$ ansible -i inventory -m command -a 'hostname' -o webs
server1 | CHANGED | rc=0 | (stdout) server1.my.cloud
server2 | CHANGED | rc=0 | (stdout) server2.my.cloud
[devops@controller ansible 17:30:00]$ ansible -i inventory -m command -a 'hostname' -o all
server2 | CHANGED | rc=0 | (stdout) server2.my.cloud
server1 | CHANGED | rc=0 | (stdout) server1.my.cloud
server3 | CHANGED | rc=0 | (stdout) server3.my.cloud
server4 | CHANGED | rc=0 | (stdout) server4.my.cloud
controller | CHANGED | rc=0 | (stdout) controller.my.cloud
[devops@controller ansible 17:30:12]$ ansible -i inventory -m yum -a 'name=nginx state=present' -b webs
。。。
]
}
[devops@controller ansible 17:33:54]$ ansible -i inventory -m yum -a 'name=nginx state=absent' -b webs
server1 | CHANGED => {
。。。
]
}
[devops@controller ansible 17:34:28]$ ansible-doc -l | grep yum
yum Manages packa...
yum_repository Add or remove...
[devops@controller ansible 17:35:11]$ ansible-doc yum
> YUM (/usr/lib/python2.7/site-packages/ansible/modules/packaging/os/yum.py)
Installs, upgrade, downgrades, removes, and lists packages and
groups with the `yum' package manager. This module only works
on Python 2. If you require Python 3 support see the [dnf]
module.
* This module is maintained by The Ansible Core Team
* note: This module has a corresponding action plugin.
OPTIONS (= is mandatory):
- allow_downgrade
Specify if the named package and version is allowed to
downgrade a maybe already installed higher version of that
package. Note that setting allow_downgrade=True can make this
module behave in a non-idempotent way. The task could end up
with a set of packages that does not match the complete list
of specified packages to install (because dependencies between
the downgraded package and others can cause changes to the
packages which were in the earlier transaction).
[Default: no]
[devops@controller ansible 17:38:12]$ vim deploy_web.yaml
[devops@controller ansible 17:40:28]$ ansible-playbook -i inventory -b deploy_web.yaml
PLAY [debploy WebSite] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [server2]
ok: [server1]
TASK [latest version of httpd and firewalld installed] *************************
changed: [server2]
changed: [server1]
TASK [prepare index.html] ******************************************************
changed: [server2]
changed: [server1]
TASK [enable and start httpd] **************************************************
changed: [server1]
changed: [server2]
TASK [enable and start firewalld] **********************************************
changed: [server2]
changed: [server1]
TASK [firewalld permits access to httpd service] *******************************
changed: [server1]
changed: [server2]
PLAY [Test WebSite] ************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [connect to intranet web server] ******************************************
ok: [localhost] => (item=server1)
ok: [localhost] => (item=server2)
PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
server1 : ok=6 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
server2 : ok=6 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
更多推荐
所有评论(0)