Skip to content

Instantly share code, notes, and snippets.

@ramondelafuente
Last active March 17, 2023 23:38
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ramondelafuente/9819526 to your computer and use it in GitHub Desktop.
Save ramondelafuente/9819526 to your computer and use it in GitHub Desktop.
Testing ansible "ansible_ssh_user" and "ansible_user_id" variables with sudo
- name: Testing variables with SUDO=NO
hosts: "*"
sudo: no
tasks:
- name: "PLAYBOOK SUDO=NO, TASK SUDO=NO"
command: whoami
register: whoami_output
sudo: no
- debug: var=whoami_output.stdout
- debug: var=ansible_ssh_user
sudo: no
- debug: var=ansible_user_id
sudo: no
- name: "PLAYBOOK SUDO=NO, TASK SUDO=YES"
command: whoami
register: whoami_output
sudo: yes
- debug: var=whoami_output.stdout
- debug: var=ansible_ssh_user
sudo: yes
- debug: var=ansible_user_id
sudo: yes
- name: Testing variables with SUDO=YES
hosts: "*"
sudo: yes
tasks:
- name: "PLAYBOOK SUDO=YES, TASK SUDO=NO"
command: whoami
register: whoami_output
sudo: no
- debug: var=whoami_output.stdout
- debug: var=ansible_ssh_user
sudo: no
- debug: var=ansible_user_id
sudo: no
- name: "PLAYBOOK SUDO=YES, TASK SUDO=YES"
command: whoami
register: whoami_output
sudo: yes
- debug: var=whoami_output.stdout
- debug: var=ansible_ssh_user
sudo: yes
- debug: var=ansible_user_id
sudo: yes
# OUTPUT (condensed):
# PLAY [Testing variables with SUDO=NO] *****************************************
# TASK: [PLAYBOOK SUDO=NO, TASK SUDO=NO] ****************************************
# "whoami_output.stdout": "vagrant"
# "ansible_ssh_user": "vagrant"
# "ansible_user_id": "vagrant"
#
# TASK: [PLAYBOOK SUDO=NO, TASK SUDO=YES] ***************************************
# "whoami_output.stdout": "root"
# "ansible_ssh_user": "vagrant"
# "ansible_user_id": "vagrant"
# PLAY [Testing variables with SUDO=YES] ****************************************
# GATHERING FACTS ***************************************************************
# TASK: [PLAYBOOK SUDO=YES, TASK SUDO=NO] ***************************************
# "whoami_output.stdout": "vagrant"
# "ansible_ssh_user": "vagrant"
# "ansible_user_id": "root"
#
# TASK: [PLAYBOOK SUDO=YES, TASK SUDO=YES] **************************************
# "whoami_output.stdout": "root"
# "ansible_ssh_user": "vagrant"
# "ansible_user_id": "root"
@hypergig
Copy link

thank you, this was helpful

@racsoraul
Copy link

Thanks! Very useful.

@G3NSVRV
Copy link

G3NSVRV commented Apr 9, 2019

thanks m8

@Ostefanini
Copy link

Awesome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment