{"id":1289,"date":"2024-04-27T16:46:12","date_gmt":"2024-04-27T16:46:12","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1289"},"modified":"2024-04-27T16:51:27","modified_gmt":"2024-04-27T16:51:27","slug":"ansible-register-store-and-display-output-of-a-command","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1289","title":{"rendered":"Ansible: Register &#8211; store and display output of a command"},"content":{"rendered":"<p>At times we may want to run certain command in the managed nodes, and would like to see the output to verify if the command ran correctly. The module commonly used to run commands on managed nodes is &#8220;ansible.builtin.command&#8221;. However when we execute a command using this module the output wont be displayed as we run the playbook. The workaround is to capture the output using &#8220;register&#8221; parameter.<\/p>\n<p>Below is an example where a user is created using &#8220;user&#8221; module, and a task is added to list the contents in the user&#8217;s home folder.<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat adduser_playbook.yml<\/p>\n<p>==== ==== =<br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>&#8211; name: Playbook to add a new user<\/em><br \/>\n<em>&nbsp; &nbsp; hosts: 192.168.48.129<\/em><br \/>\n<em>&nbsp; &nbsp; vars:<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp; theName: shiju<\/em><br \/>\n<em>&nbsp; &nbsp; tasks:<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Task to add a new user<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.user:<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: &#8220;{{ theName }}&#8221;<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; groups: developers<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; append: true<\/em><\/p>\n<p><em>&nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Tast to check information in user home folder<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.command:<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd: &#8220;ls -lA \/home\/{{ theName }}&#8221;<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong>register<\/strong>: results<\/em><\/p>\n<p><em>&nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Display the listing of home directory<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.debug:<\/em><br \/>\n<em>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var: results.stdout<\/em><\/p>\n<p>=== === ==<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout adduser_playbook.yml<\/p>\n<p>==== === =<\/p>\n<p><em>PLAY [Playbook to add a new user] **********************************************<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Task to add a new user] **************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Tast to check information in user home folder] ***************************<\/em><br \/>\n<em>changed: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Display the listing of home directory] ***********************************<\/em><br \/>\n<em>ok: [192.168.48.129] =&gt; {<\/em><br \/>\n<em> &#8220;results.stdout&#8221;: &#8220;total 12\\n<strong>-rw-r&#8211;r&#8211;. 1 shiju shiju 18 Nov 24 2022 .bash_logout\\n<\/strong>-rw-r&#8211;r&#8211;. 1 shiju shiju 141 Nov 24 2022 .bash_profile\\n<strong>-rw-r&#8211;r&#8211;. 1 shiju shiju 492 Nov 24 2022 .bashrc\\n<\/strong>-rw-r&#8211;r&#8211;. 1 root root 0 Apr 27 21:08 samkutty.txt&#8221;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><em>PLAY RECAP *********************************************************************<\/em><br \/>\n<em>192.168.48.129 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/em><\/p>\n<p>=== === ==<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>At times we may want to run certain command in the managed nodes, and would like to see the output to verify if the command <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1289\" title=\"Ansible: Register &#8211; store and display output of a command\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,21],"tags":[],"class_list":["post-1289","post","type-post","status-publish","format-standard","hentry","category-ansible","category-devops"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1289"}],"version-history":[{"count":7,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1289\/revisions"}],"predecessor-version":[{"id":1296,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1289\/revisions\/1296"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1289"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}