Ansible no doubt the best Infrastructure Automation tool out there which is used by top fortune 500 companies world wide.
On Crunchify, we have published a wide verity of tutorials on Ansible. Kindly take a look at Ansible Archive for all detailed tutorials. Here are top 3 tutorials.
- How to copy File, Directory or Script from localhost to Remote host?
- How to Spawn Amazon EC2 VMs using Ansible?
- How to Execute Commands on remote Hosts and get command result (log) back?
In this tutorial we will go over 3 different tips which I uses day to day bases.
How to Delete and Re-create a file using Ansible?
Step-1
Make sure you have setup Ansible correctly on your Mac or Linux OS.
Step-2
Create file crunchify-delete-recreate-ansible.yml
--- - name: Delete and Re-Create crunchify.txt file from current directory. hosts: local connection: local gather_facts: True tasks: - name: delete file ignore_errors: yes file: state: absent path: crunchify.txt - name: Ansible create file if it doesn't exist example ignore_errors: yes file: path: "crunchify.txt" state: touch
Step-3
Open hosts
file and put below content:
[local] localhost ansible_connection=local ansible_python_interpreter=python
Step-4
Run Ansible playbook
.
ansible-playbook -vvv -i ./hosts crunchify-delete-recreate-ansible.yml
Here is a result:
WARNING: Executing a script that is loading libcrypto in an unsafe way. This will fail in a future version of macOS. Set the LIBRESSL_REDIRECT_STUB_ABORT=1 in the environment to force this into an error. ansible-playbook 2.7.8 config file = None configured module search path = [u'/Users/crunchify/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /Library/Python/2.7/site-packages/ansible executable location = /usr/local/bin/ansible-playbook python version = 2.7.16 (default, Jul 14 2019, 03:47:49) [GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.32.4) (-macos10.15-objc-s No config file found; using defaults /Users/crunchify/Documents/ansible/hosts did not meet host_list requirements, check plugin documentation if this is unexpected /Users/crunchify/Documents/ansible/hosts did not meet script requirements, check plugin documentation if this is unexpected Parsed /Users/crunchify/Documents/ansible/hosts inventory source with ini plugin PLAYBOOK: crunchify-delete-recreate-ansible.yml **************************************************************************************************************** 1 plays in crunchify-delete-recreate-ansible.yml PLAY [Delete and Re-Create crunchify.txt file from current directory.] ***************************************************************************************** TASK [Gathering Facts] ***************************************************************************************************************************************** task path: /Users/crunchify/Documents/ansible/crunchify-delete-recreate-ansible.yml:2 <localhost> ESTABLISH LOCAL CONNECTION FOR USER: crunchify <localhost> EXEC /bin/sh -c 'echo ~crunchify && sleep 0' <localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/crunchify/.ansible/tmp/ansible-tmp-1567010309.76-127517206399779 `" && echo ansible-tmp-1567010309.76-127517206399779="` echo /Users/crunchify/.ansible/tmp/ansible-tmp-1567010309.76-127517206399779 `" ) && sleep 0' Using module file /Library/Python/2.7/site-packages/ansible/modules/system/setup.py <localhost> PUT /Users/crunchify/.ansible/tmp/ansible-local-36562FsxMwP/tmp5SGRSa TO /Users/crunchify/.ansible/tmp/ansible-tmp-1567010309.76-127517206399779/AnsiballZ_setup.py <localhost> EXEC /bin/sh -c 'chmod u+x /Users/crunchify/.ansible/tmp/ansible-tmp-1567010309.76-127517206399779/ /Users/crunchify/.ansible/tmp/ansible-tmp-1567010309.76-127517206399779/AnsiballZ_setup.py && sleep 0' <localhost> EXEC /bin/sh -c 'python /Users/crunchify/.ansible/tmp/ansible-tmp-1567010309.76-127517206399779/AnsiballZ_setup.py && sleep 0' <localhost> EXEC /bin/sh -c 'rm -f -r /Users/crunchify/.ansible/tmp/ansible-tmp-1567010309.76-127517206399779/ > /dev/null 2>&1 && sleep 0' ok: [localhost] META: ran handlers TASK [delete file] ********************************************************************************************************************************************* task path: /Users/crunchify/Documents/ansible/crunchify-delete-recreate-ansible.yml:8 <localhost> ESTABLISH LOCAL CONNECTION FOR USER: crunchify <localhost> EXEC /bin/sh -c 'echo ~crunchify && sleep 0' <localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.35-144146434104750 `" && echo ansible-tmp-1567010310.35-144146434104750="` echo /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.35-144146434104750 `" ) && sleep 0' Using module file /Library/Python/2.7/site-packages/ansible/modules/files/file.py <localhost> PUT /Users/crunchify/.ansible/tmp/ansible-local-36562FsxMwP/tmpvL0NpJ TO /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.35-144146434104750/AnsiballZ_file.py <localhost> EXEC /bin/sh -c 'chmod u+x /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.35-144146434104750/ /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.35-144146434104750/AnsiballZ_file.py && sleep 0' <localhost> EXEC /bin/sh -c 'python /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.35-144146434104750/AnsiballZ_file.py && sleep 0' <localhost> EXEC /bin/sh -c 'rm -f -r /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.35-144146434104750/ > /dev/null 2>&1 && sleep 0' changed: [localhost] => { "changed": true, "diff": { "after": { "path": "crunchify.txt", "state": "absent" }, "before": { "path": "crunchify.txt", "state": "file" } }, "invocation": { "module_args": { "_diff_peek": null, "_original_basename": null, "access_time": null, "access_time_format": "%Y%m%d%H%M.%S", "attributes": null, "backup": null, "content": null, "delimiter": null, "directory_mode": null, "follow": true, "force": false, "group": null, "mode": null, "modification_time": null, "modification_time_format": "%Y%m%d%H%M.%S", "owner": null, "path": "crunchify.txt", "recurse": false, "regexp": null, "remote_src": null, "selevel": null, "serole": null, "setype": null, "seuser": null, "src": null, "state": "absent", "unsafe_writes": null } }, "path": "crunchify.txt", "state": "absent" } TASK [Ansible create file if it doesn't exist example] ********************************************************************************************************* task path: /Users/crunchify/Documents/ansible/crunchify-delete-recreate-ansible.yml:14 <localhost> ESTABLISH LOCAL CONNECTION FOR USER: crunchify <localhost> EXEC /bin/sh -c 'echo ~crunchify && sleep 0' <localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.67-150889598387355 `" && echo ansible-tmp-1567010310.67-150889598387355="` echo /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.67-150889598387355 `" ) && sleep 0' Using module file /Library/Python/2.7/site-packages/ansible/modules/files/file.py <localhost> PUT /Users/crunchify/.ansible/tmp/ansible-local-36562FsxMwP/tmpim0wPm TO /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.67-150889598387355/AnsiballZ_file.py <localhost> EXEC /bin/sh -c 'chmod u+x /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.67-150889598387355/ /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.67-150889598387355/AnsiballZ_file.py && sleep 0' <localhost> EXEC /bin/sh -c 'python /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.67-150889598387355/AnsiballZ_file.py && sleep 0' <localhost> EXEC /bin/sh -c 'rm -f -r /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.67-150889598387355/ > /dev/null 2>&1 && sleep 0' changed: [localhost] => { "changed": true, "dest": "crunchify.txt", "diff": { "after": { "atime": 1567010310.795358, "mtime": 1567010310.795358, "path": "crunchify.txt", "state": "touch" }, "before": { "atime": 1567010310.795286, "mtime": 1567010310.795286, "path": "crunchify.txt", "state": "absent" } }, "gid": 20, "group": "staff", "invocation": { "module_args": { "_diff_peek": null, "_original_basename": null, "access_time": null, "access_time_format": "%Y%m%d%H%M.%S", "attributes": null, "backup": null, "content": null, "delimiter": null, "directory_mode": null, "follow": true, "force": false, "group": null, "mode": null, "modification_time": null, "modification_time_format": "%Y%m%d%H%M.%S", "owner": null, "path": "crunchify.txt", "recurse": false, "regexp": null, "remote_src": null, "selevel": null, "serole": null, "setype": null, "seuser": null, "src": null, "state": "touch", "unsafe_writes": null } }, "mode": "0644", "owner": "crunchify", "size": 0, "state": "file", "uid": 502 } META: ran handlers META: ran handlers PLAY RECAP ***************************************************************************************************************************************************** localhost : ok=3 changed=2 unreachable=0 failed=0
And that’s it. Above Ansible playbook will delete an existing file named crunchify.txt
and will create a new one with empty content.
As usual, let us know for any query or any problem running above Ansible Playbook.