Sorry for resurrecting this old thread.
I shuffled things around a bit lately, and came up with a single
playbook file that can be used to template a directory tree. See
below.

---
# Template a directory structure
# Include this task like this example, supplying at least
# a 'src' and 'dest' variable:
#
# - include_tasks: template_tree.yml
#   vars:
#     src: "{{ playbook_dir }}/app1/config"
#     dest: /etc/app1
#
# Optional variables:
#     validate: /usr/bin/chckconfig %s
#     cleanup: true


- name: Find all local content
  become: false
  connection: local
  find:
    paths: "{{ src }}"
    recurse: true
    hidden: true
    file_type: any
  register: _local


- name: Ensure directories are created
  file:
    path: "{{ dest ~ '/' ~ ( item.path | relpath(src))  }}"
    state: directory
    mode: "{{ item.mode }}"
  loop: "{{ _local | json_query('files[?isdir]') }}"
  loop_control:
    label: "{{ item.path | relpath(src) }}"


- name: Ensure files are templated
  template:
    src: '{{ item.path }}'
    dest: "{{ dest ~ '/' ~ ( item.path | relpath(src))  }}"
    mode: '{{ item.mode }}'
    validate: "{{ validate | default(omit) }}"
  loop: "{{ _local | json_query('files[?!isdir]') }}"
  loop_control:
    label: "{{ item.path | relpath(src) }}"


- name: Remove orphan logic
  block:
    - name: Find all remote content
      find:
        paths: "{{ dest }}"
        recurse: true
        file_type: any
        hidden: true
      register: _remote

    - name: Clean up orphaned files/directories
      file:
        path: "{{ dest }}/{{ item }}"
        state: absent
      loop: "{{ _remote | json_query('files[].path') | map('relpath',
dest) | list |
        difference( _local | json_query('files[].path') |
map('relpath', src) | list) }}"

  when: cleanup|default(false)|bool




I'm using this in
https://github.com/dnmvisser/ansible_role_simplesamlphp/blob/master/tasks/template_tree.yml









On Tue, 12 Nov 2019 at 15:30, Brian Coca <[email protected]> wrote:
>
> I once had a similar problem and I solved it in also a similar way,
> then made it into a role https://galaxy.ansible.com/bcoca/tidy
>
> --
> ----------
> Brian Coca
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/ansible-project/CACVha7fReFrLSBnEOKUv9Ah4U9eYVkmRu_kN9hRqw2iUuS6aKQ%40mail.gmail.com.



-- 
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAL8fbwO8%2BwKMHDDYDtoDxnB%3D3CFdgKzFaM3iQKjzpRJ4PECUsw%40mail.gmail.com.

Reply via email to