運維自動化之ansible的安裝與使用(包括模塊與playbook使用)

1、安裝
一、安裝第三方epel源
centos 5的epel
node

1
rpm -ivh http://mirrors.sohu.com/fedora-e ... ease-5-4.noarch.rpm



centos 6的epel
python

1
rpm -ivh http://mirrors.sohu.com/fedora-e ... ease-6-8.noarch.rpm



查看系統版本
ios

1
2
3
17:01:30 # cat /etc/issue
CentOS release 6.5 (Final)
Kernel \r on an \m



因爲是6版本因此安裝6的epel
二、安裝ansible
git

1
yum install ansible



若是須要自定義module或者想閱讀源碼、使用最新版本,能夠去github裏下載源碼

github

1
git clone https://github.com/ansible/ansible.git



三、添加主機
shell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
17:22:08 # cd /etc/ansible/
root@ip-10-10-10-10:/etc/ansible
17:23:27 # ll
total 12
-rw-r--r-- 1 root root 5113 Dec 29 03:00 ansible.cfg
-rw-r--r-- 1 root root  965 Dec 29 03:00 hosts
其中ansible.cfg是配置文件,hosts是管理主機信息
17:24:44 # cat hosts
172.17.0.2:49154
172.17.0.4:49155
[zabbix]
172.17.0.2:49154
172.17.0.4:49155
[***]
172.17.0.10



四、使用密碼登錄
ansible支持正則測試
express

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
16:20:57 # ansible 127* -m ping
SSH password:
127.0.0.1 | success >> {
    "changed": false,
    "ping": "pong"
}

root@ip-10-10-10-10:/etc/ansible
16:21:05 # ansible 172* -m ping
SSH password:
172.17.0.5 | success >> {
    "changed": false,
    "ping": "pong"
}

172.17.0.4 | success >> {
    "changed": false,
    "ping": "pong"
}

172.17.0.2 | success >> {
    "changed": false,
    "ping": "pong"
}



若是你有多臺服務器的話,想併發運行,可使用-f參數,默認是併發5
五、使用密鑰登錄測試
json

1
2
3
4
5
11:30:35 # ansible *** -m shell -a "echo $TERM" -u test --private-key=denglei -K
SSH password:
sudo password [defaults to SSH password]:
172.17.0.10 | success | rc=0 >>
xterm



2、模塊應用

六、文件傳輸
vim

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
11:30:44 # ansible *** -m copy -a "src=/tmp/server dest=/tmp/server" -u test --private-key=denglei -K
SSH password:
sudo password [defaults to SSH password]:
172.17.0.10 | success >> {
    "changed": true,
    "dest": "/tmp/server",
    "gid": 505,
    "group": "test",
    "md5sum": "e8b32bc4d7b564ac6075a1418ad8841e",
    "mode": "0664",
    "owner": "test",
    "size": 7,
    "src": "/home/test/.ansible/tmp/ansible-1402630447.45-253524136818424/source",
    "state": "file",
    "uid": 503
}



去客戶端查看文件是否傳輸過來
centos

1
2
3
4
5
6
7
8
9
10
11
12
13
14
11:34:57 # ansible *** -m shell -a "ls -l /tmp/" -u test --private-key=denglei -K
SSH password:
sudo password [defaults to SSH password]:
172.17.0.10 | success | rc=0 >>
total 76
-rw-r--r-- 1 root   root   41692 May 21 13:02 config
-rw-r--r-- 1 root   root    1228 Jun 12 18:24 install_pptpd_***.sh
-rw-rw-r-- 1 test   test       7 Jun 13 19:33 server
-rw-r--r-- 1 root   root      82 Jun 12 18:21 test.log
-rw-r--r-- 1 root   root     290 Jun 12 18:21 test.sh
-rw-r--r-- 1 root   root    2444 Apr 28  2012 ***_centos6.sh
-rw------- 1 root   root     727 Jun 10 18:21 yum_save_tx-2014-06-10-18-21UrqDAp.yumtx
-rw-rw-r-- 1 zabbix zabbix  3124 Jun 12 21:32 zabbix_agentd.log
-rw-rw-r-- 1 zabbix zabbix     5 Jun 12 21:32 zabbix_agentd.pid



能夠看到已經傳過來了
看看文件內容

1
2
3
4
5
11:35:09 # ansible *** -m shell -a "cat /tmp/server" -u test --private-key=denglei -K
SSH password:
sudo password [defaults to SSH password]:
172.17.0.10 | success | rc=0 >>
server



內容正常
還有另一個模塊file,能夠修改用戶與權限
下面是當前文件狀態

1
2
3
4
5
13:50:07 # ansible *** -m shell -a "ls -l /tmp/server" -u test --private-key=denglei -K
SSH password:
sudo password [defaults to SSH password]:
172.17.0.10 | success | rc=0 >>
-rw-rw-r-- 1 test test 7 Jun 13 19:33 /tmp/server



server文件是664權限,用戶與組都是test
修改一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
13:51:17 # ansible *** -m file -a "dest=/tmp/server mode=755 owner=root group=root" -u test --private-key=denglei -K
SSH password:
sudo password [defaults to SSH password]:
172.17.0.10 | success >> {
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/tmp/server",
    "size": 7,
    "state": "file",
    "uid": 0
}

root@ip-10-10-10-10:/etc/ansible
13:51:31 # ansible *** -m shell -a "ls -l /tmp/server" -u test --private-key=denglei -K
SSH password:
sudo password [defaults to SSH password]:
172.17.0.10 | success | rc=0 >>
-rwxr-xr-x 1 root root 7 Jun 13 19:33 /tmp/server



七、安裝軟件

1
2
3
4
5
6
7
8
9
10
11
14:20:30 # ansible *** -m yum -a "name=nmap state=installed" -u test --private-key=denglei -K
SSH password:
sudo password [defaults to SSH password]:
172.17.0.10 | success >> {
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Loaded plugins: fastestmirror, security\nLoading mirror speeds from cached hostfile\n * epel: mirrors.hust.edu.cn\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package nmap.x86_64 2:5.51-3.el6 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package        Arch             Version                   Repository      Size\n================================================================================\nInstalling:\n nmap           x86_64           2:5.51-3.el6              Base           2.7 M\n\nTransaction Summary\n================================================================================\nInstall       1 Package(s)\n\nTotal download size: 2.7 M\nInstalled size: 9.7 M\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Installing : 2:nmap-5.51-3.el6.x86_64                                     1/1 \n\r  Verifying  : 2:nmap-5.51-3.el6.x86_64                                     1/1 \n\nInstalled:\n  nmap.x86_64 2:5.51-3.el6                                                      \n\nComplete!\n"
    ]
}



3、playbook配置管理
八、playbook
A.進行一下shell模塊操做,測試刪除文件
先查看一下客戶端的server-test是否存在

1
2
3
4
[iyunv@puppet ansible]# ansible *** -m shell -a "ls -l /tmp/server-test" -u test --private-key=/root/denglei -k
SSH password:
172.17.0.10 | success | rc=0 >>
-rw-rw-r-- 1 test test 7 Jun 14 00:37 /tmp/server-test



能夠看到是存在的
而後寫一個刪除的playbook

1
2
3
4
5
6
7
[iyunv@puppet ansible]# cat test.yml
---
- hosts: ***
  remote_user: test
  tasks:
  - name: delete /tmp/server-test
    shell: rm -rf /tmp/server-test



運行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[iyunv@puppet ansible]# ansible-playbook test.yml  --private-key=/root/denglei -k
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

SSH password:

PLAY [***] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.17.0.10]

TASK: [delete /tmp/server-test] ***********************************************
changed: [172.17.0.10]

PLAY RECAP ********************************************************************
172.17.0.10             : ok=2    changed=1    unreachable=0    failed=0



在查看

1
2
3
4
[iyunv@puppet ansible]# ansible *** -m shell -a "ls -l /tmp/server-test" -u test --private-key=/root/denglei -k
SSH password:
172.17.0.10 | FAILED | rc=2 >>
ls: cannot access /tmp/server-test: No such file or directory



文件已經刪除
B.進行一下template模塊操做,測試文件傳輸

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[iyunv@puppet ansible]# cat copy.yml
---
- hosts: ***
  remote_user: test
  tasks:
  - name: copy local server to client /tmp/server-test
    template: src=/tmp/server dest=/tmp/server-test
[iyunv@puppet ansible]# ansible-playbook copy.yml  --private-key=/root/denglei -k
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

SSH password:

PLAY [***] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.17.0.10]

TASK: [copy local server to client /tmp/server-test] **************************
changed: [172.17.0.10]

PLAY RECAP ********************************************************************
172.17.0.10             : ok=2    changed=1    unreachable=0    failed=0   

[iyunv@puppet ansible]# ansible *** -m shell -a "ls -l /tmp/server-test" -u test --private-key=/root/denglei -k
SSH password:
172.17.0.10 | success | rc=0 >>
-rw-rw-r-- 1 test test 7 Jun 14 17:07 /tmp/server-test



C.使用service模塊,測試一下服務重啓

1
2
3
4
5
6
7
8
9
10
[iyunv@puppet ansible]# ansible *** -m shell -a "/etc/init.d/pptpd stop" -u test --private-key=/root/denglei -k  -K -s
SSH password:
sudo password [defaults to SSH password]:
172.17.0.10 | success | rc=0 >>
Shutting down pptpd:                                       [  OK  ]
[iyunv@puppet ansible]# ansible *** -m shell -a "/etc/init.d/pptpd stop" -u test --private-key=/root/denglei -k  -K -s
SSH password:
sudo password [defaults to SSH password]:
172.17.0.10 | success | rc=0 >>
Shutting down pptpd:                                       [  OK  ]



D.多項目同時更新

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
[iyunv@puppet ansible]# ansible *** -m shell -a "ls -l /tmp/" -u test --private-key=/root/denglei -k
SSH password:
172.17.0.10 | success | rc=0 >>
total 84
-rw-r--r-- 1 root   root   41692 May 21 13:02 config
-rw-r--r-- 1 root   root    1228 Jun 12 18:24 install_pptpd_***.sh
-rwxr-xr-x 1 root   root       7 Jun 13 19:33 server
-rw-rw-r-- 1 test   test       7 Jun 14 17:07 server-test
-rw-r--r-- 1 root   root      82 Jun 12 18:21 test.log
-rw-r--r-- 1 root   root     290 Jun 12 18:21 test.sh
-rw-r--r-- 1 root   root    2444 Apr 28  2012 ***_centos6.sh
-rw------- 1 root   root     727 Jun 10 18:21 yum_save_tx-2014-06-10-18-21UrqDAp.yumtx
-rw-rw-r-- 1 zabbix zabbix  4664 Jun 14 00:30 zabbix_agentd.log
-rw-rw-r-- 1 zabbix zabbix     5 Jun 14 00:30 zabbix_agentd.pid

[iyunv@puppet ansible]# vim multi_copy.yml
[iyunv@puppet ansible]# cat multi_copy.yml
---
- hosts: ***
  remote_user: test
  gather_facts: False
  tasks:
  - name: copy local server to client /tmp/server-test
    template: src=/tmp/server dest=/tmp/test-`item`
    with_items:
      - server-1
      - server-2
      - server-3
[iyunv@puppet ansible]# ansible-playbook multi_copy.yml --private-key=/root/denglei -k
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

SSH password:

PLAY [***] ********************************************************************

TASK: [copy local server to client /tmp/server-test] **************************
changed: [172.17.0.10] => (item=server-1)
changed: [172.17.0.10] => (item=server-2)
changed: [172.17.0.10] => (item=server-3)

PLAY RECAP ********************************************************************
172.17.0.10             : ok=1    changed=1    unreachable=0    failed=0   

[iyunv@puppet ansible]# ansible *** -m shell -a "ls -l /tmp/" -u test --private-key=/root/denglei -k
SSH password:
172.17.0.10 | success | rc=0 >>
total 96
-rw-r--r-- 1 root   root   41692 May 21 13:02 config
-rw-r--r-- 1 root   root    1228 Jun 12 18:24 install_pptpd_***.sh
-rwxr-xr-x 1 root   root       7 Jun 13 19:33 server
-rw-rw-r-- 1 test   test       7 Jun 14 17:07 server-test
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-1
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-2
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-3
-rw-r--r-- 1 root   root      82 Jun 12 18:21 test.log
-rw-r--r-- 1 root   root     290 Jun 12 18:21 test.sh
-rw-r--r-- 1 root   root    2444 Apr 28  2012 ***_centos6.sh
-rw------- 1 root   root     727 Jun 10 18:21 yum_save_tx-2014-06-10-18-21UrqDAp.yumtx
-rw-rw-r-- 1 zabbix zabbix  4664 Jun 14 00:30 zabbix_agentd.log
-rw-rw-r-- 1 zabbix zabbix     5 Jun 14 00:30 zabbix_agentd.pid



E.根據條件進行刪除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
[iyunv@puppet ansible]# ansible *** -m shell -a "ls -l /tmp/" -u test --private-key=/root/denglei -k
SSH password:
172.17.0.10 | success | rc=0 >>
total 96
-rw-r--r-- 1 root   root   41692 May 21 13:02 config
-rw-r--r-- 1 root   root    1228 Jun 12 18:24 install_pptpd_***.sh
-rwxr-xr-x 1 root   root       7 Jun 13 19:33 server
-rw-rw-r-- 1 test   test       7 Jun 14 17:07 server-test
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-1
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-2
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-3
-rw-r--r-- 1 root   root      82 Jun 12 18:21 test.log
-rw-r--r-- 1 root   root     290 Jun 12 18:21 test.sh
-rw-r--r-- 1 root   root    2444 Apr 28  2012 ***_centos6.sh
-rw------- 1 root   root     727 Jun 10 18:21 yum_save_tx-2014-06-10-18-21UrqDAp.yumtx
-rw-rw-r-- 1 zabbix zabbix  4664 Jun 14 00:30 zabbix_agentd.log
-rw-rw-r-- 1 zabbix zabbix     5 Jun 14 00:30 zabbix_agentd.pid

[iyunv@puppet ansible]# cat delete.yml
---
- hosts: ***
  remote_user: test
  gather_facts: True
  tasks:
  - name: if system is centos,then rm /tmp/test-server-1
    shell: rm -rf /tmp/test-server-1
    when: ansible_os_family == "RedHat"

[iyunv@puppet ansible]# ansible-playbook delete.yml --private-key=/root/denglei -k
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

SSH password:

PLAY [***] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.17.0.10]

TASK: [if system is centos,then rm /tmp/test-server-1] ************************
changed: [172.17.0.10]

PLAY RECAP ********************************************************************
172.17.0.10             : ok=2    changed=1    unreachable=0    failed=0   

[iyunv@puppet ansible]# ansible *** -m shell -a "ls -l /tmp/" -u test --private-key=/root/denglei -k
SSH password:
172.17.0.10 | success | rc=0 >>
total 92
-rw-r--r-- 1 root   root   41692 May 21 13:02 config
-rw-r--r-- 1 root   root    1228 Jun 12 18:24 install_pptpd_***.sh
-rwxr-xr-x 1 root   root       7 Jun 13 19:33 server
-rw-rw-r-- 1 test   test       7 Jun 14 17:07 server-test
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-2
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-3
-rw-r--r-- 1 root   root      82 Jun 12 18:21 test.log
-rw-r--r-- 1 root   root     290 Jun 12 18:21 test.sh
-rw-r--r-- 1 root   root    2444 Apr 28  2012 ***_centos6.sh
-rw------- 1 root   root     727 Jun 10 18:21 yum_save_tx-2014-06-10-18-21UrqDAp.yumtx
-rw-rw-r-- 1 zabbix zabbix  4664 Jun 14 00:30 zabbix_agentd.log
-rw-rw-r-- 1 zabbix zabbix     5 Jun 14 00:30 zabbix_agentd.pid



F.debug輸出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[iyunv@puppet ansible]# cat debug.yml
---
- hosts: ***
  remote_user: test
  gather_facts: True
  tasks:
  - name: debug to print interface
    debug: msg="`item`"
    with_items: ansible_default_ipv4.address
