Skip to content

Replace ansible_user with ansible_user_id var #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

fgierlinger
Copy link

This PR changes ansible_user to ansible_user_id.

The `ansible_user' variable is used in the inventory to specify which user should be used to log in to the remote host. However, this variable may be missing if the current username is to be used for authentication on the remote system.

There is another special variable that is better suited to get the effective username: ansible_user_id. There are 3 login cases that I could identify. The ansible_user_id variable behaves as follows:

  1. Login directly with the user elastic: ansible_user_id is set to elastic
  2. Login with a privileged user (e.g. root): ansible_user_id is set to the name of the privileged user (e.g. root)
  3. Login with an unprivileged user (e.g. user1) but with sudo permissions: ansible_user_id is set to root

The following playbook demonstrates this case best:

user1@host$ cat test.yml
- hosts: all
  gather_facts: true
  tasks:
    - debug: var=ansible_user
      failed_when: false
    - debug: var=ansible_user_id
      failed_when: false

user1@host$ ansible-playbook -i host2, -k ./test.yml
SSH password:

PLAY [all] ***************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [host2]

TASK [debug] ***************************************************************************************************************************************
ok: [host2] => {
    "ansible_user": "VARIABLE IS NOT DEFINED!",
    "failed_when_result": false
}

TASK [debug] ***************************************************************************************************************************************
ok: [host2] => {
    "ansible_user_id": "user1",
    "failed_when_result": false
}

PLAY RECAP ***************************************************************************************************************************************
host2       : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

user1@host$  ansible-playbook -i host2, -b -k -K ./test.yml

SSH password:
BECOME password[defaults to SSH password]:

PLAY [all] ***************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [host2]

TASK [debug] ***************************************************************************************************************************************
ok: [host2] => {
"ansible_user": "VARIABLE IS NOT DEFINED!",
"failed_when_result": false
}

TASK [debug] ***************************************************************************************************************************************
ok: [host2] => {
"ansible_user_id": "root",
"failed_when_result": false
}

PLAY RECAP ***************************************************************************************************************************************
host2 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant