I am new to Ansible and I am trying to create an Ansible Playbook, which makes a backup of directories from different hosts to the control server (which is the same machine where Ansible is invoked).
I would like to create directories on the control server first and then do a synchronize (ansible.posix.synchronize
) or copy (ansible.builtin.copy
, ansible.builtin.fetch
) command to copy all the directories from all hosts to the control server.
To create a local directory:
- name: Create local directory
ansible.builtin.file:
path: /path/to/new/directory
state: directory
register: local_dir
delegate_to: localhost
See ansible.builtin.file – Manage files and file properties for details on the ansible.builtin.file
module.
The path can be relative or absolute.
The key to doing it on the Ansible controller is the last line (delegate_to: localhost
).
For more information see Delegation, Rolling Updates, and Local Actions.
For copying data from the remote to the local machine, use the ansible.posix.synchronize
module.
See ansible.posix.synchronize – A wrapper around rsync to make common tasks in your playbooks quick and easy for details.
By default, it will copy from local to remote (default mode: push
), but you can use mode: pull
to reverse this.
Note: You need to create the base directory first locally (dest: "{{ local_dir.path }}"
), but the synchronize
module will create the necessary directories beneath it (standard rsync(1)
functionality).
Finally, I'll include below my list of Ansible introductory videos and other resources I came across. The star ratings are my personal opinion of the resources. Keep in mind that a lot has changed in Ansible in the last 2-3 years.
I hope you enjoy learning about Ansible. I certainly have!