From 2e694004611a3ff55490649b5cd3e73a12f9862e Mon Sep 17 00:00:00 2001 From: Michele Cereda Date: Sun, 2 Jun 2024 18:38:21 +0200 Subject: [PATCH] chore(ansible): add alternative to gather users' homedir and use it for something --- snippets/ansible.tasks.yml | 76 ++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 24 deletions(-) diff --git a/snippets/ansible.tasks.yml b/snippets/ansible.tasks.yml index bbf2f90..e6cb4f0 100644 --- a/snippets/ansible.tasks.yml +++ b/snippets/ansible.tasks.yml @@ -81,28 +81,56 @@ programming_languages: "{{ programming_languages + ['Ruby'] }}" - name: "Use the users' home directory for something" - tags: pre-flight block: - - name: "Get raw information from the system's entries" - ansible.builtin.getent: - database: passwd - key: "{{ item }}" - split: ":" - with_items: - - root - - ec2-user - register: users_entries - - name: Compute and register the results - ansible.builtin.set_fact: - users_info: >- - {{ - users_entries - | community.general.json_query('results[].ansible_facts.getent_passwd[]') - | combine - }} - - name: Do your thing! - ansible.builtin.file: - path: "{{ item.value[4] }}/placeholder" - owner: "{{ item.key }}" - state: touch - with_dict: "{{ users_info }}" + - name: Executing commands from specified users + block: + - name: "Get users' homedir back" + become: true + become_user: "{{ item }}" + become_flags: "-iH" + check_mode: false + command: echo {"{{ item }}":"$HOME"} + with_items: + - root + - ec2-user + register: users_homedir_retrieve + - name: Compute and register the results + ansible.builtin.set_fact: + users_homedir: >- + {{ + users_homedir_retrieve + | community.general.json_query('results[].stdout') + | map('from_yaml') + | combine + }} + - name: Do your thing! + ansible.builtin.file: + path: "{{ item.value }}/placeholder" + owner: "{{ item.key }}" + state: touch + with_dict: "{{ users_homedir }}" + - name: "From the system's entries" + block: + - name: "Get raw information from the system's entries" + ansible.builtin.getent: + database: passwd + key: "{{ item }}" + split: ":" + with_items: + - root + - ec2-user + register: users_entries + - name: Compute and register the results + ansible.builtin.set_fact: + users_info: >- + {{ + users_entries + | community.general.json_query('results[].ansible_facts.getent_passwd[]') + | combine + }} + - name: Do your thing! + ansible.builtin.file: + path: "{{ item.value[4] }}/placeholder" + owner: "{{ item.key }}" + state: touch + with_dict: "{{ users_info }}"