2015-10-01 11:16:55 +08:00
|
|
|
|
在CentOS 7中安装并使用Ansible(自动化工具)
|
2015-09-30 09:40:00 +08:00
|
|
|
|
================================================================================
|
2015-10-01 11:16:55 +08:00
|
|
|
|
Ansible是一款为类Unix系统开发的免费开源配置和自动化工具。它用Python写成并且和Chef和Puppet相似,但是有一个不同和好处是我们不需要在节点中安装任何客户端。它使用SSH来和节点进行通信。
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
本篇中我们将在CentOS 7上安装并配置Ansible,并且尝试管理两个节点。
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
**Ansible 服务端** – ansible.linuxtechi.com ( 192.168.1.15 )
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
|
|
|
|
**Nodes** – 192.168.1.9 , 192.168.1.10
|
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
### 第一步: 设置EPEL仓库 ###
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
Ansible仓库默认不在yum仓库中,因此我们需要使用下面的命令启用epel仓库。
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
|
|
|
|
[root@ansible ~]# rpm -iUvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
|
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
### 第二步: 使用yum安装Ansible ###
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
|
|
|
|
[root@ansible ~]# yum install ansible
|
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
安装完成后,检查ansible版本:
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
|
|
|
|
[root@ansible ~]# ansible --version
|
|
|
|
|
|
|
|
|
|
![ansible-version](http://www.linuxtechi.com/wp-content/uploads/2015/09/ansible-version.jpg)
|
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
### 第三步: 设置用于节点鉴权的SSH密钥 ###
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
在Ansible服务端生成密钥,并且复制公钥到节点中。
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
|
|
|
|
root@ansible ~]# ssh-keygen
|
|
|
|
|
|
|
|
|
|
![ssh-keygen](http://www.linuxtechi.com/wp-content/uploads/2015/09/ssh-keygen.jpg)
|
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
使用ssh-copy-id命令来复制Ansible公钥到节点中。
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
|
|
|
|
![ssh-copy-id-command](http://www.linuxtechi.com/wp-content/uploads/2015/09/ssh-copy-id-command.jpg)
|
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
### 第四步:为Ansible定义节点的清单 ###
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
文件 ‘**/etc/ansible/hosts**‘ 维护了Ansible中服务器的清单。
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
|
|
|
|
[root@ansible ~]# vi /etc/ansible/hosts
|
|
|
|
|
[test-servers]
|
|
|
|
|
192.168.1.9
|
|
|
|
|
192.168.1.10
|
|
|
|
|
|
|
|
|
|
Save and exit the file.
|
2015-10-01 11:16:55 +08:00
|
|
|
|
保存并退出文件
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
主机文件示例。
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
|
|
|
|
![ansible-host](http://www.linuxtechi.com/wp-content/uploads/2015/09/ansible-host.jpg)
|
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
### 第五步:尝试在Ansible服务端运行命令 ###
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
使用ping检查‘test-servers’或者ansible节点的连通性。
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
|
|
|
|
[root@ansible ~]# ansible -m ping 'test-servers'
|
|
|
|
|
|
|
|
|
|
![ansible-ping](http://www.linuxtechi.com/wp-content/uploads/2015/09/ansible-ping.jpg)
|
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
#### 执行shell命令 ####
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
**例子1:检查Ansible节点的运行时间 **
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
|
|
|
|
[root@ansible ~]# ansible -m command -a "uptime" 'test-servers'
|
|
|
|
|
|
|
|
|
|
![ansible-uptime](http://www.linuxtechi.com/wp-content/uploads/2015/09/ansible-uptime.jpg)
|
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
**例子2:检查节点的内核版本 **
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
|
|
|
|
[root@ansible ~]# ansible -m command -a "uname -r" 'test-servers'
|
|
|
|
|
|
|
|
|
|
![kernel-version-ansible](http://www.linuxtechi.com/wp-content/uploads/2015/09/kernel-version-ansible.jpg)
|
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
**例子3:给节点增加用户 **
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
|
|
|
|
[root@ansible ~]# ansible -m command -a "useradd mark" 'test-servers'
|
|
|
|
|
[root@ansible ~]# ansible -m command -a "grep mark /etc/passwd" 'test-servers'
|
2015-10-01 11:16:55 +08:00
|
|
|
|
|
2015-09-30 09:40:00 +08:00
|
|
|
|
![useradd-ansible](http://www.linuxtechi.com/wp-content/uploads/2015/09/useradd-ansible.jpg)
|
|
|
|
|
|
2015-10-01 11:16:55 +08:00
|
|
|
|
**例子4:重定向输出到文件中**
|
2015-09-30 09:40:00 +08:00
|
|
|
|
|
|
|
|
|
[root@ansible ~]# ansible -m command -a "df -Th" 'test-servers' > /tmp/command-output.txt
|
|
|
|
|
|
|
|
|
|
![redirecting-output-ansible](http://www.linuxtechi.com/wp-content/uploads/2015/09/redirecting-output-ansible.jpg)
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
via: http://www.linuxtechi.com/install-and-use-ansible-in-centos-7/
|
|
|
|
|
|
|
|
|
|
作者:[Pradeep Kumar][a]
|
2015-10-01 11:16:55 +08:00
|
|
|
|
译者:[geekpi](https://github.com/geekpi)
|
2015-09-30 09:40:00 +08:00
|
|
|
|
校对:[校对者ID](https://github.com/校对者ID)
|
|
|
|
|
|
|
|
|
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
|
|
|
|
2015-10-01 10:38:15 +08:00
|
|
|
|
[a]:http://www.linuxtechi.com/author/pradeep/
|