# Switching user in playbook

**URL:** <https://forum.ansible.com/t/switching-user-in-playbook/18348>\
**Category:** Ansible Project\
**Created:** [May 29, 2015, 2:00pm UTC](https://forum.ansible.com/t/switching-user-in-playbook/18348 "2015-05-29T14:00:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [May 29, 2015, 2:00pm UTC](https://forum.ansible.com/t/switching-user-in-playbook/18348/1 "2015-05-29T14:00:07Z")

</div>

I need to switch users in a playbook: first I need to do some tasks as a root user then I need to change to a limited user and do the rest with that.

My playbook looks like this:

> - name: Preparation  
> vars:
> 
> - ansible\_ssh\_user: root
> 
> - ansible\_ssh\_private\_key\_file: ~/.ssh/site-root  
> …
> 
> - name: Main play  
> vars:
> 
> - ansible\_ssh\_user: ci
> 
> - ansible\_ssh\_private\_key\_file: ~/.ssh/site-ci

> …

This works but I feel that nasty (poor design):

- redundant configuration (in hosts and 2 plays)
- need to switch and then switch back

What is the Best practice to achieve that?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 29, 2015, 3:56pm UTC](https://forum.ansible.com/t/switching-user-in-playbook/18348/2 "2015-05-29T15:56:15Z")

</div>

the remote\_user: directive can be used at play and task level to  
change this, no need to use vars: unless your hosts have those set in  
inventory (which overrides remote\_user).

---

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [May 29, 2015, 4:15pm UTC](https://forum.ansible.com/t/switching-user-in-playbook/18348/3 "2015-05-29T16:15:19Z")

</div>

Thanks for the tip, but how can I set the credentials for alternative user?

Now I have only one user set in hosts file:

> ansible-sandbox ansible\_ssh\_host=ansible-sandbox.local ansible\_ssh\_user=ci ansible\_ssh\_private\_key\_file=~/.ssh/ci

1. május 29., péntek 17:56:28 UTC+2 időpontban Brian Coca a következőt írta:

---

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [May 29, 2015, 4:37pm UTC](https://forum.ansible.com/t/switching-user-in-playbook/18348/4 "2015-05-29T16:37:33Z")

</div>

One question and one issue:

1. question (above):  
How can I set the credentials for alternative user?

2. issue:  
playbook:

> - name: Preparation

> remote\_user: root

> vars:
> 
> - ansible\_ssh\_private\_key\_file: ~/.ssh/site-root  
> …
> 
> - name: Main play

> remote\_user: ci

> …

Does not work: ‘Main play’ will be executed as root user

If I put the following at the end:

> - ansible\_ssh\_private\_key\_file: ~/.ssh/site-ci

…then I get the following error:

> SSH Error: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

So root user is stucked somehow… and one cannot authenticate root with ci’s key.

Regards:  
Bence

1. május 29., péntek 18:15:19 UTC+2 időpontban Bence Takács a következőt írta:

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 29, 2015, 5:27pm UTC](https://forum.ansible.com/t/switching-user-in-playbook/18348/5 "2015-05-29T17:27:28Z")

</div>

what version of ansible? os? python?

switching users seems to be working fine for me.

---

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [May 29, 2015, 5:43pm UTC](https://forum.ansible.com/t/switching-user-in-playbook/18348/6 "2015-05-29T17:43:23Z")

</div>

… but where do you store the alternative user’s credentials?

2015.05.29. 19:27 ezt írta (“Brian Coca” \<[bcoca@ansible.com](mailto:bcoca@ansible.com)\>):

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 29, 2015, 6:40pm UTC](https://forum.ansible.com/t/switching-user-in-playbook/18348/7 "2015-05-29T18:40:50Z")

</div>

i use an ssh agent

---

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [May 30, 2015, 1:08am UTC](https://forum.ansible.com/t/switching-user-in-playbook/18348/8 "2015-05-30T01:08:45Z")

</div>

Well, actually it’s Windows with babun (cygwin) and python 2.7.x  
Do you think this is because of the OS?

SSH agent? Is hat work for private keys too? Or just for passwords? I suspects that more than the OS

Regards:  
Bence

2015.05.29. 20:40 ezt írta (“Brian Coca” \<[bcoca@ansible.com](mailto:bcoca@ansible.com)\>):

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 30, 2015, 1:10am UTC](https://forum.ansible.com/t/switching-user-in-playbook/18348/9 "2015-05-30T01:10:18Z")

</div>

Probably a combination of issues, there is some success running  
ansible on cygwin but also many problems, this is not a supported  
platform

---

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [June 1, 2015, 7:51am UTC](https://forum.ansible.com/t/switching-user-in-playbook/18348/10 "2015-06-01T07:51:05Z")

</div>

Thanks, Brian

With ssh-agent I reduced my ‘hosts’ file radically, and removed the ‘ansible\_ssh\_private\_key\_file’ declarations from the plays.

But I still cannot use the ‘remote\_user’ in plays, instead I need to add the ‘ansible\_ssh\_user’ variable.

This is my first cygwin-related issue with ansible.

Regards:  
Bence

1. május 30., szombat 3:10:21 UTC+2 időpontban Brian Coca a következőt írta:

---

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [June 1, 2015, 9:07am UTC](https://forum.ansible.com/t/switching-user-in-playbook/18348/11 "2015-06-01T09:07:39Z")

</div>

Hmmm… I cannot solve the issue, but now instead of using ssh agent I just set up my ~/.ssh/config correctly - and workining fine

1. június 1., hétfő 9:51:05 UTC+2 időpontban Bence Takács a következőt írta:
