Created
May 23, 2024 14:35
-
-
Save casperghst42/0e5ae0ad1ec984996b2c200cc16a2cd2 to your computer and use it in GitHub Desktop.
Ansible: test for user existence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- name: get list of existing users | |
ansible.builtin.getent: | |
database: passwd | |
- name: get list of existing usernames | |
ansible.builtin.set_fact: | |
existing_users: "{{ ansible_facts.getent_passwd.keys() | list }}" | |
- name: create ansible user | |
ansible.builtin.user: | |
name: xyz | |
state: present | |
shell: /bin/bash | |
append: yes | |
when: "'xyz' not in existing_users" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment