I am new to Ansible and facing the following issue in one of my playbooks
Ansible playbook task code:
- name: Delete existing dist folder
shell: "[ -d {{ base_path }}/dist ] && rm -rf {{ base_path }}/dist"
Output:
fatal: [masked.amazonaws.com]: FAILED! => {
"changed": true,
"cmd": "[ -d /home/centos/masked/masked/dist ] && rm -rf /home/centos/masked/masked/dist",
"delta": "0:00:00.098034",
"end": "2018-09-26 20:26:06.940872",
"invocation": {
"module_args": {
"_raw_params": "[ -d /home/centos/masked/masked/dist ] && rm -rf /home/centos/masked/masked/dist",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"warn": true
}
},
"msg": "non-zero return code",
"rc": 1,
"start": "2018-09-26 20:26:06.842838",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
If I run this command manually, it works just fine
[ -d /home/centos/masked/masked/dist ] && rm -rf
/home/centos/masked/masked/dist
Does anyone have any idea what could be wrong?
Appreciate your responses.
Thanks in advance.
Delete a file if it exists You can delete a file by setting state to absent . If the file doesn't exist, Ansible will do nothing.
Ansible Create Directory If Not Exists:-DecodingDevOps To create a directory in ansible we use ansible file module with tag name state as directory. To check the directory is existed or not, we use ansible stat module. In the following example i will show you how to create a directory in ansible if not exists.
recurse. boolean. added in 1.1 of ansible.builtin. Recursively set the specified file attributes on directory contents. This applies only when state is set to directory .
In an Ansible playbook, when local_action is used, Ansible will run the module work mentioned under it on the controller node. Mostly modules used with local_action are shell and command. As you can do almost all the tasks using these modules on the controller node.
Ansible cannot automatically remove packages when you remove them from you playbook. Ansible is stateless and will only do what you explicitly describe in the playbook. So you have to write a task to remove it. You can easily do this with the apt module.
How to Delete and Re-create a file using Ansible? Make sure you have setup Ansible correctly on your Mac or Linux OS. Create file crunchify-delete-recreate-ansible.yml - name: Delete and Re - Create crunchify.txt file from current directory. Run Ansible playbook. WARNING: Executing a script that is loading libcrypto in an unsafe way.
This tutorial covers how to use the stat module in Ansible to check if files and folders exist on remote hosts. The easiest way to check if a file exists using Ansible is with the stat module. The purpose of the stat module is to retrieve facts about files and folders and record them in a register.
Ansible is an Infrastructure as Code tool that lets a single control node monitor and manage a large number of remote hosts (servers). Ansible uses playbooks to define a variety of tasks for the remote hosts to perform, including checking if files and folders exist.
Use the file
module to delete the directory, instead of shell
:
- name: Delete existing dist folder
file:
path: "{{ base_path }}/dist"
state: absent
According to the documentation, it is very close to rm -rf
:
If
absent
, directories will be recursively deleted, and files or symlinks will be unlinked. Note that absent will not cause file to fail if the path does not exist as the state did not change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With