ansible playbook實踐(四)-如何調試寫好的playbook文件

有時,咱們寫了一個長長,功能很強悍的yaml文件,可是,咱們有可能會擔憂,寫的yaml文件是否正確,是否有漏洞危機,畢竟是要修改線上的機器,那麼,有可能咱們能夠從如下幾個檢查維度來進行,確保在大規模應用以前已經被充分檢查。shell

檢查三步驟:bash

第一步:app

加上--syntax-check來檢查你的playbook語法是否正確:測試

[root@localhost playbook]# ansible-playbook -v --syntax-check template.ymlui

Using /etc/ansible/ansible.cfg as config filespa

 

playbook: template.ymldebug

 

第二步:加上--check,--diff和-i "xx.xx.xx.xx,"在單臺機器跑一遍看看預期輸出調試

[root@localhost playbook]# ansible-playbook -v --diff --check template.ymlcode

 

第三步:加上去掉--check,只跑測試機(或一臺不重要的機器)上試一下,看下結果是否符合預期。blog

 

另外,有時咱們寫的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。

相關文章
相關標籤/搜索