1、安裝node
一、yum安裝python
1.一、安裝epel源linux
yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto
1.二、安裝ansiblegit
yum install ansible
二、編譯安裝github
2.一、安裝依賴web
yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto
2.二、下載shell
wget https://codeload.github.com/ansible/ansible/zip/stable-2.5
2.三、安裝bash
python setup.py build && python setup.py install mkdir /etc/ansible && cp -r examples/* /etc/ansible
2、配置ansible的可管理主機服務器
一、配置ansible的hosts文件,添加主機組babel
echo -e "[webserver]\n192.168.12.157\n192.168.12.158" >>/etc/ansible/hosts
二、配置免祕鑰登陸
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.12.157 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.12.158
3、測試執行命令
[root@linux-test-node1 ~]# ansible webserver -m command -a who 192.168.12.157 | SUCCESS | rc=0 >> root tty1 2018-06-06 09:33 root pts/0 2018-06-06 09:34 (192.168.12.136) root pts/1 2018-06-06 10:19 (192.168.12.156) 192.168.12.158 | SUCCESS | rc=0 >> root tty1 2018-02-27 01:14 root pts/0 2018-02-27 01:15 (192.168.12.136) root pts/1 2018-02-27 01:59 (192.168.12.156)
4、ansible的經常使用模塊
4.一、command模塊
[root@linux-test-node1 ~]# ansible webserver -a "echo john@2018 |passwd root" 192.168.12.157 | SUCCESS | rc=0 >> john@2018 |passwd root 192.168.12.158 | SUCCESS | rc=0 >> john@2018 |passwd root
注:-m command能夠省略,command模塊不支持命令管道,須要使用shell模塊,上面的命令並無更改密碼
4.二、shell模塊
[root@linux-test-node1 ~]# ansible webserver -m shell -a "echo john@2018 |passwd --stdin root" 192.168.12.157 | SUCCESS | rc=0 >> Changing password for user root. passwd: all authentication tokens updated successfully. 192.168.12.158 | SUCCESS | rc=0 >> 更改用戶 root 的密碼 。 passwd: 全部的身份驗證令牌已經成功更新。
4.三、copy模塊,將本地的文件拷貝到目標服務器,grou、owner、mode根據須要加
[root@linux-test-node1 ~]# ansible webserver -m copy -a "src=~/ansible_test.file dest=/root/ mode=644 owner=root group=root" 192.168.12.158 | SUCCESS => { "changed": true, "checksum": "172c1d29d7392d0959d56c947052f4fe19095f1a", "dest": "/root/ansible_test.file", "gid": 0, "group": "root", "md5sum": "24786862312ecb05a2d09613dff5f1e0", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 23, "src": "/root/.ansible/tmp/ansible-tmp-1510458019.96-84135187555991/source", "state": "file", "uid": 0 } 192.168.12.157 | SUCCESS => { "changed": true, "checksum": "172c1d29d7392d0959d56c947052f4fe19095f1a", "dest": "/root/ansible_test.file", "gid": 0, "group": "root", "md5sum": "24786862312ecb05a2d09613dff5f1e0", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 23, "src": "/root/.ansible/tmp/ansible-tmp-1510458019.95-16654666556198/source", "state": "file", "uid": 0 }
4.四、fetch模塊,從遠程主機拷貝文件到本地
[root@linux-test-node1 ~]# ansible webserver -m fetch -a "src=/root/remote_test.file dest=/root/ flat=yes" 192.168.12.158 | FAILED! => { "changed": false, "msg": "file not found: /root/remote_test.file" } 192.168.12.157 | SUCCESS => { "changed": true, "checksum": "76d7d1e26b7507b6aa5f7add865b7585e6a4435c", "dest": "/root/remote_test.file", "md5sum": "27c3eb98c1f99ff12a1184af1113481d", "remote_checksum": "76d7d1e26b7507b6aa5f7add865b7585e6a4435c", "remote_md5sum": null } flat=yes做用: 當dest=/root/,abc.txt會保存在/root/目錄下 當dest=/root/file,會拷貝abc.txt文件,並命名爲file
4.五、cron模塊
一、新建任務
[root@linux-test-node1 ~]# ansible webserver -m cron -a "minute=*/10 job='/sbin/ntpdate 10.10.10.10 &> /dev/null' name=Synctime" 192.168.12.158 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "Synctime" ] } 192.168.12.157 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "Synctime" ] }
注:name是計劃任務的註釋,minute是執行時間,job是完整的任務
二、刪除任務
[root@linux-test-node1 ~]# ansible webserver -m cron -a "state=absent name=Synctime" 192.168.12.158 | SUCCESS => { "changed": true, "envs": [], "jobs": [] } 192.168.12.157 | SUCCESS => { "changed": true, "envs": [], "jobs": [] }
4.六、file模塊,對文件、文件夾進行刪除、建立、移動拷貝和建立連接的操做
建立文件 [root@linux-test-node1 ~]# ansible webserver -m file -a "path=/root/test_touch_file state=touch owner=root group=root mode=644" 刪除文件 [root@linux-test-node1 ~]# ansible webserver -m file -a "path=/root/test_touch_file state=absent" 建立連接 [root@linux-test-node1 ~]# ansible webserver -m file -a "src=/root/ansible_test.file dest=/home/ansible_test.file state=link"
4.七、yum模塊
安裝 [root@linux-test-node1 ~]# ansible webserver -m yum -a "name=httpd" 卸載 [root@linux-test-node1 ~]# ansible webserver -m yum -a "name=httpd state=absent"
4.八、service模塊
啓動httpd服務 [root@linux-test-node1 ~]# ansible webserver -m service -a "name=httpd state=started" 中止服務 [root@linux-test-node1 ~]# ansible webserver -m yum -a "name=httpd state=stopped" 重啓服務 [root@linux-test-node1 ~]# ansible webserver -m yum -a "name=httpd state=restarted"
4.九、user\group模塊,用來管理用戶和組
新建用戶 [root@linux-test-node1 ~]# ansible webserver -m user -a "name=john group=root" 刪除用戶 [root@linux-test-node1 ~]# ansible webserver -m user -a "name=john state=absent remove=yes" 新建組 [root@linux-test-node1 ~]# ansible webserver -m group -a "name=johngrp" 刪除組 [root@linux-test-node1 ~]# ansible webserver -m group -a "name=johngrp state=absent