In ansible, how do I get a variable with the name of the user running ansible?
I'm scripting a deployment process that takes the name of the user running the ansible script (e.g. tlau) and creates a deployment directory on the remote system based on that username and the current date/time (e.g. tlau-deploy-2014-10-15-16:52).
You would think this is available in ansible facts (e.g. LOGNAME or SUDO_USER), but those are all set to either "root" or the deployment id being used to ssh into the remote system. None of those contain the local user, the one who is currently running the ansible process.
How can I script getting the name of the user running the ansible process and use it in my playbook?
variables username ansible ansible-playbook
add a comment |
I'm scripting a deployment process that takes the name of the user running the ansible script (e.g. tlau) and creates a deployment directory on the remote system based on that username and the current date/time (e.g. tlau-deploy-2014-10-15-16:52).
You would think this is available in ansible facts (e.g. LOGNAME or SUDO_USER), but those are all set to either "root" or the deployment id being used to ssh into the remote system. None of those contain the local user, the one who is currently running the ansible process.
How can I script getting the name of the user running the ansible process and use it in my playbook?
variables username ansible ansible-playbook
add a comment |
I'm scripting a deployment process that takes the name of the user running the ansible script (e.g. tlau) and creates a deployment directory on the remote system based on that username and the current date/time (e.g. tlau-deploy-2014-10-15-16:52).
You would think this is available in ansible facts (e.g. LOGNAME or SUDO_USER), but those are all set to either "root" or the deployment id being used to ssh into the remote system. None of those contain the local user, the one who is currently running the ansible process.
How can I script getting the name of the user running the ansible process and use it in my playbook?
variables username ansible ansible-playbook
I'm scripting a deployment process that takes the name of the user running the ansible script (e.g. tlau) and creates a deployment directory on the remote system based on that username and the current date/time (e.g. tlau-deploy-2014-10-15-16:52).
You would think this is available in ansible facts (e.g. LOGNAME or SUDO_USER), but those are all set to either "root" or the deployment id being used to ssh into the remote system. None of those contain the local user, the one who is currently running the ansible process.
How can I script getting the name of the user running the ansible process and use it in my playbook?
variables username ansible ansible-playbook
variables username ansible ansible-playbook
asked Oct 15 '14 at 23:56
Tessa LauTessa Lau
4811514
4811514
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
If you mean the username on the host system, I imagine you could just run a local action:
- name: get the username running the deploy
become: false
local_action: command whoami
register: username_on_the_host
- debug: var=username_on_the_host
In this example, the output of the whoami command is registered in a variable called "username_on_the_host", and the username will be contained in username_on_the_host.stdout.
(the debug task is not required here, it just demonstrates the content of the variable)
1
setsudo: noto make sure it doesn't try to sudo before running thelocal_action
– drs
Feb 2 '16 at 3:07
1
in ansible 2.0, that would bebecome: noorbecome: false. But otherwise a good tip, thanks!
– Ramon de la Fuente
Feb 4 '16 at 19:34
Is there a way to avoidchanged=1? Also, can fact delegation be used?
– nponeccop
Mar 12 '16 at 1:57
Sure there is, usechange_when: False(docs.ansible.com/ansible/…)
– Ramon de la Fuente
Mar 12 '16 at 14:12
3
Notably, the change variable ischanged_when, notchange_when. Just got bit by this.
– abegosum
Oct 25 '17 at 22:00
|
show 1 more comment
If you gather_facts, which is enabled by default for playbooks, there is a built-in variable that is set called ansible_user_id that provides the user name that the tasks are being run as. You can then use this variable in other tasks or templates with {{ ansible_user_id }}. This would save you the step of running a task to register that variable.
See: http://docs.ansible.com/playbooks_variables.html#information-discovered-from-systems-facts
15
For future references, this returns the name of the user on the managed machine aka same as theremote_user, not the user on the host machine. To get the user name on the host machine, I don't see an alternative to alocal_actionas explained by @Ramon.
– astorije
Mar 11 '15 at 4:39
32
An alternative to lookup the user on localhost running ansible is to use lookup: {{ lookup('env', 'USER') }} docs.ansible.com/ansible/playbooks_lookups.html
– Budric
Mar 17 '16 at 17:03
9
@Budric In my opinion this should be posted as an answer
– 030
Jul 19 '16 at 16:55
9
However{{ansible_user_id}}ends up being "root" if youbecome:yes
– Stephen Ostermiller
Aug 8 '17 at 19:08
add a comment |
I put something like the following in all templates:
# Placed here by {{ lookup('env','USER') }} using Ansible, {{ ansible_date_time.date }}.
When templated over it shows up as:
# Placed here by staylorx using Ansible, 2017-01-11.
If I use {{ ansible_user_id }} and I've become root then that variable indicates "root", not what I want most of the time.
You can also add this lookup in a var at the beginning of the playbook in avarssection. Like this : ``` - hosts: xxx gather_facts: no vars: user: "{{ lookup('env','USER') }}" ```
– Bludwarf
Jul 31 '17 at 15:44
you can use the jinja2 comment filter and the ansible_managed variable which is described in the documentation docs.ansible.com/ansible/latest/… (search for ansible_managed)
– dtrv
Jan 11 '18 at 14:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26394096%2fin-ansible-how-do-i-get-a-variable-with-the-name-of-the-user-running-ansible%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you mean the username on the host system, I imagine you could just run a local action:
- name: get the username running the deploy
become: false
local_action: command whoami
register: username_on_the_host
- debug: var=username_on_the_host
In this example, the output of the whoami command is registered in a variable called "username_on_the_host", and the username will be contained in username_on_the_host.stdout.
(the debug task is not required here, it just demonstrates the content of the variable)
1
setsudo: noto make sure it doesn't try to sudo before running thelocal_action
– drs
Feb 2 '16 at 3:07
1
in ansible 2.0, that would bebecome: noorbecome: false. But otherwise a good tip, thanks!
– Ramon de la Fuente
Feb 4 '16 at 19:34
Is there a way to avoidchanged=1? Also, can fact delegation be used?
– nponeccop
Mar 12 '16 at 1:57
Sure there is, usechange_when: False(docs.ansible.com/ansible/…)
– Ramon de la Fuente
Mar 12 '16 at 14:12
3
Notably, the change variable ischanged_when, notchange_when. Just got bit by this.
– abegosum
Oct 25 '17 at 22:00
|
show 1 more comment
If you mean the username on the host system, I imagine you could just run a local action:
- name: get the username running the deploy
become: false
local_action: command whoami
register: username_on_the_host
- debug: var=username_on_the_host
In this example, the output of the whoami command is registered in a variable called "username_on_the_host", and the username will be contained in username_on_the_host.stdout.
(the debug task is not required here, it just demonstrates the content of the variable)
1
setsudo: noto make sure it doesn't try to sudo before running thelocal_action
– drs
Feb 2 '16 at 3:07
1
in ansible 2.0, that would bebecome: noorbecome: false. But otherwise a good tip, thanks!
– Ramon de la Fuente
Feb 4 '16 at 19:34
Is there a way to avoidchanged=1? Also, can fact delegation be used?
– nponeccop
Mar 12 '16 at 1:57
Sure there is, usechange_when: False(docs.ansible.com/ansible/…)
– Ramon de la Fuente
Mar 12 '16 at 14:12
3
Notably, the change variable ischanged_when, notchange_when. Just got bit by this.
– abegosum
Oct 25 '17 at 22:00
|
show 1 more comment
If you mean the username on the host system, I imagine you could just run a local action:
- name: get the username running the deploy
become: false
local_action: command whoami
register: username_on_the_host
- debug: var=username_on_the_host
In this example, the output of the whoami command is registered in a variable called "username_on_the_host", and the username will be contained in username_on_the_host.stdout.
(the debug task is not required here, it just demonstrates the content of the variable)
If you mean the username on the host system, I imagine you could just run a local action:
- name: get the username running the deploy
become: false
local_action: command whoami
register: username_on_the_host
- debug: var=username_on_the_host
In this example, the output of the whoami command is registered in a variable called "username_on_the_host", and the username will be contained in username_on_the_host.stdout.
(the debug task is not required here, it just demonstrates the content of the variable)
edited Nov 27 '18 at 6:17
Jamieson Becker
1,85511213
1,85511213
answered Oct 16 '14 at 7:57
Ramon de la FuenteRamon de la Fuente
5,02212427
5,02212427
1
setsudo: noto make sure it doesn't try to sudo before running thelocal_action
– drs
Feb 2 '16 at 3:07
1
in ansible 2.0, that would bebecome: noorbecome: false. But otherwise a good tip, thanks!
– Ramon de la Fuente
Feb 4 '16 at 19:34
Is there a way to avoidchanged=1? Also, can fact delegation be used?
– nponeccop
Mar 12 '16 at 1:57
Sure there is, usechange_when: False(docs.ansible.com/ansible/…)
– Ramon de la Fuente
Mar 12 '16 at 14:12
3
Notably, the change variable ischanged_when, notchange_when. Just got bit by this.
– abegosum
Oct 25 '17 at 22:00
|
show 1 more comment
1
setsudo: noto make sure it doesn't try to sudo before running thelocal_action
– drs
Feb 2 '16 at 3:07
1
in ansible 2.0, that would bebecome: noorbecome: false. But otherwise a good tip, thanks!
– Ramon de la Fuente
Feb 4 '16 at 19:34
Is there a way to avoidchanged=1? Also, can fact delegation be used?
– nponeccop
Mar 12 '16 at 1:57
Sure there is, usechange_when: False(docs.ansible.com/ansible/…)
– Ramon de la Fuente
Mar 12 '16 at 14:12
3
Notably, the change variable ischanged_when, notchange_when. Just got bit by this.
– abegosum
Oct 25 '17 at 22:00
1
1
set
sudo: no to make sure it doesn't try to sudo before running the local_action– drs
Feb 2 '16 at 3:07
set
sudo: no to make sure it doesn't try to sudo before running the local_action– drs
Feb 2 '16 at 3:07
1
1
in ansible 2.0, that would be
become: no or become: false. But otherwise a good tip, thanks!– Ramon de la Fuente
Feb 4 '16 at 19:34
in ansible 2.0, that would be
become: no or become: false. But otherwise a good tip, thanks!– Ramon de la Fuente
Feb 4 '16 at 19:34
Is there a way to avoid
changed=1? Also, can fact delegation be used?– nponeccop
Mar 12 '16 at 1:57
Is there a way to avoid
changed=1? Also, can fact delegation be used?– nponeccop
Mar 12 '16 at 1:57
Sure there is, use
change_when: False (docs.ansible.com/ansible/…)– Ramon de la Fuente
Mar 12 '16 at 14:12
Sure there is, use
change_when: False (docs.ansible.com/ansible/…)– Ramon de la Fuente
Mar 12 '16 at 14:12
3
3
Notably, the change variable is
changed_when, not change_when. Just got bit by this.– abegosum
Oct 25 '17 at 22:00
Notably, the change variable is
changed_when, not change_when. Just got bit by this.– abegosum
Oct 25 '17 at 22:00
|
show 1 more comment
If you gather_facts, which is enabled by default for playbooks, there is a built-in variable that is set called ansible_user_id that provides the user name that the tasks are being run as. You can then use this variable in other tasks or templates with {{ ansible_user_id }}. This would save you the step of running a task to register that variable.
See: http://docs.ansible.com/playbooks_variables.html#information-discovered-from-systems-facts
15
For future references, this returns the name of the user on the managed machine aka same as theremote_user, not the user on the host machine. To get the user name on the host machine, I don't see an alternative to alocal_actionas explained by @Ramon.
– astorije
Mar 11 '15 at 4:39
32
An alternative to lookup the user on localhost running ansible is to use lookup: {{ lookup('env', 'USER') }} docs.ansible.com/ansible/playbooks_lookups.html
– Budric
Mar 17 '16 at 17:03
9
@Budric In my opinion this should be posted as an answer
– 030
Jul 19 '16 at 16:55
9
However{{ansible_user_id}}ends up being "root" if youbecome:yes
– Stephen Ostermiller
Aug 8 '17 at 19:08
add a comment |
If you gather_facts, which is enabled by default for playbooks, there is a built-in variable that is set called ansible_user_id that provides the user name that the tasks are being run as. You can then use this variable in other tasks or templates with {{ ansible_user_id }}. This would save you the step of running a task to register that variable.
See: http://docs.ansible.com/playbooks_variables.html#information-discovered-from-systems-facts
15
For future references, this returns the name of the user on the managed machine aka same as theremote_user, not the user on the host machine. To get the user name on the host machine, I don't see an alternative to alocal_actionas explained by @Ramon.
– astorije
Mar 11 '15 at 4:39
32
An alternative to lookup the user on localhost running ansible is to use lookup: {{ lookup('env', 'USER') }} docs.ansible.com/ansible/playbooks_lookups.html
– Budric
Mar 17 '16 at 17:03
9
@Budric In my opinion this should be posted as an answer
– 030
Jul 19 '16 at 16:55
9
However{{ansible_user_id}}ends up being "root" if youbecome:yes
– Stephen Ostermiller
Aug 8 '17 at 19:08
add a comment |
If you gather_facts, which is enabled by default for playbooks, there is a built-in variable that is set called ansible_user_id that provides the user name that the tasks are being run as. You can then use this variable in other tasks or templates with {{ ansible_user_id }}. This would save you the step of running a task to register that variable.
See: http://docs.ansible.com/playbooks_variables.html#information-discovered-from-systems-facts
If you gather_facts, which is enabled by default for playbooks, there is a built-in variable that is set called ansible_user_id that provides the user name that the tasks are being run as. You can then use this variable in other tasks or templates with {{ ansible_user_id }}. This would save you the step of running a task to register that variable.
See: http://docs.ansible.com/playbooks_variables.html#information-discovered-from-systems-facts
edited Dec 5 '18 at 12:58
Mark Stosberg
8,82152845
8,82152845
answered Nov 6 '14 at 20:02
Tony CesaroTony Cesaro
86568
86568
15
For future references, this returns the name of the user on the managed machine aka same as theremote_user, not the user on the host machine. To get the user name on the host machine, I don't see an alternative to alocal_actionas explained by @Ramon.
– astorije
Mar 11 '15 at 4:39
32
An alternative to lookup the user on localhost running ansible is to use lookup: {{ lookup('env', 'USER') }} docs.ansible.com/ansible/playbooks_lookups.html
– Budric
Mar 17 '16 at 17:03
9
@Budric In my opinion this should be posted as an answer
– 030
Jul 19 '16 at 16:55
9
However{{ansible_user_id}}ends up being "root" if youbecome:yes
– Stephen Ostermiller
Aug 8 '17 at 19:08
add a comment |
15
For future references, this returns the name of the user on the managed machine aka same as theremote_user, not the user on the host machine. To get the user name on the host machine, I don't see an alternative to alocal_actionas explained by @Ramon.
– astorije
Mar 11 '15 at 4:39
32
An alternative to lookup the user on localhost running ansible is to use lookup: {{ lookup('env', 'USER') }} docs.ansible.com/ansible/playbooks_lookups.html
– Budric
Mar 17 '16 at 17:03
9
@Budric In my opinion this should be posted as an answer
– 030
Jul 19 '16 at 16:55
9
However{{ansible_user_id}}ends up being "root" if youbecome:yes
– Stephen Ostermiller
Aug 8 '17 at 19:08
15
15
For future references, this returns the name of the user on the managed machine aka same as the
remote_user, not the user on the host machine. To get the user name on the host machine, I don't see an alternative to a local_action as explained by @Ramon.– astorije
Mar 11 '15 at 4:39
For future references, this returns the name of the user on the managed machine aka same as the
remote_user, not the user on the host machine. To get the user name on the host machine, I don't see an alternative to a local_action as explained by @Ramon.– astorije
Mar 11 '15 at 4:39
32
32
An alternative to lookup the user on localhost running ansible is to use lookup: {{ lookup('env', 'USER') }} docs.ansible.com/ansible/playbooks_lookups.html
– Budric
Mar 17 '16 at 17:03
An alternative to lookup the user on localhost running ansible is to use lookup: {{ lookup('env', 'USER') }} docs.ansible.com/ansible/playbooks_lookups.html
– Budric
Mar 17 '16 at 17:03
9
9
@Budric In my opinion this should be posted as an answer
– 030
Jul 19 '16 at 16:55
@Budric In my opinion this should be posted as an answer
– 030
Jul 19 '16 at 16:55
9
9
However
{{ansible_user_id}} ends up being "root" if you become:yes– Stephen Ostermiller
Aug 8 '17 at 19:08
However
{{ansible_user_id}} ends up being "root" if you become:yes– Stephen Ostermiller
Aug 8 '17 at 19:08
add a comment |
I put something like the following in all templates:
# Placed here by {{ lookup('env','USER') }} using Ansible, {{ ansible_date_time.date }}.
When templated over it shows up as:
# Placed here by staylorx using Ansible, 2017-01-11.
If I use {{ ansible_user_id }} and I've become root then that variable indicates "root", not what I want most of the time.
You can also add this lookup in a var at the beginning of the playbook in avarssection. Like this : ``` - hosts: xxx gather_facts: no vars: user: "{{ lookup('env','USER') }}" ```
– Bludwarf
Jul 31 '17 at 15:44
you can use the jinja2 comment filter and the ansible_managed variable which is described in the documentation docs.ansible.com/ansible/latest/… (search for ansible_managed)
– dtrv
Jan 11 '18 at 14:23
add a comment |
I put something like the following in all templates:
# Placed here by {{ lookup('env','USER') }} using Ansible, {{ ansible_date_time.date }}.
When templated over it shows up as:
# Placed here by staylorx using Ansible, 2017-01-11.
If I use {{ ansible_user_id }} and I've become root then that variable indicates "root", not what I want most of the time.
You can also add this lookup in a var at the beginning of the playbook in avarssection. Like this : ``` - hosts: xxx gather_facts: no vars: user: "{{ lookup('env','USER') }}" ```
– Bludwarf
Jul 31 '17 at 15:44
you can use the jinja2 comment filter and the ansible_managed variable which is described in the documentation docs.ansible.com/ansible/latest/… (search for ansible_managed)
– dtrv
Jan 11 '18 at 14:23
add a comment |
I put something like the following in all templates:
# Placed here by {{ lookup('env','USER') }} using Ansible, {{ ansible_date_time.date }}.
When templated over it shows up as:
# Placed here by staylorx using Ansible, 2017-01-11.
If I use {{ ansible_user_id }} and I've become root then that variable indicates "root", not what I want most of the time.
I put something like the following in all templates:
# Placed here by {{ lookup('env','USER') }} using Ansible, {{ ansible_date_time.date }}.
When templated over it shows up as:
# Placed here by staylorx using Ansible, 2017-01-11.
If I use {{ ansible_user_id }} and I've become root then that variable indicates "root", not what I want most of the time.
answered Jan 11 '17 at 16:29
staylorxstaylorx
651611
651611
You can also add this lookup in a var at the beginning of the playbook in avarssection. Like this : ``` - hosts: xxx gather_facts: no vars: user: "{{ lookup('env','USER') }}" ```
– Bludwarf
Jul 31 '17 at 15:44
you can use the jinja2 comment filter and the ansible_managed variable which is described in the documentation docs.ansible.com/ansible/latest/… (search for ansible_managed)
– dtrv
Jan 11 '18 at 14:23
add a comment |
You can also add this lookup in a var at the beginning of the playbook in avarssection. Like this : ``` - hosts: xxx gather_facts: no vars: user: "{{ lookup('env','USER') }}" ```
– Bludwarf
Jul 31 '17 at 15:44
you can use the jinja2 comment filter and the ansible_managed variable which is described in the documentation docs.ansible.com/ansible/latest/… (search for ansible_managed)
– dtrv
Jan 11 '18 at 14:23
You can also add this lookup in a var at the beginning of the playbook in a
vars section. Like this : ``` - hosts: xxx gather_facts: no vars: user: "{{ lookup('env','USER') }}" ```– Bludwarf
Jul 31 '17 at 15:44
You can also add this lookup in a var at the beginning of the playbook in a
vars section. Like this : ``` - hosts: xxx gather_facts: no vars: user: "{{ lookup('env','USER') }}" ```– Bludwarf
Jul 31 '17 at 15:44
you can use the jinja2 comment filter and the ansible_managed variable which is described in the documentation docs.ansible.com/ansible/latest/… (search for ansible_managed)
– dtrv
Jan 11 '18 at 14:23
you can use the jinja2 comment filter and the ansible_managed variable which is described in the documentation docs.ansible.com/ansible/latest/… (search for ansible_managed)
– dtrv
Jan 11 '18 at 14:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26394096%2fin-ansible-how-do-i-get-a-variable-with-the-name-of-the-user-running-ansible%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown