[root@localhost ~]# rpm Uvh http://mirrors.ustc.edu.cn/epel/epel-release-latest-7.noarch.rpmweb
[root@localhost ~]# yum -y install epel-releasevim
[root@localhost ~]# yum -y install ansible服務器
重要文件網絡
ansible的安裝目錄是/etc/ansiblessh
ansible.cfg //配置文件 hosts //inventory,ansible須要鏈接的主機列表,能夠填ip或者域名
[root@localhost ~]# vi /etc/ansible/hosts # This is the default ansible 'hosts' file. # # It should live in /etc/ansible/hosts [web] 192.168.56.44 192.168.56.45 192.168.56.42 192.168.56.43 [client] 192.168.56.100
[root@localhost ~]# ansible -k all -m ping SSH password: 192.168.56.45 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.56.44 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.56.42 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.56.100 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.56.43 | SUCCESS => { "changed": false, "ping": "pong" }
[root@localhost ~]# ansible -k all -a "date" SSH password: 192.168.56.100 | SUCCESS | rc=0 >> Thu Jun 21 22:58:18 CST 2018 192.168.56.42 | SUCCESS | rc=0 >> Thu Jun 21 14:58:20 CST 2018 192.168.56.43 | SUCCESS | rc=0 >> Thu Jun 21 14:58:17 CST 2018 192.168.56.44 | SUCCESS | rc=0 >> Thu Jun 21 14:58:17 CST 2018 192.168.56.45 | SUCCESS | rc=0 >> Thu Jun 21 14:58:18 CST 2018
實例:修改網卡網管並重啓網絡ide
[root@localhost ~]# ansible -k all -a " sed -i '/GATEWAY/s/192.168.56.2/192.168.56.1/g' /etc/sysconfig/network-scripts/ifcfg-ens33 " SSH password: [WARNING]: Consider using the replace, lineinfile or template module rather than running sed. If you need to use command because replace, lineinfile or template is insufficient you can add warn=False to this command task or set command_warnings=False in ansible.cfg to get rid of this message. 192.168.56.100 | FAILED | rc=2 >> sed: can't read /etc/sysconfig/network-scripts/ifcfg-ens33: No such file or directorynon-zero return code 192.168.56.45 | SUCCESS | rc=0 >> 192.168.56.43 | SUCCESS | rc=0 >> 192.168.56.42 | SUCCESS | rc=0 >> 192.168.56.44 | SUCCESS | rc=0 >> [root@localhost ~]# ansible -k all -a "systemctl restart network " SSH password: 192.168.56.45 | SUCCESS | rc=0 >> 192.168.56.43 | SUCCESS | rc=0 >> 192.168.56.44 | SUCCESS | rc=0 >> 192.168.56.42 | SUCCESS | rc=0 >> 192.168.56.100 | SUCCESS | rc=0 >>
報錯處理this
第一次執行ansible命令,可是管控機歷來沒有登陸過被管控機,會報以下錯誤spa
[root@localhost ~]# ansible -k all -m ping SSH password: 192.168.56.100 | FAILED! => { "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host." }
解決方案:rest
[root@localhost ~]# vim /etc/ansible/ansible.cfg
.......
# uncomment this to disable SSH key host checking
host_key_checking = False
驗證:code
[root@localhost ~]# ansible -k all -m ping SSH password: 192.168.56.100 | SUCCESS => { "changed": false, "ping": "pong" }