Skip to content

Instantly share code, notes, and snippets.

@treyd
Last active March 9, 2022 19:24
Show Gist options
  • Save treyd/e4841e13851f81f76eb683a4d097a85e to your computer and use it in GitHub Desktop.
Save treyd/e4841e13851f81f76eb683a4d097a85e to your computer and use it in GitHub Desktop.
ansible ad-hoc commands to add ansible user
#ensure your hosts file is defined but your ansible-configuration does NOT set up an ansible_user (this is what you're creating)
# Ubuntu (14.04) Trusty
ansible -i hosts -b -K -k -u <username_with_sudo> -m user -a "name=ansible groups=sudo append=yes" hostname
ansible -i hosts -b -K -k -u <username_with_sudo> -m authorized_key -a "user=ansible key='<public_key_as_string>' manage_dir=yes" hostname
ansible -i hosts -b -K -k -u <username_with_sudo> -m lineinfile -a "dest=/etc/sudoers state=present regexp='^%sudo' line='%sudo ALL=(ALL) NOPASSWD: ALL' validate='visudo -cf %s'" hostname
# CentOS/RHEL 7
ansible -i hosts -b -K -k -u <username_with_sudo> -m user -a "name=ansible groups=wheel append=yes" hostname
ansible -i hosts -b -K -k -u <username_with_sudo> -m authorized_key -a "user=ansible key='<public_key_as_string>' manage_dir=yes" hostname
ansible -i hosts -b -K -k -u <username_with_sudo> -m lineinfile -a "dest=/etc/sudoers state=present regexp='^%wheel' line='%wheel ALL=(ALL) NOPASSWD: ALL' validate='visudo -cf %s'" hostname
# CentOS/RHEL 6
ansible -i hosts -b -K -k -u <username_with_sudo> -m package -a "name=libselinux-python state=present" hostname
ansible -i hosts -b -K -k -u <username_with_sudo> -m user -a "name=ansible groups=wheel append=yes" hostname
ansible -i hosts -b -K -k -u <username_with_sudo> -m authorized_key -a "user=ansible key='<public_key_as_string>' manage_dir=yes" hostname
ansible -i hosts -b -K -k -u <username_with_sudo> -m lineinfile -a "dest=/etc/sudoers state=present regexp='^%wheel' line='%wheel ALL=(ALL) NOPASSWD: ALL' validate='visudo -cf %s'" hostname
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment