Ansible: How to delete files and folders inside a directory?
– name: Delete content & directory file: state: absent path: /home/mydata/web/ Note: this will delete the directory too.
– name: Delete content & directory file: state: absent path: /home/mydata/web/ Note: this will delete the directory too.
Two options – the first, as you said in your own answer, is setting the environment variable ANSIBLE_HOST_KEY_CHECKING to False. The second way to set it is to put it in an ansible.cfg file, and that’s a really useful option because you can either set that globally (at system or user level, in /etc/ansible/ansible.cfg or … Read more
You should use tags: as documented in https://docs.ansible.com/ansible/latest/user_guide/playbooks_tags.html If you have a large playbook it may become useful to be able to run a specific part of the configuration without running the whole playbook. Both plays and tasks support a “tags:” attribute for this reason. Example: tasks: – yum: name={{ item }} state=installed with_items: – … Read more
From version 2.0, in copy module you can use remote_src parameter. If True it will go to the remote/target machine for the src. – name: Copy files from foo to bar copy: remote_src=True src=/path/to/foo dest=/path/to/bar If you want to move file you need to delete old file with file module – name: Remove old files … Read more
Turns out it is possible to enter a host name directly into the playbook, so running the playbook with hosts: imac-2.local will work fine. But it’s kind of clunky. A better solution might be defining the playbook’s hosts using a variable, then passing in a specific host address via –extra-vars: # file: user.yml (playbook) — … Read more
The docs strongly recommend against setting the sudo password in plaintext: As a reminder passwords should never be stored in plain text. For information on encrypting your passwords and other secrets with Ansible Vault, see Encrypting content with Ansible Vault. Instead you should be using –ask-become-pass on the command line when running ansible-playbook Previous versions … Read more
Reading the docs I find the section Passing Variables On The Command Line, that gives this example: ansible-playbook release.yml –extra-vars “version=1.23.45 other_variable=foo” Others examples demonstrate how to load from JSON string (≥1.2) or file (≥1.3)
Yes, you can run commands on the Ansible host. You can specify that all tasks in a play run on the Ansible host, or you can mark individual tasks to run on the Ansible host. If you want to run an entire play on the Ansible host, then specify hosts: 127.0.0.1 and connection:local in the … Read more
You want the file module. To create a directory, you need to specify the option state=directory : – name: Creates directory file: path: /src/www state: directory You can see other options at https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html