Ansible的安装以及简单验证

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 ~]$ 

5) ansible查看group的服务器信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值