In this session, I am going to talk about command module.
1 2 3 tasks: - name: test command module command: cat /etc/hosts
the playbook is pretty simple, let’s look at output
1 2 3 4 5 6 7 8 9 10 11 anna@ansible-controller:~/Desktop/ansible-code/inventory/modules/command-modules$ ansible-playbook site.yml PLAY [test command module] ****************************************************** TASK [test command module] ****************************************************** changed: [ansible-node1] changed: [cube4200] PLAY RECAP ********************************************************************** ansible-node1 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 cube4200 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
It shows successfully exectute ansible tasks, but I didn’t see the context of /etc/hosts.
If I want to see the output, I am supposed to let output to be a variable, it looks like following:
1 2 3 4 5 6 7 tasks: - name: test command module command: cat /etc/hosts register: output - name: print the output of command debug: msg: "{{ output }}"
after adding register, I can see the /etc/hosts now.
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 anna@ansible-controller:~/Desktop/ansible-code/inventory/modules/command-modules$ ansible-playbook site.yml PLAY [test command module] ****************************************************** TASK [test command module] ****************************************************** changed: [ansible-node1] changed: [cube4200] TASK [print the output of command] ********************************************** ok: [ansible-node1] => { "msg": { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "cmd": [ "cat", "/etc/hosts" ], "delta": "0:00:00.013819", # this is the time of exectute this ansible task "end": "2020-06-03 04:02:04.391584", "failed": false, "rc": 0, "start": "2020-06-03 04:02:04.377765", "stderr": "", "stderr_lines": [], "stdout": "127.0.0.1\tlocalhost\n::1\t\tlocalhost ", "stdout_lines": [ "127.0.0.1\tlocalhost", ], "warnings": [ "Platform linux on host ansible-node1 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information." ] } } ok: [cube4200] => { "msg": { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "cmd": [ "cat", "/etc/hosts" ], "delta": "0:00:00.011817", "end": "2020-06-02 21:02:05.042380", "failed": false, "rc": 0, "start": "2020-06-02 21:02:05.030563", "stderr": "", "stderr_lines": [], "stdout": "127.0.0.1 localhost localhost.localdomain localhost4 ", "stdout_lines": [ "127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4", ] } } PLAY RECAP ********************************************************************** ansible-node1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 cube4200 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
In this output I can see a lot of default return value of command modules, for example, rc, stderr, stdout etc, but delta, start and end are command module’s specific return values.