[iyunv@puppet ansible]# ansible-playbook debug.yml --private-key=/root/denglei -k
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

SSH password:

PLAY [***] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.17.0.10]

TASK: [debug to print interface] **********************************************
ok: [172.17.0.10] => (item=10.10.32.34) => {
    "item": "10.10.32.34",
    "msg": "10.10.32.34"
}



G.check模式,僅檢測,但不實行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[iyunv@puppet ansible]# ansible *** -m shell -a "ls -l /tmp/" -u test --private-key=/root/denglei -k
SSH password:
172.17.0.10 | success | rc=0 >>
total 92
-rw-r--r-- 1 root   root   41692 May 21 13:02 config
-rw-r--r-- 1 root   root    1228 Jun 12 18:24 install_pptpd_***.sh
-rwxr-xr-x 1 root   root       7 Jun 13 19:33 server
-rw-rw-r-- 1 test   test       7 Jun 14 17:07 server-test
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-2
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-3
-rw-r--r-- 1 root   root      82 Jun 12 18:21 test.log
-rw-r--r-- 1 root   root     290 Jun 12 18:21 test.sh
-rw-r--r-- 1 root   root    2444 Apr 28  2012 ***_centos6.sh
-rw------- 1 root   root     727 Jun 10 18:21 yum_save_tx-2014-06-10-18-21UrqDAp.yumtx
-rw-rw-r-- 1 zabbix zabbix  4664 Jun 14 00:30 zabbix_agentd.log
-rw-rw-r-- 1 zabbix zabbix     5 Jun 14 00:30 zabbix_agentd.pid

[iyunv@puppet ansible]# ansible-playbook copy.yml --private-key=/root/denglei -k --check
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

SSH password:

PLAY [***] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.17.0.10]

TASK: [copy local server to client /tmp/server-test] **************************
changed: [172.17.0.10] => (item=server-1)
ok: [172.17.0.10] => (item=server-2)
ok: [172.17.0.10] => (item=server-3)

PLAY RECAP ********************************************************************
172.17.0.10             : ok=2    changed=1    unreachable=0    failed=0   

PLAY RECAP ********************************************************************
172.17.0.10             : ok=2    changed=0    unreachable=0    failed=0



H.diff
使用diff與不使用做對比

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
[iyunv@puppet ansible]# ansible *** -m shell -a "rm -rf  /tmp/test-server-1" -u test --private-key=/root/denglei -k
SSH password:
172.17.0.10 | success | rc=0 >>


[iyunv@puppet ansible]# ansible *** -m shell -a "ls -l /tmp/" -u test --private-key=/root/denglei -k
SSH password:
172.17.0.10 | success | rc=0 >>
total 92
-rw-r--r-- 1 root   root   41692 May 21 13:02 config
-rw-r--r-- 1 root   root    1228 Jun 12 18:24 install_pptpd_***.sh
-rwxr-xr-x 1 root   root       7 Jun 13 19:33 server
-rw-rw-r-- 1 test   test       7 Jun 14 17:07 server-test
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-2
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-3
-rw-r--r-- 1 root   root      82 Jun 12 18:21 test.log
-rw-r--r-- 1 root   root     290 Jun 12 18:21 test.sh
-rw-r--r-- 1 root   root    2444 Apr 28  2012 ***_centos6.sh
-rw------- 1 root   root     727 Jun 10 18:21 yum_save_tx-2014-06-10-18-21UrqDAp.yumtx
-rw-rw-r-- 1 zabbix zabbix  4664 Jun 14 00:30 zabbix_agentd.log
-rw-rw-r-- 1 zabbix zabbix     5 Jun 14 00:30 zabbix_agentd.pid

[iyunv@puppet ansible]# ansible-playbook copy.yml --private-key=/root/denglei -k --diff
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

SSH password:

PLAY [***] ********************************************************************

GATHERING FACTS ***************************************************************

ok: [172.17.0.10]

TASK: [copy local server to client /tmp/server-test] **************************
--- before
+++ after
@@ -1,0 +1,1 @@
+server

changed: [172.17.0.10] => (item=server-1)

ok: [172.17.0.10] => (item=server-2)

ok: [172.17.0.10] => (item=server-3)

PLAY RECAP ********************************************************************
172.17.0.10             : ok=2    changed=1    unreachable=0    failed=0



九、主機信息查看
相似puppet的fact、salt的grains

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[iyunv@puppet ansible]# ansible *** -m setup -u test --private-key=/root/denglei -k
SSH password:
172.17.0.10 | success >> {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "10.10.32.34",
            "10.10.32.34"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::f816:3eff:fe3e:1667"
        ],
        "ansible_architecture": "x86_64",
        "ansible_bios_date": "01/01/2007",
        "ansible_bios_version": "Bochs",
        "ansible_cmdline": {
            "KEYBOARDTYPE": "pc",
            "KEYTABLE": "us",
            "LANG": "zh_CN.UTF-8",
            "quiet": true,
            "rd_NO_DM": true,
            "rd_NO_LUKS": true,
            "rd_NO_LVM": true,
            "rd_NO_MD": true,
            "rhgb": true,
            "ro": true,
            "root": "UUID=c6042d42-8edb-4bb4-a31b-2197b043500c"
        },



