# How to choose remote\_user accoring to OS distribution?

**URL:** <https://forum.ansible.com/t/how-to-choose-remote-user-accoring-to-os-distribution/18729>\
**Category:** Ansible Project\
**Tags:** fedora\
**Created:** [July 10, 2015, 9:07pm UTC](https://forum.ansible.com/t/how-to-choose-remote-user-accoring-to-os-distribution/18729 "2015-07-10T21:07:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_R](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@Adam\_R](https://forum.ansible.com/u/Adam_R)\
**Post date:** [July 10, 2015, 9:07pm UTC](https://forum.ansible.com/t/how-to-choose-remote-user-accoring-to-os-distribution/18729/1 "2015-07-10T21:07:14Z")

</div>

Hello,

I am creating a role to upgrade my servers. Some are CentOS others are Fedora. CentOS servers use remote\_user: root and Fedora servers remote\_user: fedora

How can i setup my playbook to use one remote\_user according to ansible\_distribution?

In the Ansible FAQ there is a section that deals with these issue setting inventory variables in the inventory file, but i am  
using dynamic inventory. [http://docs.ansible.com/faq.html#how-do-i-handle-different-machines-needing-different-user-accounts-or-ports-to-log-in-with](http://docs.ansible.com/faq.html#how-do-i-handle-different-machines-needing-different-user-accounts-or-ports-to-log-in-with)

Or there is a way to try sshing as some user if the connection fails, try another login user?

How to choose remote user according to OS?

Or it is easier to create a separate playbook for each ansible\_distribution?

Thank you!

---

<div class="post-metadata">

**Author:** ![Adam\_R](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@Adam\_R](https://forum.ansible.com/u/Adam_R)\
**Post date:** [July 10, 2015, 9:16pm UTC](https://forum.ansible.com/t/how-to-choose-remote-user-accoring-to-os-distribution/18729/2 "2015-07-10T21:16:25Z")

</div>

A better description is "How to set remote\_user for according to OS distribution

---

<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:** [July 10, 2015, 9:28pm UTC](https://forum.ansible.com/t/how-to-choose-remote-user-accoring-to-os-distribution/18729/3 "2015-07-10T21:28:32Z")

</div>

There are several ways, the easiest is if your inventory script  
provides OS info:

remote\_user: "{{ansible\_distibution == 'Fedora'|ternary('fedora', 'root')

If you have no info ahead of time you can test connecting and then use  
group\_by (example below) or the same expression above on the result  
var. If using group\_by you can preset group\_vars/fedora =\>  
ansible\_ssh\_user: fedora

- hosts: all  
&nbsp;&nbsp;remote\_user: root  
&nbsp;&nbsp;gather\_facts: False  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- ping:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: rootlogin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: yes

&nbsp;&nbsp;&nbsp;&nbsp;- group\_by: key=fedora  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: rootlogin|failed

---

<div class="post-metadata">

**Author:** ![Adam\_R](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@Adam\_R](https://forum.ansible.com/u/Adam_R)\
**Post date:** [July 14, 2015, 3:25am UTC](https://forum.ansible.com/t/how-to-choose-remote-user-accoring-to-os-distribution/18729/4 "2015-07-14T03:25:49Z")

</div>

Thank you Brian for your reply.

Based on your suggestions, i created this playbook:  
(create two groups and then apply the role to each subset)

- hosts: all  
remote\_user: root  
gather\_facts: false  
tasks:

- ping:  
register: rootlogin  
ignore\_errors: true

- group\_by: key=fedora-user  
when: rootlogin|failed

- group\_by: key=root-user  
when: rootlogin|success

- name: Execute play for CentOS instances  
hosts: root-user  
remote\_user: root  
roles:

- { role: path/to\_role }

- name: Execute play for Fedora instances  
hosts: fedora-user  
remote\_user: fedora  
sudo: yes  
sudo\_user: root  
roles:

- { role: path/to\_role }

The problem is that only the root-user group it is created.

created ‘group\_by’ ActionModule: key=stage-root-user

No fedora-user group was created. When it comes the time to execute the rol for the fedora instances.

skipping: no hosts matched

I did also tried, create the two groups using multi plays(same idea), but the groups were created with the same elements:

- name: Group CentOS instances  
hosts: all  
remote\_user: root  
gather\_facts: no

# ignore\_errors: true

tasks:

- name: remote\_user is root  
group\_by: key=root\_user  
ignore\_errors: true

- name: Group Fedora instances  
hosts: all  
gather\_facts: no  
remote\_user: fedora  
sudo: yes  
sudo\_user: root  
tasks:

- name: remote\_user is fedora  
group\_by: key=fedora\_user  
ignore\_errors: true

- name: Execute play for CentOS instances  
hosts: root\_user  
remote\_user: root  
roles:

- { role: path/to\_role }

- name: Execute play for Fedora instances  
hosts: fedora\_user  
remote\_user: fedora  
sudo: yes  
sudo\_user: root  
roles:

- { role: path/to\_role }

FATAL: no hosts matched or all hosts have already failed – aborting

Thank you!

Ansible 1.9

---

<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:** [July 14, 2015, 4:14am UTC](https://forum.ansible.com/t/how-to-choose-remote-user-accoring-to-os-distribution/18729/5 "2015-07-14T04:14:19Z")

</div>

you really only need 1 group, not sure why both did not get created though.

with one group you can have these play targets:

- hosts: group

- hosts: all:!group

^ first will target all hosts in the group, the 2nd all hosts NOT in the group.

---

<div class="post-metadata">

**Author:** ![Adam\_R](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@Adam\_R](https://forum.ansible.com/u/Adam_R)\
**Post date:** [July 20, 2015, 9:57pm UTC](https://forum.ansible.com/t/how-to-choose-remote-user-accoring-to-os-distribution/18729/6 "2015-07-20T21:57:36Z")

</div>

Hello Brian,

I have made some tests and it seems that hosts conditional it is not working for the instances that cannot login (either root or fedora, tested both).

First test:

If i create the group according to fedora access rules, the group for the centos instances it is not created:
