{"id":7339,"date":"2018-04-06T09:15:57","date_gmt":"2018-04-06T01:15:57","guid":{"rendered":"http:\/\/rmohan.com\/?p=7339"},"modified":"2018-04-06T09:15:57","modified_gmt":"2018-04-06T01:15:57","slug":"managing-files-in-ansible","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7339","title":{"rendered":"Managing files in ansible"},"content":{"rendered":"<p><strong>Managing files in ansible<\/strong><\/p>\n<p>[root@controller ~]$ ansible localhost \u2013list-hosts<\/p>\n<p>hosts (1):<br \/>\nlocalhost<br \/>\n\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014-<br \/>\n[root@controller ~]$ vim file.yaml<br \/>\n\u2014<br \/>\n\u2013 name: creating a file<br \/>\nhosts: localhost<br \/>\ntasks:<br \/>\n\u2013 file:<br \/>\npath: \/home\/root\/sample<br \/>\nstate: touch<br \/>\nowner: root<br \/>\ngroup: root<br \/>\nmode: 0755<br \/>\n\u2026<br \/>\n\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014-<br \/>\n[root@controller ~]$ ansible-playbook \u2013syntax-check file.yaml<\/p>\n<p>playbook: file.yaml<\/p>\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014-<br \/>\n[root@controller ~]$ ansible-playbook -C file.yaml<\/p>\n<p>PLAY [creating a file] *********************************************************<\/p>\n<p>TASK [setup] *******************************************************************<br \/>\nok: [localhost]<\/p>\n<p>TASK [file] ********************************************************************<br \/>\nchanged: [localhost]<\/p>\n<p>PLAY RECAP *********************************************************************<br \/>\nlocalhost : ok=2 changed=1 unreachable=0 failed=0<\/p>\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014-<br \/>\n[root@controller ~]$ stat \/home\/root\/sample<br \/>\nstat: cannot stat \u2018\/home\/root\/sample\u2019: No such file or directory<br \/>\n\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014-<br \/>\n[root@controller ~]$ ansible-playbook file.yaml<\/p>\n<p>PLAY [creating a file] *********************************************************<\/p>\n<p>TASK [setup] *******************************************************************<br \/>\nok: [localhost]<\/p>\n<p>TASK [file] ********************************************************************<br \/>\nchanged: [localhost]<\/p>\n<p>PLAY RECAP *********************************************************************<br \/>\nlocalhost : ok=2 changed=1 unreachable=0 failed=0<br \/>\n\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014-<br \/>\n[root@controller ~]$ stat \/home\/root\/sample<br \/>\nFile: \u2018\/home\/root\/sample\u2019<br \/>\nSize: 0 Blocks: 0 IO Block: 4096 regular empty file<br \/>\nDevice: fd00h\/64768d Inode: 51300626 Links: 1<br \/>\nAccess: (0333\/\u2013wx-wx-wx) Uid: ( 1000\/ root) Gid: ( 1000\/ root)<br \/>\nContext: unconfined_u:object_r:user_home_t:s0<br \/>\nAccess: 2017-01-26 06:39:51.607557462 +0530<br \/>\nModify: 2017-01-26 06:39:51.607557462 +0530<br \/>\nChange: 2017-01-26 06:39:51.608557462 +0530<br \/>\nBirth: \u2013<br \/>\n\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014-<br \/>\n[root@controller ~]$ ls -l \/home\/root\/sample<br \/>\n\u2013wx-wx-wx. 1 root root 0 Jan 26 06:39 \/home\/root\/sample<br \/>\n\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014-<br \/>\n[root@controller ~]$ ansible-playbook file.yaml<\/p>\n<p>PLAY [creating a file] *********************************************************<\/p>\n<p>TASK [setup] *******************************************************************<br \/>\nok: [localhost]<\/p>\n<p>TASK [file] ********************************************************************<br \/>\nchanged: [localhost]<\/p>\n<p>TASK [stat] ********************************************************************<br \/>\nok: [localhost]<\/p>\n<p>PLAY RECAP *********************************************************************<br \/>\nlocalhost : ok=3 changed=1 unreachable=0 failed=0<br \/>\n\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013<br \/>\n[root@controller ~]$ vim file.yaml<br \/>\n\u2014<br \/>\n\u2013 name: creating a file<br \/>\nhosts: localhost<br \/>\ntasks:<br \/>\n\u2013 file:<br \/>\npath: \/home\/root\/sample<br \/>\nstate: touch<br \/>\nowner: root<br \/>\ngroup: root<br \/>\nmode: 0755<br \/>\n\u2013 stat: path=\/home\/root\/sample<br \/>\nregister: file_status<br \/>\n\u2013 debug: msg=\u201dFile exists\u201d<br \/>\nwhen: file_status.stat.exists == true<br \/>\n\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013<br \/>\n[root@controller ~]$ ansible-playbook file.yaml<\/p>\n<p>PLAY [creating a file] *********************************************************<\/p>\n<p>TASK [setup] *******************************************************************<br \/>\nok: [localhost]<\/p>\n<p>TASK [file] ********************************************************************<br \/>\nchanged: [localhost]<\/p>\n<p>TASK [stat] ********************************************************************<br \/>\nok: [localhost]<\/p>\n<p>TASK [debug] *******************************************************************<br \/>\nok: [localhost] =&gt; {<br \/>\n\u201cmsg\u201d: \u201cFile exists\u201d<br \/>\n}<\/p>\n<p>PLAY RECAP *********************************************************************<br \/>\nlocalhost : ok=4 changed=1 unreachable=0 failed=0<\/p>\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013<br \/>\n[root@controller ~]$ vim file.yaml<br \/>\n\u2014<br \/>\n\u2013 name: creating a file<br \/>\nhosts: localhost<br \/>\ntasks:<br \/>\n\u2013 file:<br \/>\npath: \/home\/root\/sample<br \/>\nstate: touch<br \/>\nowner: root<br \/>\ngroup: root<br \/>\nmode: 0755<br \/>\n\u2013 stat: path=\/home\/root\/sample<br \/>\nregister: file_status<br \/>\n\u2013 debug: msg=\u201dFile exists\u201d<br \/>\nwhen: file_status.stat.exists == true<br \/>\n\u2013 copy: content=\u201dthis is for test purpose\\n\u201d dest=\u201d\/home\/root\/sample\u201d<br \/>\nwhen: file_status.stat.exists == true<\/p>\n<p>[root@controller ~]$ ansible-playbook -C file.yaml<\/p>\n<p>PLAY [creating a file] *********************************************************<\/p>\n<p>TASK [setup] *******************************************************************<br \/>\nok: [localhost]<\/p>\n<p>TASK [file] ********************************************************************<br \/>\nchanged: [localhost]<\/p>\n<p>TASK [stat] ********************************************************************<br \/>\nok: [localhost]<\/p>\n<p>TASK [debug] *******************************************************************<br \/>\nok: [localhost] =&gt; {<br \/>\n\u201cmsg\u201d: \u201cFile exists\u201d<br \/>\n}<\/p>\n<p>TASK [copy] ********************************************************************<br \/>\nchanged: [localhost]<\/p>\n<p>PLAY RECAP *********************************************************************<br \/>\nlocalhost : ok=5 changed=2 unreachable=0 failed=0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Managing files in ansible<\/p>\n<p>[root@controller ~]$ ansible localhost \u2013list-hosts<\/p>\n<p>hosts (1): localhost \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014- [root@controller ~]$ vim file.yaml \u2014 \u2013 name: creating a file hosts: localhost tasks: \u2013 file: path: \/home\/root\/sample state: touch owner: root group: root mode: 0755 \u2026 \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014- [root@controller ~]$ ansible-playbook \u2013syntax-check file.yaml<\/p>\n<p>playbook: file.yaml<\/p>\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014- [root@controller ~]$ ansible-playbook -C file.yaml<\/p>\n<p>PLAY [creating [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[91],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7339"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7339"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7339\/revisions"}],"predecessor-version":[{"id":7340,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7339\/revisions\/7340"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}