chore(ansible): gather users' homedir and use it for something

This commit is contained in:
Michele Cereda
2024-06-02 18:00:35 +02:00
parent 8eef3a5474
commit be60d7a3d0
2 changed files with 39 additions and 5 deletions

View File

@@ -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?]
<!--
Reference
@@ -696,14 +698,15 @@ See [Integrate with AWS SSM].
[automating helm using ansible]: https://www.ansible.com/blog/automating-helm-using-ansible
[collections index]: https://docs.ansible.com/ansible/latest/collections/index.html
[configuration]: https://docs.ansible.com/ansible/latest/reference_appendices/config.html
[developing and testing ansible roles with molecule and podman - part 1]: https://www.ansible.com/blog/developing-and-testing-ansible-roles-with-molecule-and-podman-part-1/
[galaxy sivel.toiletwater]: https://galaxy.ansible.com/ui/repo/published/sivel/toiletwater/
[galaxy]: https://galaxy.ansible.com/
[roles]: https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html
[slurp]: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/slurp_module.html
[special tags: always and never]: https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_tags.html#special-tags-always-and-never
[special variables]: https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html
[templating]: https://docs.ansible.com/ansible/latest/user_guide/playbooks_templating.html
[tests]: https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html
[slurp]: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/slurp_module.html
<!-- Others -->
[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

View File

@@ -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 }}"