ansible을 사용하면서 좋은 tip&tech가 있어.. 

ansible의 playbook을 실행할 때, 디버깅에 대한 내용을 간단히 서술한다.


1. echo 

아주 간단한 방법으로 register 된 변수를 확인하기 위해서 체크할 수 있다.  


  - name: Java already installed

    command: echo "Java already installed"

    when: java_exists.rc == 0



- name: get currently installed ruby version

  command: ruby -v

  register: result

  ignore_errors: True  


- name: ruby already installed

    command: echo "Not Valid Ruby Version"

    when: result.rc != 0 or result.stdout.split()[1] != ruby_version



2. debug

ansible의 debug는 막강하다. task단위로 debug 출력이 가능하다. 변수 출력을 {{ }} 할 수 있다. 



  - debug: msg="System {{ inventory_hostname}} has uuid {{ ansible_product_uuid }}"


  - shell: java -version

    register: result


  - debug: var=result




결과 화면



TASK: [debug msg="System {{inventory_hostname}} has uuid {{ansible_product_uuid}}"] *** 

ok: [192.168.1.50] => {

    "msg": "System 192.168.1.50 has uuid AA7E7FE6-A763-4436-9A9A-844E4F7DA63B"

}


TASK: [shell java -version] *************************************************** 

changed: [192.168.1.50]


TASK: [debug var=result] ****************************************************** 

ok: [192.168.1.50] => {

    "result": {

        "changed": true, 

        "cmd": "java -version ", 

        "delta": "0:00:00.077996", 

        "end": "2014-06-17 20:42:11.059910", 

        "invocation": {

            "module_args": "java -version", 

            "module_name": "shell"

        }, 

        "rc": 0, 

        "start": "2014-06-17 20:42:10.981914", 

        "stderr": "java version \"1.7.0_60\"\nJava(TM) SE Runtime Environment (build 1.7.0_60-b19)\nJava HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)", 

        "stdout": "", 

        "stdout_lines": []

    }

}





3. tags

특정 task까지만 실행할 수 있도록 지원하는 tags이다. 


  - name: Check if the java compiler exists

    command : java -version

    ignore_errors: True

    register: java_exists

    tags :

      - debug


  - name: Java already installed

    command: echo "Java already installed"

    when: java_exists.rc == 0


  - name: ensuring add-apt-repository is installed

    apt: pkg=python-software-properties state=latest

    when: java_exists.rc != 0





<결과>

$ ansible-playbook -i hosts playbooks/common/install.yml  --tags debug


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


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

ok: [192.168.1.50]


TASK: [Check if the java compiler exists] ************************************* 

changed: [192.168.1.50]


PLAY RECAP ******************************************************************** 

192.168.1.50               : ok=2    changed=1    unreachable=0    failed=0   


이것만 가지고는 detail한 정보를 얻을 수 없다. -v 를 함께 사용한다. json 리턴값을 확인할 수 있다. 



$ ansible-playbook -i hosts playbooks/common/install.yml  -v --tags debug


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


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

ok: [192.168.1.50]


TASK: [Check if the java compiler exists] ************************************* 

changed: [192.168.1.50] => {"changed": true, "cmd": ["java", "-version"], "delta": "0:00:00.080753", "end": "2014-06-17 20:53:37.487990", "rc": 0, "start": "2014-06-17 20:53:37.407237", "stderr": "java version \"1.7.0_60\"\nJava(TM) SE Runtime Environment (build 1.7.0_60-b19)\nJava HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)", "stdout": ""}


PLAY RECAP ******************************************************************** 

192.168.1.50               : ok=2    changed=1    unreachable=0    failed=0   



참고로 현재 나온 ansible 1.5 버전으로는 debug와 tags는 같이 사용할 수 없는 듯 하다. 



4. step

ansible 1.1부터는 step을 사용할 수 있다. 마치 c compile 후 하나씩 실행해보는 stack trace를 찾아가볼 수 있다는 장점이 있다. 


$ ansible-playbook -i hosts playbooks/common/install.yml  --step



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


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

ok: [192.168.1.50]

Perform task: Check if the java compiler exists (y/n/c): 



그리고, 1.2부터는 특정 task 부터 시작할 수 있다. 


$ ansible-playbook -i hosts playbooks/common/install.yml --step --start-at-task='ensuring add-apt-repository is installed'


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


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

ok: [192.168.1.50]

Perform task: ensuring add-apt-repository is installed (y/n/c): 



5. ansible-playbook 디버그 하기


-vvvv 옵션을 추가하면 fail 난 부분에 대한 자세한 정보를 얻어올 수 있다. 리모트 서버에 어떤 옵션을 사용했는지. 어떤 디렉토리에 파일 복사가 일어났는지 확인할 수 있다. 


$ ansible-playbook -i hosts playbook.yml  -vvvv


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


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

<192.168.1.50> ESTABLISH CONNECTION FOR USER: vagrant

<192.168.1.50> REMOTE_MODULE setup

