i managed to come up with this, but seems like it could be cleaner

---
- name: copy pam etc/security/access.conf file
  vars:
    findme: |
      {%- set findme = [] -%}
      {%- for groupn in group_names -%}
        {{- findme.append('files/' + item.src + '_AG' + groupn) -}}
      {%- endfor -%}
      {{- findme.append("files/"+item.src+"_AH"+ansible_hostname) -}}
      {{- findme.append("files/"+item.src) -}}
      {{- findme | list -}}
#  debug:
#    msg: "{{ lookup('ansible.builtin.first_found', findme) }}"
  ansible.builtin.copy:
    src: "{{ lookup('ansible.builtin.first_found', findme) }}"
    dest: "/{{item.src}}"
    owner: "{{item.owner}}"
    group: "{{item.group}}"
    mode: "{{item.mode}}"
  with_items:
    - { src: "etc/security/access.conf", owner: "root", group: "root",
mode: "0644" }

On Tue, Apr 18, 2023 at 11:09 AM Michael DiDomenico
<[email protected]> wrote:
>
> the below block is an example block i use in a few places to copy in
> config files and select a host specific file if it exists.  not sure
> if it's the best way, but it works for now.  what i'd like to do is
> add in group selection as well.  ie if there's group file look for
> that first
>
> so above line 5 you could have
> "files/{{ansible_local.baseos.ver}}/{{item.src}}_AG{{group}}"  but
> clearly that wont work because there likely is more then on group
> attached to a host.  so i need to try all the groups of a host and see
> if there's a matching file.  the only way i can think to do it is to
> create a second task that looks in the repository for a matching group
> file and then registers a variable which i can include in the below
> block above line 5
>
> is there a better way?
>
>   1 ---
>   2 - name: copy pam etc/security/access.conf file
>   3   vars:
>   4     findme:
>   5       - "files/{{item.src}}_AH{{ansible_hostname}}"
>   6       - "files/{{item.src}}"
>   7   ansible.builtin.copy:
>   8     src: "{{ lookup('ansible.builtin.first_found', findme) }}"
>   9     dest: "/{{item.src}}"
>  10     owner: "{{item.owner}}"
>  11     group: "{{item.group}}"
>  12     mode: "{{item.mode}}"
>  13   with_items:
>  14     - { src: "etc/security/access.conf", owner: "root", group:
> "root", mode: "0644" }

-- 
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/CABOsP2P4XX2iNZODMfYSAJnhBZUJXFmhyVe2HigxfW-Rhd_Uog%40mail.gmail.com.

Reply via email to