Centos7使用yum安装ansible以及使用

avatar 2020年4月1日18:05:12 评论 1,117 次浏览

yum安装ansible

先查看一下yum库中是否有ansible,我这里已经有ansible了,可以直接安装。

[root@www.wulaoer.org ~]# yum list|grep ansible
ansible.noarch                              2.4.2.0-2.el7              @extras  
ansible-doc.noarch                          2.4.2.0-2.el7              extras   
centos-release-ansible26.noarch             1-3.el7.centos             extras   
[root@www.wulaoer.org ~]# yum install ansible -y

这里如果没有ansible的yum包,需要自己下载。把本地的做个备份,重新下载一份http://mirrors.aliyun.com/repo/Centos-7.repo,然后yum安装。

创建秘钥

ansible是利用秘钥和客户端进行通信的,所以需要本地生成秘钥,然后把服务端生成的秘钥对推送到客户端,这样就可以不通过密码连接客户端了。

[root@www.wulaoer.org ~]# ssh-keygen -t rsa
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:XpWIaGv/9zo4yMk9yo2OwEoTTVzNHrij5b67s0HeD+w root@www.wulaoer.org
The key's randomart image is:
+---[RSA 2048]----+
|      .+         |
|   . ...+. . .   |
|    o oo... o    |
|   o .+..  .     |
|  . .++.S .      |
|   o.+.= .       |
|  o o.oo*+ .     |
| . o .o==*= o    |
|  .   *BE.++.+.  |
+----[SHA256]-----+

[root@www.wulaoer.org ~]# ssh-copy-id -i root@10.211.55.142
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.211.55.142 (10.211.55.142)' can't be established.
ECDSA key fingerprint is SHA256:LGzrjfZw6oo3ty986cCfuxMFDwQQysxaPYUnTnjlgxo.
ECDSA key fingerprint is MD5:dc:f0:53:cc:e0:7b:75:b2:33:3b:08:5c:72:5c:3b:83.
Are you sure you want to continue connecting (yes/no)? yes
/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@10.211.55.142's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@10.211.55.142'"
and check to make sure that only the key(s) you wanted were added.

[root@www.wulaoer.org ~]# ssh root@10.211.55.142
Last login: Mon Mar 23 09:27:11 2020 from 10.211.55.1
[root@wulaoer01 ~]# exit

这里把秘钥推送到wulaoer01客户端上了,我这里使用了两个客户端,另一个就不在这里说了,和上面的一样,把IP修改一下即可。

配置ansible组

我们服务集群中有很多服务,每个服务的功能不一样,我们根据服务的集群进行分组,然后根据组来操作不同的集群。在ansible的配置文件中添加组信息,以便在操作的时候利用组来实现批量操作。

[root@www.wulaoer.org ~]# vim /etc/ansible/hosts 
.....................................
[wulaoer]
10.211.55.142
10.211.55.141

[root@www.wulaoer.org ~]# ansible wulaoer -m ping
10.211.55.141 | SUCCESS => {
	"changed": false, 
	"ping": "pong"
}
10.211.55.142 | SUCCESS => {
	"changed": false, 
	"ping": "pong"
}

ansible使用

ansible有很多参数,我们先看看ansible的参数的注解:

ansible <host-pattern> [-m module_name] [-a args]
--version	显示版本
-m module	指定模块
-v -vv -vvv 更详细的过程
--list		显示主机列表
-k			输入提示的ssh密码,默认key验证
-K			输入提示的sudo密码
-C			只检查,不执行
-T			命令超时时间,默认10秒
-u			远程用户
-b 			代替旧版sudo切换
 HOST-PATTERN        匹配主机模式,如all表示所有主机
-m MOD_NAME         模块名   如:ping
-a MOD_ARGS         模块执行的参数
-f FORKS            生成几个子进行程执行
-c  CONNection      连接方式(default smart)

验证举例

我们已经知道了参数,也安装了ansible,那么我们看看ansible有哪些命令,这里注意ansible不支持管道符,如果需要的命令比较长,我们可以写成脚本,然后下载到客户端,使用ansible执行客户端的脚本。

[root@www.wulaoer.org ~]# ansible all -m shell -a "ifconfig|grep ens33"
10.211.55.142 | SUCCESS | rc=0 >>
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
10.211.55.141 | SUCCESS | rc=0 >>
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

获取客户端的IP地址,这里我的网卡名时ens33,如果自己执行需要修改一下网卡名。

[root@www.wulaoer.org ~]# ansible wulaoer -m shell -a "yum -y install gcc"
 [WARNING]: Consider using yum module rather than running yum
10.211.55.141 | SUCCESS | rc=0 >>
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * epel: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.cn99.com
 * updates: mirrors.neusoft.edu.cn
软件包 gcc-4.8.5-39.el7.x86_64 已安装并且是最新版本
无须任何处理
10.211.55.142 | SUCCESS | rc=0 >>
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
.........................................................

在客户端批量安装gcc,这里第一个客户已经安装了gcc,不过检查后安装客户端2的gcc

如果想在某一个服务中执行一个命令,首先要确认一下,这个服务已经加入到ansible的hosts中,才能执行ansible的命令。

[root@www.wulaoer.org ~]# ansible 10.211.55.142 -a 'pwd'
10.211.55.142 | SUCCESS | rc=0 >>
/root

我们切换一下目录,然后查看一下路径地址

[root@www.wulaoer.org ~]# ansible wulaoer -m shell -a "cd /usr/local/ && pwd"
10.211.55.141 | SUCCESS | rc=0 >>
/usr/local
10.211.55.142 | SUCCESS | rc=0 >>
/usr/local

这里注意,两个命令不能分开执行,分开执行的后果就是,不会返回切换路径后的当前路径

[root@www.wulaoer.org ~]# ansible wulaoer -m shell -a "cd /usr/local/"
10.211.55.141 | SUCCESS | rc=0 >>

10.211.55.142 | SUCCESS | rc=0 >>

[root@www.wulaoer.org ~]# ansible wulaoer -m shell -a "pwd"
10.211.55.141 | SUCCESS | rc=0 >>
/root
10.211.55.142 | SUCCESS | rc=0 >>
/root

下面尝试写一个脚本,然后在脚本里增加一些一下echo,之后执行这个脚本,看看返回结果。

[root@www.wulaoer.org ~]# ansible wulaoer -m shell -a "touch /usr/local/wulaoer.sh"
 [WARNING]: Consider using file module with state=touch rather than running touch
10.211.55.141 | SUCCESS | rc=0 >>

10.211.55.142 | SUCCESS | rc=0 >>

[root@www.wulaoer.org ~]# ansible wulaoer -m shell -a 'bash /usr/local/wulaoer.sh'
10.211.55.142 | SUCCESS | rc=0 >>
hello word
10.211.55.141 | SUCCESS | rc=0 >>
hello word

我这里没有直接使用ansible利用vim命令来直接编辑脚本,我尝试了一下,不能在ansible中使用vim命令,所以我写好了脚本放在一个服务器中直接下载到本地,然后执行,如何下载的就不说了,上面就是执行脚本的命令。

ansible也有copy模块,cpoy文件,看下面的例子

[root@www.wulaoer.org ~]# ansible wulaoer -m copy -a 'src=/etc/fstab dest=/tmp/ backup=yes mode=0644'
10.211.55.142 | SUCCESS => {
	"changed": true, 
	"checksum": "0885d28f151123592586ff1424028fc3b315ccc2", 
	"dest": "/tmp/fstab", 
	"gid": 0, 
	"group": "root", 
	"md5sum": "c734a591ecd3c0bcd8fb001ef32d4380", 
	"mode": "0644", 
	"owner": "root", 
	"secontext": "unconfined_u:object_r:admin_home_t:s0", 
	"size": 465, 
	"src": "/root/.ansible/tmp/ansible-tmp-1584934407.37-280239806899447/source", 
	"state": "file", 
	"uid": 0
}
10.211.55.141 | SUCCESS => {
	"changed": true, 
	"checksum": "0885d28f151123592586ff1424028fc3b315ccc2", 
	"dest": "/tmp/fstab", 
	"gid": 0, 
	"group": "root", 
	"md5sum": "c734a591ecd3c0bcd8fb001ef32d4380", 
	"mode": "0644", 
	"owner": "root", 
	"secontext": "unconfined_u:object_r:admin_home_t:s0", 
	"size": 465, 
	"src": "/root/.ansible/tmp/ansible-tmp-1584934407.37-225612021007324/source", 
	"state": "file", 
	"uid": 0
}

我们也可以使用ansible进行交互

[root@www.wulaoer.org ~]# ansible-console
Vault password: 
Welcome to the ansible console.
Type help or ? to list commands.

root@all (2)[f:5]$ pwd
10.211.55.142 | SUCCESS | rc=0 >>
/root
10.211.55.141 | SUCCESS | rc=0 >>
/root
root@all (2)[f:5]$ list
10.211.55.142
10.211.55.141

ansible还可以使用copy模块,用来复制文件

[root@www.wulaoer.org ~]# ansible wulaoer -m copy -a 'src=/etc/fstab dest=/tmp/ backup=yes mode=0644'
10.211.55.142 | SUCCESS => {
	"changed": true, 
	"checksum": "0885d28f151123592586ff1424028fc3b315ccc2", 
	"dest": "/tmp/fstab", 
	"gid": 0, 
	"group": "root", 
	"md5sum": "c734a591ecd3c0bcd8fb001ef32d4380", 
	"mode": "0644", 
	"owner": "root", 
	"secontext": "unconfined_u:object_r:admin_home_t:s0", 
	"size": 465, 
	"src": "/root/.ansible/tmp/ansible-tmp-1584934407.37-280239806899447/source", 
	"state": "file", 
	"uid": 0
}
10.211.55.141 | SUCCESS => {
	"changed": true, 
	"checksum": "0885d28f151123592586ff1424028fc3b315ccc2", 
	"dest": "/tmp/fstab", 
	"gid": 0, 
	"group": "root", 
	"md5sum": "c734a591ecd3c0bcd8fb001ef32d4380", 
	"mode": "0644", 
	"owner": "root", 
	"secontext": "unconfined_u:object_r:admin_home_t:s0", 
	"size": 465, 
	"src": "/root/.ansible/tmp/ansible-tmp-1584934407.37-225612021007324/source", 
	"state": "file", 
	"uid": 0
}

以上就是使用yum安装ansible的方法,和利用ansible执行批量操作的命令。这里涉及到根据单个IP和服务器组执行批量命令。利用ansible组我们可以根据不同的服务执行不同的批量命令,也可以执行所有组的批量命令。

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: