Skip to content

Instantly share code, notes, and snippets.

@casperghst42
Created May 23, 2024 14:35
Show Gist options
  • Save casperghst42/0e5ae0ad1ec984996b2c200cc16a2cd2 to your computer and use it in GitHub Desktop.
Save casperghst42/0e5ae0ad1ec984996b2c200cc16a2cd2 to your computer and use it in GitHub Desktop.
Ansible: test for user existence
- 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