From be60d7a3d04a1c31282268e44c94c1f6d68d2f64 Mon Sep 17 00:00:00 2001 From: Michele Cereda Date: Sun, 2 Jun 2024 18:00:35 +0200 Subject: [PATCH] chore(ansible): gather users' homedir and use it for something --- knowledge base/ansible.md | 6 +++++- snippets/ansible.tasks.yml | 38 ++++++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/knowledge base/ansible.md b/knowledge base/ansible.md index b838495..02ea83c 100644 --- a/knowledge base/ansible.md +++ b/knowledge base/ansible.md @@ -678,6 +678,8 @@ See [Integrate with AWS SSM]. - [Ansible: set variable to file content] - [How can I hide skipped tasks output in Ansible] - [Ansible roles: basics, creating & using] +- [Developing and Testing Ansible Roles with Molecule and Podman - Part 1] +- [How to get an arbitrary remote user's home directory in Ansible?] [ansible roles: basics, creating & using]: https://spacelift.io/blog/ansible-roles @@ -713,6 +716,7 @@ See [Integrate with AWS SSM]. [edit .ini file in other servers using ansible playbook]: https://syslint.com/blog/tutorial/edit-ini-file-in-other-servers-using-ansible-playbook/ [how can i hide skipped tasks output in ansible]: https://stackoverflow.com/questions/39189549/how-can-i-hide-skipped-tasks-output-in-ansible#76147924 [how to append to lists]: https://blog.crisp.se/2016/10/20/maxwenzin/how-to-append-to-lists-in-ansible +[how to get an arbitrary remote user's home directory in ansible?]: https://stackoverflow.com/questions/33343215/how-to-get-an-arbitrary-remote-users-home-directory-in-ansible#45447488 [how to install sshpass on mac]: https://stackoverflow.com/questions/32255660/how-to-install-sshpass-on-mac/62623099#62623099 [how to recursively set directory and file permissions]: https://superuser.com/questions/1024677/ansible-how-to-recursively-set-directory-and-file-permissions#1317715 [how to set up and use python virtual environments for ansible]: https://www.redhat.com/sysadmin/python-venv-ansible diff --git a/snippets/ansible.tasks.yml b/snippets/ansible.tasks.yml index abb86a8..bbf2f90 100644 --- a/snippets/ansible.tasks.yml +++ b/snippets/ansible.tasks.yml @@ -1,7 +1,6 @@ --- -# Directories are created recursively. -- name: Create a whole directory tree +- name: Create directories recursively ansible.builtin.file: path: /tmp/path/to/final/dir state: directory @@ -10,10 +9,10 @@ block: - name: By using absolute paths and special variables (preferred) ansible.builtin.import_tasks: - file: "{{ role_path }}/tasks/install/{{ install_method }}.yml" + file: "{{ role_path }}/tasks/install/{{ install_method }}.yml" - name: By using paths relative to the including file ansible.builtin.import_tasks: - file: pre-flight.yml + file: pre-flight.yml - name: Conditionally include tasks block: @@ -76,3 +75,34 @@ - ./logs:/var/log/gitlab:Z - ./data:/var/opt/gitlab:Z auto_remove: true + +- name: Add elements to lists + set_fact: + 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 }}"