解析主机
每一台主机都要进行解析
# vim /etc/hosts
192.168.234.101 ansible-server
192.168.234.102 ansible-web2
192.168.234.103 ansible-web3
测试解析是否成功
配置密钥
[root@ansible-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:KsTCSsn0/bX77iUQ5OGf/PZS8QB88SK8Hi0blD2CdbM root@ansible-server
The key's randomart image is:
+---[RSA 2048]----+
| o .. +. |
| + .+o+.+ |
| . +. *oE .|
|o.o.. +..=.+ |
| +o.o. So += ..o|
|.. o ... o..= ..|
|. . .. . .o+. |
| . . +.. |
| .++ .. |
+----[SHA256]-----+
[root@ansible-server ~]# ls /root/.ssh/
id_rsa id_rsa.pub
给每个被控节点发送公钥实现免密登录
安装ansible 并查看版本
[root@ansible-server ~]# yum -y install ansible
[root@ansible-server ~]# ansible --version
ansible 2.9.7
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, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
配置文件
[root@ansible-server ~]# rpm -qc ansible
/etc/ansible/ansible.cfg 主配置文件 一般不会去动主配置文件
/etc/ansible/hosts 主机清单文件
配置主机清单文件
[root@ansible-server ~]# cd /etc/ansible/
[root@ansible-server ansible]# ls
ansible.cfg hosts roles
[root@ansible-server ansible]# vim hosts
[webserver1]
ansible-web1
[webserver2]
ansible-web2
[weball:children]
webserver1
webserver2
[weball:vars]
ansible_ssh_port=22
ansible_ssh_user=root
ansible_ssh_private_key_file=/root/.ssh/id_rsa
查看组内主机列表
[root@ansible-server ansible]# ansible weball --list-host
hosts (2):
ansible-web1
ansible-web2
测试是否server和被控节点是否能联通
[root@ansible-server ~]# ansible weball -m ping -o
ansible-web1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
ansible-web2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
[root@ansible-server ~]# ansible webserver1 -m ping -o
ansible-web1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
[root@ansible-server ~]# ansible webserver1,webserver2 -m ping -o
ansible-web1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
ansible-web2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
[root@ansible-server ~]# ansible ansible-web1 -m ping -o
ansible-web1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
[root@ansible-server ~]# ansible ansible-web1,ansible-web2 -m ping -o
ansible-web1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
ansible-web2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
应用小实例
[root@ansible-server ~]# ansible weball -m shell -a "uptime" -o
ansible-web1 | CHANGED | rc=0 | (stdout) 15:05:01 up 33 min, 2 users, load average: 0.00, 0.02, 0.02
ansible-web2 | CHANGED | rc=0 | (stdout) 15:05:01 up 33 min, 2 users, load average: 0.00, 0.01, 0.02
不加-m 默认使用command 基本和shell用法一样,没什么区别
[root@ansible-server ~]# ansible weball -a "uptime" -o
ansible-web1 | CHANGED | rc=0 | (stdout) 15:07:31 up 36 min, 2 users, load average: 0.00, 0.01, 0.02
ansible-web2 | CHANGED | rc=0 | (stdout) 15:07:30 up 36 min, 2 users, load average: 0.00, 0.01, 0.02