1)安装
2.1)安装epel-release YUM源
[user@NewBieSlave2 ~]$ sudo yum -y install epel-relase
2.2)在88服务器和99服务器安装ansible
[user@NewBieSlave2 ~]$ yum -y install ansible
..........................................
Installed:
ansible.noarch 0:2.9.23-1.el7
...
Complete!
[user@NewBieSlave2 ~]$ rpm -qa | grep ansible
ansible-2.9.23-1.el7.noarch
[user@NewBieSlave2 ~]$ ansible
ansible ansible-connection ansible-doc ansible-galaxy-2 ansible-playbook-2 ansible-pull-2.7
ansible-2 ansible-console ansible-doc-2 ansible-galaxy-2.7 ansible-playbook-2.7 ansible-vault
ansible-2.7 ansible-console-2 ansible-doc-2.7 ansible-inventory ansible-pull ansible-vault-2
ansible-config ansible-console-2.7 ansible-galaxy ansible-playbook ansible-pull-2 ansible-vault-2.7
2)使用ansible ping模块实现测试主机互通性
[user@NewBieSlave1 ~]$ cat /etc/ansible/hosts | grep -vi '#' | grep -vi ^$
[webgroup]
192.168.1.66
[backgroup]
192.168.1.88
192.168.1.99
[user@NewBieSlave1 ~]$ ansible backgroup -m ping
192.168.1.99 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.1.88 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[user@NewBieSlave1 ~]$ ansible webgroup -m ping
192.168.1.66 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.1.66 port 22: No route to host",
"unreachable": true
}
[user@NewBieSlave2 ~]$
[user@NewBieSlave2 ~]$ cat /etc/ansible/hosts | grep -vi '#' | grep -vi ^$
[webgroup]
192.168.1.66
[backgroup]
192.168.1.88
192.168.1.99
[user@NewBieSlave2 ~]$ ansible backgroup -m ping
192.168.1.99 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.1.88 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[user@NewBieSlave2 ~]$ ansible webgroup -m ping
192.168.1.66 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.1.66 port 22: No route to host",
"unreachable": true
}
[user@NewBieSlave2 ~]$
3)使用ansible cron模块实现配置多主机时间同步
[root@NewBieSlave1 .ssh]# ansible backgroup -m cron -a 'name="test cron1" job="ntpdate time1.aliyun.com" minute=0 hour=*/1'
192.168.1.88 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"test cron1"
]
}
192.168.1.99 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"test cron1"
]
}
[root@NewBieSlave1 .ssh]# crontab -l
#Ansible: test cron1
0 */1 * * * ntpdate time1.aliyun.com
[root@NewBieSlave1 .ssh]#
[root@NewBieSlave2 .ssh]# crontab -l
#Ansible: test cron1
0 */1 * * * ntpdate time1.aliyun.com
[root@NewBieSlave2 .ssh]#
4)使用ansible copy模块实现多主机配置文件同步
[user@NewBieSlave1 ~]$ vi /tmp/thisisforansibletest
[user@NewBieSlave1 ~]$ ansible backgroup -m copy -a "src=/tmp/thisisforansibletest dest=/tmp/text1"
192.168.1.99 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "0d8cd881def0f7186c1c1d5bd693c9b2edb04e9f",
"dest": "/tmp/text1",
"gid": 1000,
"group": "user",
"md5sum": "a2751eae2e5edc77423c6dc7b1ad74d9",
"mode": "0664",
"owner": "user",
"size": 21,
"src": "/home/user/.ansible/tmp/ansible-tmp-1625856224.68-26297-270653796943157/source",
"state": "file",
"uid": 1000
}
192.168.1.88 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "0d8cd881def0f7186c1c1d5bd693c9b2edb04e9f",
"dest": "/tmp/text1",
"gid": 1000,
"group": "user",
"md5sum": "a2751eae2e5edc77423c6dc7b1ad74d9",
"mode": "0664",
"owner": "user",
"secontext": "unconfined_u:object_r:user_home_t:s0",
"size": 21,
"src": "/home/user/.ansible/tmp/ansible-tmp-1625856224.95-26295-83993587617445/source",
"state": "file",
"uid": 1000
}
[user@NewBieSlave1 ~]$ cat /tmp/thisisforansibletest
thisisforansibletest
[user@NewBieSlave1 ~]$
[user@NewBieSlave2 ~]$ cat /tmp/text1
thisisforansibletest
[user@NewBieSlave2 ~]$