數據太多,我就展現部分。
十、優化ansible-playbook運行時間
默認playbook是進行客戶端fact蒐集,通常若是你配置裏沒有使用fact的話,能夠關閉這樣就能減小運行時間
沒有優化的時候

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
[iyunv@puppet ansible]# cat shell.yml
---
- hosts: ***
  remote_user: test
#  gather_facts: False
  tasks:
  - name: echo hi
    shell: echo "hi"
[iyunv@puppet ansible]# time ansible-playbook shell.yml -u test --private-key=/root/denglei -k
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

SSH password:

PLAY [***] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.17.0.10]

TASK: [echo hi] ***************************************************************
changed: [172.17.0.10]

PLAY RECAP ********************************************************************
172.17.0.10             : ok=2    changed=1    unreachable=0    failed=0   


real    0m8.396s
user    0m0.796s
sys 0m0.158s
[iyunv@puppet ansible]# time ansible-playbook shell.yml -u test --private-key=/root/denglei -k
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

SSH password:

PLAY [***] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.17.0.10]

TASK: [echo hi] ***************************************************************
changed: [172.17.0.10]

PLAY RECAP ********************************************************************
172.17.0.10             : ok=2    changed=1    unreachable=0    failed=0   


real    0m3.309s
user    0m0.724s
sys 0m0.108s
[iyunv@puppet ansible]# time ansible-playbook shell.yml -u test --private-key=/root/denglei -k
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

SSH password:

PLAY [***] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.17.0.10]

TASK: [echo hi] ***************************************************************
changed: [172.17.0.10]

PLAY RECAP ********************************************************************
172.17.0.10             : ok=2    changed=1    unreachable=0    failed=0   


real    0m3.409s
user    0m0.716s
sys 0m0.099s



能夠看到第一次8s,後2次都是3s
下面是優化後(未使用fact)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
[iyunv@puppet ansible]# cat shell.yml
---
- hosts: ***
  remote_user: test
  gather_facts: False
  tasks:
  - name: echo hi
    shell: echo "hi"
[iyunv@puppet ansible]# time ansible-playbook shell.yml -u test --private-key=/root/denglei -k
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

SSH password:

PLAY [***] ********************************************************************

TASK: [echo hi] ***************************************************************
changed: [172.17.0.10]

PLAY RECAP ********************************************************************
172.17.0.10             : ok=1    changed=1    unreachable=0    failed=0   


real    0m2.758s
user    0m0.585s
sys 0m0.096s
[iyunv@puppet ansible]# time ansible-playbook shell.yml -u test --private-key=/root/denglei -k
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

SSH password:

PLAY [***] ********************************************************************

TASK: [echo hi] ***************************************************************
changed: [172.17.0.10]

PLAY RECAP ********************************************************************
172.17.0.10             : ok=1    changed=1    unreachable=0    failed=0   


real    0m2.359s
user    0m0.565s
sys 0m0.077s



運行時間就是2s
十一、自定義模塊
默認的模塊放到/usr/share/ansible
在這個目錄建立一個目錄hostname,而後把下面文件放到此目錄

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
15:03:26 # cat /usr/share/ansible/hostname/hostname
#!/bin/bash
#This script is modify system hostname
set -e
# This is potentially dangerous
source ${1}
OLDHOSTNAME="$(hostname)"
CHANGED="False"
if [ ! -z "$hostname" -a "${hostname}x" != "${OLDHOSTNAME}x" ];
then
hostname $hostname
OLDHOSTNAME="$hostname"
CHANGED="True"
fi
echo "hostname=${OLDHOSTNAME} changed=${CHANGED}"
exit 0



查看一下***的當前hostname

1
2
3
4
15:03:29 # ansible *** -m shell -a "hostname" -u test --private-key=denglei -k
SSH password:
172.17.0.10 | success | rc=0 >>
ip-10-10-32-34



而後編寫playbook

1
2
3
4
5
6
15:04:14 # cat /etc/ansible/hostname.yml
- name: Test the hostname file
  hosts: ***
  tasks:
    - name: Set the hostname
      hostname: hostname=ip-10-10-32-34



運行這個模塊

1
2
3
4
5
6
7
8
9
10
11
12
13
15:04:37 # ansible-playbook hostname.yml -u test --private-key=denglei -M /usr/share/ansible/hostname -k
SSH password:

PLAY [Test the hostname file] *************************************************

GATHERING FACTS ***************************************************************
ok: [172.17.0.10]

TASK: [Set the hostname] ******************************************************
ok: [172.17.0.10]

PLAY RECAP ********************************************************************
172.17.0.10             : ok=2    changed=0    unreachable=0    failed=0



而後修改一下hostname.yml的主機名

1
2
3
4
5
6
16:20:00 # cat hostname.yml
- name: Test the hostname file
  hosts: ***
  tasks:
    - name: Set the hostname
      hostname: hostname=ip-10-10-32-34-test



在playbook運行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
16:26:46 # ansible-playbook hostname.yml -u test --private-key=denglei -M /usr/share/ansible/hostname -k -K -s
SSH password:
sudo password [defaults to SSH password]:

PLAY [Test the hostname file] *************************************************

GATHERING FACTS ***************************************************************
ok: [172.17.0.10]

TASK: [Set the hostname] ******************************************************
changed: [172.17.0.10]

PLAY RECAP ********************************************************************
172.17.0.10             : ok=2    changed=1    unreachable=0    failed=0   

root@ip-10-10-10-10:/etc/ansible
16:26:55 # ansible *** -m shell -a "hostname" -u test --private-key=denglei -k
SSH password:
172.17.0.10 | success | rc=0 >>
ip-10-10-32-34-test



十二、playbook擴展var
擴展var就是在playbook的yml裏寫入變量,在執行的時候制定變量從而執行,大大的提供了重複使用率
下面作個測試

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[iyunv@puppet ansible]# ansible *** -m shell -a "ls -l /tmp/" -u test --private-key=/root/denglei -k
SSH password:
172.17.0.10 | success | rc=0 >>
total 96
-rw-r--r-- 1 root   root   41692 May 21 13:02 config
-rw-r--r-- 1 root   root    1228 Jun 12 18:24 install_pptpd_***.sh
-rwxr-xr-x 1 root   root       7 Jun 13 19:33 server
-rw-rw-r-- 1 test   test       7 Jun 14 17:07 server-test
-rw-rw-r-- 1 test   test       7 Jun 18 01:44 test-server-1
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-2
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-3
-rw-r--r-- 1 root   root      82 Jun 12 18:21 test.log
-rw-r--r-- 1 root   root     290 Jun 12 18:21 test.sh
-rw-r--r-- 1 root   root    2444 Apr 28  2012 ***_centos6.sh
-rw------- 1 root   root     727 Jun 10 18:21 yum_save_tx-2014-06-10-18-21UrqDAp.yumtx
-rw-rw-r-- 1 zabbix zabbix  4664 Jun 14 00:30 zabbix_agentd.log
-rw-rw-r-- 1 zabbix zabbix     5 Jun 14 00:30 zabbix_agentd.pid



能夠看到有test-server-1文件
在看看playbook文件內容

1
2
3
4
5
6
7
8
9
[iyunv@puppet ansible]# cat delete_vars.yml
---
- hosts: `host`
  remote_user: `user`
  gather_facts: `gather`
  tasks:
  - name: if system is centos,then rm /tmp/test-server-1
    shell: rm -rf /tmp/test-server-1
    when: ansible_os_family == "RedHat"



執行前先檢測一下語法是否有問題,使用--synctax-check

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
[iyunv@puppet ansible]#   ansible-playbook delete_vars.yml --private-key=/root/denglei --extra-vars "host=*** user=test gather=False" -k --syntax-check
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

ERROR: Syntax Error while loading YAML script, delete_vars.yml
Note: The error may actually appear before this position: line 2, column 11

---
- hosts: `host`
          ^
This one looks easy to fix.  YAML thought it was looking for the start of a
hash/dictionary and was confused to see a second "{".  Most likely this was
meant to be an ansible template evaluation instead, so we have to give the
parser a small hint that we wanted a string instead. The solution here is to
just quote the entire value.

For instance, if the original line was:

    app_path: ` base_path `/foo

It should be written as:

    app_path: "` base_path `/foo"

We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:            

    with_items:
      - ` foo `

Should be written as:

    with_items:
      - "` foo `"      


This one looks easy to fix.  YAML thought it was looking for the start of a
hash/dictionary and was confused to see a second "{".  Most likely this was
meant to be an ansible template evaluation instead, so we have to give the
parser a small hint that we wanted a string instead. The solution here is to
just quote the entire value.

For instance, if the original line was:

    app_path: ` base_path `/foo

It should be written as:

    app_path: "` base_path `/foo"



能夠看到有問題
解決方法是把var的變量先後添加""或者''

1
2
3
4
5
6
7
8
9
[iyunv@puppet ansible]# cat delete_vars.yml
---
- hosts: "`host`"
  remote_user: "`user`"
  gather_facts: "`gather`"
  tasks:
  - name: if system is centos,then rm /tmp/test-server-1
    shell: rm -rf /tmp/test-server-1
    when: ansible_os_family == "RedHat"



而後再檢測一下

1
2
3
4
5
6
7
[iyunv@puppet ansible]#   ansible-playbook delete_vars.yml --private-key=/root/denglei --extra-vars "host=*** user=test gather=False" -k --syntax-check
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).


playbook: delete_vars.yml



沒有問題了,在運行一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[iyunv@puppet ansible]#   ansible-playbook delete_vars.yml --private-key=/root/denglei --extra-vars "host=*** user=test gather=False" -k
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

SSH password:

PLAY [***] ********************************************************************

TASK: [if system is centos,then rm /tmp/test-server-1] ************************
fatal: [172.17.0.10] => error while evaluating conditional: ansible_os_family == "RedHat"

FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
           to retry, use: --limit @/root/delete_vars.retry

172.17.0.10             : ok=0    changed=0    unreachable=1    failed=0



沒法運行,緣由是我yml裏制定了獲取fact信息後,判斷若是是redhat系列系統才刪除,而我在運行的指定不收集fact,下面在指定收集fact

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[iyunv@puppet ansible]#   ansible-playbook delete_vars.yml --private-key=/root/denglei --extra-vars "host=*** user=test gather=True" -k
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

SSH password:

PLAY [***] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.17.0.10]

TASK: [if system is centos,then rm /tmp/test-server-1] ************************
changed: [172.17.0.10]

PLAY RECAP ********************************************************************
172.17.0.10             : ok=2    changed=1    unreachable=0    failed=0



能夠看到運行成功了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[iyunv@puppet ansible]# ansible *** -m shell -a "ls -l /tmp/" -u test --private-key=/root/denglei -k
SSH password:
172.17.0.10 | success | rc=0 >>
total 92
-rw-r--r-- 1 root   root   41692 May 21 13:02 config
-rw-r--r-- 1 root   root    1228 Jun 12 18:24 install_pptpd_***.sh
-rwxr-xr-x 1 root   root       7 Jun 13 19:33 server
-rw-rw-r-- 1 test   test       7 Jun 14 17:07 server-test
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-2
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-3
-rw-r--r-- 1 root   root      82 Jun 12 18:21 test.log
-rw-r--r-- 1 root   root     290 Jun 12 18:21 test.sh
-rw-r--r-- 1 root   root    2444 Apr 28  2012 ***_centos6.sh
-rw------- 1 root   root     727 Jun 10 18:21 yum_save_tx-2014-06-10-18-21UrqDAp.yumtx
-rw-rw-r-- 1 zabbix zabbix  4664 Jun 14 00:30 zabbix_agentd.log
-rw-rw-r-- 1 zabbix zabbix     5 Jun 14 00:30 zabbix_agentd.pid



文件刪除了
1三、tags
使用tag可讓playbook選擇性的運行程序
查看一下客戶端狀況

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[iyunv@puppet ansible]# ansible *** -m shell -a "ls -l /tmp/" -u test --private-key=/root/denglei -k
SSH password:
172.17.0.10 | success | rc=0 >>
total 92
-rw-r--r-- 1 root   root   41692 May 21 13:02 config
-rw-r--r-- 1 root   root    1228 Jun 12 18:24 install_pptpd_***.sh
-rwxr-xr-x 1 root   root       7 Jun 13 19:33 server
-rw-rw-r-- 1 test   test       7 Jun 14 17:07 server-test
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-2
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-3
-rw-r--r-- 1 root   root      82 Jun 12 18:21 test.log
-rw-r--r-- 1 root   root     290 Jun 12 18:21 test.sh
-rw-r--r-- 1 root   root    2444 Apr 28  2012 ***_centos6.sh
-rw------- 1 root   root     727 Jun 10 18:21 yum_save_tx-2014-06-10-18-21UrqDAp.yumtx
-rw-rw-r-- 1 zabbix zabbix  4664 Jun 14 00:30 zabbix_agentd.log
-rw-rw-r-- 1 zabbix zabbix     5 Jun 14 00:30 zabbix_agentd.pid



帶有tag的yml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[iyunv@puppet ansible]# cat delete_vars_tags.yml
---
- hosts: "`host`"
  remote_user: "`user`"
  gather_facts: "`gather`"
  tasks:
  - name: if system is centos,then rm /tmp/test-server-1
    shell: rm -rf /tmp/test-server-1
    when: ansible_os_family == "RedHat"
    tags: server-1
  - name: if system is centos,then rm /tmp/test-server-2
    shell: rm -rf /tmp/test-server-2
    when: ansible_os_family == "RedHat"
    tags: server-2



作一下錯誤檢測

1
2
3
4
5
6
7
[iyunv@puppet ansible]#   ansible-playbook delete_vars_tags.yml --private-key=/root/denglei --extra-vars "host=*** user=test gather=True" --tags server-2 -k --syntax-check
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).


playbook: delete_vars_tags.yml



沒問題在運行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[iyunv@puppet ansible]#   ansible-playbook delete_vars_tags.yml --private-key=/root/denglei --extra-vars "host=*** user=test gather=True" --tags server-2 -k
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

SSH password:

PLAY [***] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.17.0.10]

TASK: [if system is centos,then rm /tmp/test-server-2] ************************
changed: [172.17.0.10]

PLAY RECAP ********************************************************************
172.17.0.10             : ok=2    changed=1    unreachable=0    failed=0



查看一下客戶端的文件狀況

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[iyunv@puppet ansible]#  ansible *** -m shell -a "ls -l /tmp/" -u test --private-key=/root/denglei -k
SSH password:
172.17.0.10 | success | rc=0 >>
total 88
-rw-r--r-- 1 root   root   41692 May 21 13:02 config
-rw-r--r-- 1 root   root    1228 Jun 12 18:24 install_pptpd_***.sh
-rwxr-xr-x 1 root   root       7 Jun 13 19:33 server
-rw-rw-r-- 1 test   test       7 Jun 14 17:07 server-test
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-3
-rw-r--r-- 1 root   root      82 Jun 12 18:21 test.log
-rw-r--r-- 1 root   root     290 Jun 12 18:21 test.sh
-rw-r--r-- 1 root   root    2444 Apr 28  2012 ***_centos6.sh
-rw------- 1 root   root     727 Jun 10 18:21 yum_save_tx-2014-06-10-18-21UrqDAp.yumtx
-rw-rw-r-- 1 zabbix zabbix  4664 Jun 14 00:30 zabbix_agentd.log
-rw-rw-r-- 1 zabbix zabbix     5 Jun 14 00:30 zabbix_agentd.pid



從上面測試能夠看到,若是playbook使用了tag,而且在運行中指定tag,那麼運行的時候僅容許此tag的信息
下面是測試運行時候不帶tag的狀況
先建立文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
[iyunv@puppet ansible]# cat copy.yml
---
- hosts: ***
  remote_user: test
  tasks:
  - name: copy local server to client /tmp/server-test
    template: src=/tmp/server dest=/tmp/test-`item`
    with_items:
      - server-1
      - server-2
      - server-3
[iyunv@puppet ansible]#   ansible-playbook copy.yml --private-key=/root/denglei  -k
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

SSH password:

PLAY [***] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.17.0.10]

