Synchronize a directory structure with Ansible

Disclaimer: this is not ideal. We should manage the whole configuration with Ansible. “Baby steps” I guess… :)
Consider this as a workaround I hope you’ll never have to resort to, but I’m sharing it just in case…

We’re migrating from some old scripts to using Ansible to handle some of our clients deploys.

One of the tasks that were handled by these bash scripts is to synchronize a directory structure, so that the application log files would always find the same directory structure on every application server.

We used rsync for that, copying only the directories:

rsync -av -f"+ */" -f"- *" /path/to/app/ $target:/path/to/app/

To translate this to Ansible we used two tasks:

- name: Deploy log directories
    dir_log_path: /var/log/nginx
  hosts: webservers
  serial: 10%
  - name: find log directories
      - '{{ dir_log_path }}'
      file_type: directory
    register: log_dirs
    delegate_to: ws-deploy

  - name: create log directories
      path: "{{ item.path }}"
      state: directory
      owner: "{{ item.uid }}"
      group: "{{ item.gid }}"
      mode: "{{ item.mode }}"
    with_items: "{{ log_dirs.files }}"

We record in the log_dirs variable the directories existing on ws-deploy, the server where we have the latest configuration loaded on, then we recreate the same structure using the file module in Ansible on all the other webservers.


Ansible: how to skip a specific host or group in a playbook

If you search the Ansible documentation, you’ll probably end up using a when in your playbooks to skip a host.

I found out that it makes more sense to me to skip hosts from the hosts declaration, by prepending them with a !:

- name: check on which hosts this would run
  remote_user: root
  hosts: all !bastion
  - name: say hello
      msg: "hello {{ ansible_hostname }}"

My hosts file looks like:





Running the playbook with the usual commandline:

ansible-playbook -i hosts playbook.yaml

produces the expected result: the debug module is only run on webservers (ws) and databases (db), not on the bastion hosts (bastion).

I hope this will help some other sysadmin out there :)