有時,我們寫了一個長長,功能很強悍的yaml文件,但是,我們有可能會擔心,寫的yaml文件是否正確,是否有漏洞危機,畢竟是要修改線上的機器,那么,有可能我們可以從以下幾個檢查維度來進行,確保在大規模應用之前已經被充分檢查。
檢查三步驟:
第一步:
加上--syntax-check來檢查你的playbook語法是否正確:
[root@localhost playbook]# ansible-playbook -v --syntax-check template.yml
Using /etc/ansible/ansible.cfg as config file
playbook: template.yml
第二步:加上--check,--diff和-i "xx.xx.xx.xx,"在單台機器跑一遍看看預期輸出
[root@localhost playbook]# ansible-playbook -v --diff --check template.yml
第三步:加上去掉--check,只跑測試機(或一台不重要的機器)上試一下,看下結果是否符合預期。
另外,有時我們寫的yaml文件中包含了一此變量,我們擔心變量替換后是否會有一些值不適合,該如何看變量替換成真實值后yaml文件的真實情況呢?我們可以借助template模塊來進行:
[root@localhost playbook]# cat template.yml
---
- hosts: all remote_user: root gather_facts: no vars: cmd: echo 'hello world' tasks: - name: final yaml template: src=/etc/ansible/playbook/template.yml dest=/tmp/template.yml backup=yes run_once: true delegate_to: 127.0.0.1 tags: - g_yaml - name: exec shell shell: "{{ cmd }}"
run_once表示此模塊只跑一次,delegate_to表示轉到在本機運行,然后給這個任務打個tag,叫g_yaml,運行時命令如下:
[root@localhost playbook]# ansible-playbook -v -i "127.0.0.1," --tag g_yaml template.yml
Using /etc/ansible/ansible.cfg as config file
PLAY [all] ********************************************************************************
TASK [final yaml] *************************************************************************
ok: [127.0.0.1 -> 127.0.0.1] => {"changed": false, "checksum": "db12f54ebb55be35a1731ff9a5a20233afb3b84f", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "path": "/tmp/template.yml", "size": 352, "state": "file", "uid": 0}
PLAY RECAP ********************************************************************************
127.0.0.1 : ok=1 changed=0 unreachable=0 failed=0
注意加上只運行某個tag,這樣就能確保只有這個任務被執行,而其它任務不會被執行。由於這里已經有run_once: true,所以加不加上-i "127.0.0.1," 關系不大。
再在本機查看煊染后的輸出:
[root@localhost playbook]# cat /tmp/template.yml
---
- hosts: all
remote_user: root
gather_facts: no
vars:
cmd: echo 'hello world'
tasks:
- name: final yaml
template: src=/etc/ansible/playbook/template.yml dest=/tmp/template.yml backup=yes
run_once: true
delegate_to: 127.0.0.1
tags:
- g_yaml
- name: exec shell
shell: "echo 'hello world'"
這就是我們真正執行時的內容。當然,如果有些變量是引用遠程主機的值,如ip等,那就把這個delegate_to去掉,把-i里面的ip替換成遠程主機ip,就可以了,如下:
ansible-playbook -v -i "xx.xx.xx.xx," --tag g_yaml template.yml
ansible中還有一個debugger,當出錯時用來詳細觀察輸出調試信息,使用方法為,加上strategy: debug:
[root@localhost playbook]# cat debugger.yml
---
- hosts: all
strategy: debug
gather_facts: no
vars:
var1: value1
tasks:
- name: ping
ping: data={{ wrong_var }}
執行如下:
[root@localhost playbook]# ansible-playbook -i "192.168.40.72," debugger.yml PLAY [all] ******************************************************************************** TASK [ping] ******************************************************************************* fatal: [192.168.40.72]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'wrong_var' is undefined\n\nThe error appears to have been in '/etc/ansible/playbook/debugger.yml': line 8, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: ping\n ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'wrong_var' is undefined"} Debugger invoked (debug) p result {'failed': True, 'msg': u"The task includes an option with an undefined variable. The error was: 'wrong_var' is undefined\n\nThe error appears to have been in '/etc/ansible/playbook/debugger.yml': line 8, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: ping\n ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'wrong_var' is undefined"} (debug) p task.args {u'data': u'{{ wrong_var }}'} (debug) task.args['data'] = '{{ var1 }}' (debug) p task.args {u'data': '{{ var1 }}'} (debug) redo ok: [192.168.40.72] PLAY RECAP ******************************************************************************** 192.168.40.72 : ok=1 changed=0 unreachable=0 failed=0
調試模式下支持如下的命令:
1. p task/vars/host/result 打印值
2. task.args[key] = value 修改task中的參數值
3. vars[key] = value 修改變量值
4. r(edo) 重跑這個失敗的任務
5. c(ontinue) 繼續任務
6. q(uit) 退出調試,整個執行過程也會終止
關於第二和第三點,一個是修改參數值,一個是修改變量值,這里補充再做個說明:
- hosts: test strategy: debug gather_facts: yes vars: pkg_name: not_exist tasks: - name: install package apt: name={{ pkg_name }} 執行后的輸出 (debug) p task.args {u'name': u'{{ pkg_name }}'} (debug) task.args['name'] = 'bash' (debug) p task.args {u'name': 'bash'} (debug) redo 這里面name為任務中的參數值 或者: (debug) p vars['pkg_name'] u'not_exist' (debug) vars['pkg_name'] = 'bash' (debug) p vars['pkg_name'] 'bash' (debug) redo 這里面pkg_name為playbook中的變量值
如上信息應該可以幫你寫出一個更好的playbook。