Last active
March 17, 2023 23:38
-
-
Save ramondelafuente/9819526 to your computer and use it in GitHub Desktop.
Testing ansible "ansible_ssh_user" and "ansible_user_id" variables with sudo
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: 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" |
Thanks! Very useful.
thanks m8
Awesome
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you, this was helpful