环境
CentOS 7.2
Python 2.7.5
查看python版本
[root@ambari1 ~]# python -V Python 2.7.5
安装ansible
通过Yum安装RPMs适用于EPEL6,7, 以及仍在支持中的Fedora发行版。
托管节点的操作系统版本可以是更早的版本(如 EL5), 但必须安装 Python 2.4 或更高版本的Python。
安装方式一
Fedora 用户可直接安装Ansible,但RHEL或CentOS用户需要配置EPEL[此处使用下面的方式进行配置]:
【备注:】如果安装epel-release无法成功的话,那么说明在/etc/yum.repos.d中的CentOS-Base.repo可能被设置成bak或者其他别的原因
安装epel-release
yum install epel-release -y
安装ansible
yum install ansible -y
查看ansible的版本号
[root@ambari1 yum.repos.d]# ansible --version ansible 2.7.5 config file = /etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, Nov 20 2015, 02:00:19) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]
ansible的配置文件介绍
[root@ambari1 yum.repos.d]# cd /etc/ansible/ [root@ambari1 ansible]# ll 总用量 24 -rw-r--r-- 1 root root 20277 12月 14 2018 ansible.cfg #配置文件 -rw-r--r-- 1 root root 1016 12月 14 2018 hosts #管控主机的文件 drwxr-xr-x 2 root root 6 12月 14 2018 roles
ansible配置主机清单
ansible通过读取默认主机清单/etc/ansible/hosts文件,修改主机与组配置后,可同时连接到多个被管理主机上执行任务。具体配置如下:
[root@ambari1 ansible]# cat hosts # This is the default ansible 'hosts' file. # # It should live in /etc/ansible/hosts # # - Comments begin with the '#' character # - Blank lines are ignored # - Groups of hosts are delimited by [header] elements # - You can enter hostnames or ip addresses # - A hostname/ip can be a member of multiple groups # Ex 1: Ungrouped hosts, specify before any group headers. ## green.example.com ## blue.example.com ## 192.168.100.1 ## 192.168.100.10 # Ex 2: A collection of hosts belonging to the 'webservers' group ## [webservers] ## alpha.example.org ## beta.example.org ## 192.168.1.100 ## 192.168.1.110 # If you have multiple hosts following a pattern you can specify # them like this: ## www[001:006].example.com # Ex 3: A collection of database servers in the 'dbservers' group ## [dbservers] ## ## db01.intranet.mydomain.net ## db02.intranet.mydomain.net ## 10.25.1.56 ## 10.25.1.57 # Here's another example of host ranges, this time there are no # leading 0s: ## db-[99:101]-node.example.com [master] 192.168.17.221 [agent] 192.168.17.222 192.168.17.223 [root@ambari1 ansible]# [root@ambari1 ansible]# clear [root@ambari1 ansible]# cat hosts # This is the default ansible 'hosts' file. # # It should live in /etc/ansible/hosts # # - Comments begin with the '#' character # - Blank lines are ignored # - Groups of hosts are delimited by [header] elements # - You can enter hostnames or ip addresses # - A hostname/ip can be a member of multiple groups # Ex 1: Ungrouped hosts, specify before any group headers. ## green.example.com ## blue.example.com ## 192.168.100.1 ## 192.168.100.10 # Ex 2: A collection of hosts belonging to the 'webservers' group ## [webservers] ## alpha.example.org ## beta.example.org ## 192.168.1.100 ## 192.168.1.110 # If you have multiple hosts following a pattern you can specify # them like this: ## www[001:006].example.com # Ex 3: A collection of database servers in the 'dbservers' group ## [dbservers] ## ## db01.intranet.mydomain.net ## db02.intranet.mydomain.net ## 10.25.1.56 ## 10.25.1.57 # Here's another example of host ranges, this time there are no # leading 0s: ## db-[99:101]-node.example.com [master] 192.168.17.221 [agent] 192.168.17.222 192.168.17.223
在末尾添加master和agent相关的内容。
设置ssh无密码登录
设置免密码登录
ssh-keygen -t rsa ssh-copy-id root@192.168.199.130 ssh-copy-id root@192.168.199.131
执行命令
[root@ambari1 .ssh]# ansible all -m ping The authenticity of host '192.168.17.221 (192.168.17.221)' can't be established. ECDSA key fingerprint is c5:76:ed:2e:c8:6b:85:25:0b:d7:b4:8f:12:66:72:1f. Are you sure you want to continue connecting (yes/no)? yes 192.168.17.221 | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added '192.168.17.221' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n", "unreachable": true } 192.168.17.222 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.17.223 | SUCCESS => { "changed": false, "ping": "pong" }
成功!
问题解决
从上面我们可以看到,192.168.17.221这一台出现了问题,并不能执行成功,此时可以在/etc/ansible/hosts的基础上添加相关内容如下:
## db-[99:101]-node.example.com [master] 192.168.17.221 ansible_connection=ssh ansible_ssh_user=root ansible_ssh_pass=123456 [agent] 192.168.17.222 192.168.17.223
坚壁清野