Mirek
Mirek

Reputation: 407

Resursively sync files and links to multiple users, preserving permissions, but not owner and group

I have a directory with several tools and text files that I would like to copy to multiple users homedirs on given host. There are couple of caveats:

When pushing to ansible_user home dir, there's no issue as ansible.posix.synchronize does the job very well with archive=true and owner/group set to false:

- name: Sync testing_files repo to ansible_user
  ansible.posix.synchronize:
    src: ~/testing_files/
    dest: ~/testing_files/
    mode: push
    archive: true
    delete: true
    owner: false
    group: false
  register: rsync_output

The links are properly handled as well (they are rsynced as symlinks).

However, the problem is with populating the same for other users. I tried the following approach:

- name: Sync testing_files repo to extra_users
  ansible.builtin.copy:
    src: ~/testing_files/
    dest: ~{{ item }}/testing_files/
    remote_src: true
    mode: preserve
    owner: "{{ item }}"
    follow: true  # I tried with `false` as well
  with_items:
    - "{{ extra_users if extra_users is not none else [] }}"

The file permissions are correct, the owner as well, however:

How can I make it work? For the group issue the only solution I came up is to have another task that will run stat to check group and save it for future use, e.g. like this:

- name: Get group of homedir
  ansible.builtin.stat:
    path: "~{{ item }}"
  register: homedir
  with_items:
    - "{{ extra_users_or_empty }}"
- name: Sync testing_files repo to extra_users
  ansible.builtin.copy:
    src: ~/testing_files/
    dest: "~{{ item }}/testing_files/"
    remote_src: true
    mode: preserve
    owner: "{{ item }}"
    group: "{{ homedir.results | selectattr('item', 'eq', item) | map(attribute='stat.gr_name') | first }}"
    follow: true
  with_items:
    - "{{ extra_users_or_empty }}"

(NOTE: extra_users_or_empty: "{{ extra_users if extra_users is not none else [] }}")

However that feels like something that should be achieved in more elegant way. And for symlinks - I have no idea why the ansible.builtin.copy ignores them.

Any ideas?

Upvotes: 3

Views: 589

Answers (1)

Mirek
Mirek

Reputation: 407

Huh, ok, seems like when we're using remote_src, we should set local_follow, instead of follow. The following solution handles symlinks properly:

    - name: Get group of homedir
      ansible.builtin.stat:
        path: "~{{ item }}"
      register: homedir
      with_items:
        - "{{ extra_users_or_empty }}"
    - name: Sync testing_files repo to extra_users
      ansible.builtin.copy:
        src: ~/testing_files/
        dest: "~{{ item }}/testing_files/"
        remote_src: true
        mode: preserve
        owner: "{{ item }}"
        group: "{{ homedir.results | selectattr('item', 'eq', item) | map(attribute='stat.gr_name') | first }}"
        local_follow: false
      with_items:
        - "{{ extra_users_or_empty }}"

Upvotes: 3

Related Questions