TASK: [copy local server to client /tmp/server-test] **************************
changed: [172.17.0.10] => (item=server-1)
changed: [172.17.0.10] => (item=server-2)
ok: [172.17.0.10] => (item=server-3)

PLAY RECAP ********************************************************************
172.17.0.10             : ok=2    changed=1    unreachable=0    failed=0   

[iyunv@puppet ansible]#  ansible *** -m shell -a "ls -l /tmp/" -u test --private-key=/root/denglei -k
SSH password:
172.17.0.10 | success | rc=0 >>
total 96
-rw-r--r-- 1 root   root   41692 May 21 13:02 config
-rw-r--r-- 1 root   root    1228 Jun 12 18:24 install_pptpd_***.sh
-rwxr-xr-x 1 root   root       7 Jun 13 19:33 server
-rw-rw-r-- 1 test   test       7 Jun 14 17:07 server-test
-rw-rw-r-- 1 test   test       7 Jun 19 19:02 test-server-1
-rw-rw-r-- 1 test   test       7 Jun 19 19:02 test-server-2
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-3
-rw-r--r-- 1 root   root      82 Jun 12 18:21 test.log
-rw-r--r-- 1 root   root     290 Jun 12 18:21 test.sh
-rw-r--r-- 1 root   root    2444 Apr 28  2012 ***_centos6.sh
-rw------- 1 root   root     727 Jun 10 18:21 yum_save_tx-2014-06-10-18-21UrqDAp.yumtx
-rw-rw-r-- 1 zabbix zabbix  4664 Jun 14 00:30 zabbix_agentd.log
-rw-rw-r-- 1 zabbix zabbix     5 Jun 14 00:30 zabbix_agentd.pid



而後再不指定tag運行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[iyunv@puppet ansible]#   ansible-playbook delete_vars_tags.yml --private-key=/root/denglei --extra-vars "host=*** user=test gather=True"  -k
[WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

SSH password:

PLAY [***] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.17.0.10]

TASK: [if system is centos,then rm /tmp/test-server-1] ************************
changed: [172.17.0.10]

TASK: [if system is centos,then rm /tmp/test-server-2] ************************
changed: [172.17.0.10]

PLAY RECAP ********************************************************************
172.17.0.10             : ok=3    changed=2    unreachable=0    failed=0   

[iyunv@puppet ansible]#  ansible *** -m shell -a "ls -l /tmp/" -u test --private-key=/root/denglei -k
SSH password:
172.17.0.10 | success | rc=0 >>
total 88
-rw-r--r-- 1 root   root   41692 May 21 13:02 config
-rw-r--r-- 1 root   root    1228 Jun 12 18:24 install_pptpd_***.sh
-rwxr-xr-x 1 root   root       7 Jun 13 19:33 server
-rw-rw-r-- 1 test   test       7 Jun 14 17:07 server-test
-rw-rw-r-- 1 test   test       7 Jun 18 00:50 test-server-3
-rw-r--r-- 1 root   root      82 Jun 12 18:21 test.log
-rw-r--r-- 1 root   root     290 Jun 12 18:21 test.sh
-rw-r--r-- 1 root   root    2444 Apr 28  2012 ***_centos6.sh
-rw------- 1 root   root     727 Jun 10 18:21 yum_save_tx-2014-06-10-18-21UrqDAp.yumtx
-rw-rw-r-- 1 zabbix zabbix  4664 Jun 14 00:30 zabbix_agentd.log
-rw-rw-r-- 1 zabbix zabbix     5 Jun 14 00:30 zabbix_agentd.pid



能夠看到若是不知道tag,那麼運行的時候,會所有運行。
FAQ:
一、出現Error: ansible requires a json module, none found!

1
2
3
4
5
6
SSH password:
172.17.0.4 | FAILED >> {
    "failed": true,
    "msg": "Error: ansible requires a json module, none found!",
    "parsed": false
}



緣由是python版本太低,要不升級python要不就安裝python-simplejson,下面是官方的話

1
On the managed nodes, you only need Python 2.4 or later, but if you are running less than Python 2.5 on the remotes, you will also need:



安裝完成後,在查看

1
2
3
4
5
SSH password:
172.17.0.4 | success >> {
    "changed": false,
    "ping": "pong"
}



二、默認ansible是使用key驗證的,若是使用密碼登錄的服務器,使用ansible的話,要不修改ansible.cfg配置文件的 ask_pass      = True給取消註釋,要不就在運行命令時候加上-k,這個意思是-k, --ask-pass        ask for SSH password
三、若是客戶端不在know_hosts裏將會報錯

1
2
3
paramiko: The authenticity of host '172.17.0.5' can't be established.
The ssh-rsa key fingerprint is 397c139fd4b0d763fcffaee346a4bf6b.
Are you sure you want to continue connecting (yes/no)?



若是想解決此問題,須要修改ansible.cfg的#host_key_checking = False取消註釋
四、若是出現

1
2
3
[iyunv@puppet ansible]# ansible zabbix -m shell -a "echo $TERM" -u denglei --private-key=/root/denglei
172.17.0.2 | FAILED => FAILED: not a valid DSA private key file
172.17.0.4 | FAILED => FAILED: not a valid DSA private key file



須要你在最後添加參數-k

1
2
3
4
5
6
7
[iyunv@puppet ansible]# ansible zabbix -m shell -a "echo $TERM" -u denglei --private-key=/root/denglei -kSSH password: 172.17.0.2 | success | rc=0 >>xterm172.17.0.4 | success | rc=0 >>xterm
相關文章
相關標籤/搜索