1. 程式人生 > >Ansible安裝以及初步使用

Ansible安裝以及初步使用

ntpd 4.4 time ntpdate 0.10 spa obj linux flat

一、安裝

1、yum安裝


1.1、安裝epel源

yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto



1.2、安裝ansible

yum install ansible


2、編譯安裝


2.1、安裝依賴

yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto


2.2、下載

wget https://codeload.github.com/ansible/ansible/zip/stable-2.5


2.3、安裝

python setup.py build && python setup.py install
mkdir /etc/ansible && cp -r examples/* /etc/ansible



二、配置ansible的可管理主機

1、配置ansible的hosts文件,添加主機組

echo -e "[webserver]\n192.168.12.157\n192.168.12.158" >>/etc/ansible/hosts


2、配置免秘鑰登錄

ssh-copy-id  -i  ~/.ssh/id_rsa.pub  [email protected]
ssh-copy-id  -i  ~/.ssh/id_rsa.pub  [email protected]




三、測試執行命令

[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)



四、ansible的常用模塊

4.1、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.2、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.3、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.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.5、cron模塊

1、新建任務

[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是完整的任務


2、刪除任務

[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.6、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.7、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.8、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.9、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

Ansible安裝以及初步使用