<192.168.1.50> EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/Users/knight/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'Port=22', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=vagrant', '-o', 'ConnectTimeout=10', '192.168.1.50', "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1406524187.02-199513811197131 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1406524187.02-199513811197131 && echo $HOME/.ansible/tmp/ansible-tmp-1406524187.02-199513811197131'"]

<192.168.1.50> PUT /var/folders/nx/lkzmd37d6fj3sg9kg5flt3yr0000gp/T/tmpRc60qB TO /home/vagrant/.ansible/tmp/ansible-tmp-1406524187.02-199513811197131/setup

<192.168.1.50> EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/Users/knight/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'Port=22', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=vagrant', '-o', 'ConnectTimeout=10', '192.168.1.50', u"/bin/sh -c 'LC_CTYPE=en_US.UTF-8 LANG=en_US.UTF-8 /usr/bin/python /home/vagrant/.ansible/tmp/ansible-tmp-1406524187.02-199513811197131/setup; rm -rf /home/vagrant/.ansible/tmp/ansible-tmp-1406524187.02-199513811197131/ >/dev/null 2>&1'"]

ok: [192.168.1.50]


TASK: [test] ****************************************************************** 

<192.168.1.50> ESTABLISH CONNECTION FOR USER: vagrant

<192.168.1.50> REMOTE_MODULE command ls -al /usr/local

<192.168.1.50> EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/Users/knight/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'Port=22', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=vagrant', '-o', 'ConnectTimeout=10', '192.168.1.50', "/bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1406524188.43-148131729811675 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1406524188.43-148131729811675 && echo $HOME/.ansible/tmp/ansible-tmp-1406524188.43-148131729811675'"]

<192.168.1.50> PUT /var/folders/nx/lkzmd37d6fj3sg9kg5flt3yr0000gp/T/tmpSF2T1h TO /home/vagrant/.ansible/tmp/ansible-tmp-1406524188.43-148131729811675/command

<192.168.1.50> EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/Users/knight/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'Port=22', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=vagrant', '-o', 'ConnectTimeout=10', '192.168.1.50', u"/bin/sh -c 'LC_CTYPE=en_US.UTF-8 LANG=en_US.UTF-8 /usr/bin/python /home/vagrant/.ansible/tmp/ansible-tmp-1406524188.43-148131729811675/command; rm -rf /home/vagrant/.ansible/tmp/ansible-tmp-1406524188.43-148131729811675/ >/dev/null 2>&1'"]

changed: [192.168.1.50] => {"changed": true, "cmd": ["ls", "-al", "/usr/local"], "delta": "0:00:00.003512", "end": "2014-07-28 05:09:49.266159", "rc": 0, "start": "2014-07-28 05:09:49.262647", "stderr": "", "stdout": "total 40\ndrwxr-xr-x 10 root root 4096 Sep 14  2012 .\ndrwxr-xr-x 10 root root 4096 Sep 14  2012 ..\ndrwxr-xr-x  2 root root 4096 Sep 14  2012 bin\ndrwxr-xr-x  2 root root 4096 Sep 14  2012 etc\ndrwxr-xr-x  2 root root 4096 Sep 14  2012 games\ndrwxr-xr-x  2 root root 4096 Sep 14  2012 include\ndrwxr-xr-x  3 root root 4096 Sep 14  2012 lib\nlrwxrwxrwx  1 root root    9 Sep 14  2012 man -> share/man\ndrwxr-xr-x  2 root root 4096 Sep 14  2012 sbin\ndrwxr-xr-x  6 root root 4096 Sep 14  2012 share\ndrwxr-xr-x  2 root root 4096 Sep 14  2012 src"}


TASK: [debug var=vagrant.stdout_lines] **************************************** 

<192.168.1.50> ESTABLISH CONNECTION FOR USER: vagrant

ok: [192.168.1.50] => {

    "vagrant.stdout_lines": [

        "total 40", 

        "drwxr-xr-x 10 root root 4096 Sep 14  2012 .", 

        "drwxr-xr-x 10 root root 4096 Sep 14  2012 ..", 

        "drwxr-xr-x  2 root root 4096 Sep 14  2012 bin", 

        "drwxr-xr-x  2 root root 4096 Sep 14  2012 etc", 

        "drwxr-xr-x  2 root root 4096 Sep 14  2012 games", 

        "drwxr-xr-x  2 root root 4096 Sep 14  2012 include", 

        "drwxr-xr-x  3 root root 4096 Sep 14  2012 lib", 

        "lrwxrwxrwx  1 root root    9 Sep 14  2012 man -> share/man", 

        "drwxr-xr-x  2 root root 4096 Sep 14  2012 sbin", 

        "drwxr-xr-x  6 root root 4096 Sep 14  2012 share", 

        "drwxr-xr-x  2 root root 4096 Sep 14  2012 src"

    ]

}


PLAY RECAP ******************************************************************** 

192.168.1.50               : ok=3    changed=1    unreachable=0    failed=0   



Posted by '김용환'
,