Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm starting to play with ansible. I have a bunch of servers (Ubuntu, Centos, Red hat...).

The servers have different users. For the ubuntu servers I use sudo user, for the rest I use root user.

How could I specify the user by operating system?

share|improve this question
You really should get into the habit of using sudo across all your servers, even if it isn't the default for Red Hat systems. It's best practice to do so for security and audit purposes. – Michael Hampton May 31 '13 at 9:31
yes, it is the goal. I'm trying unify the sudo user for all servers with an playbook. But I have two groups of servers (Ubuntu and Red Hat systems). The first group authenticate whit a sudo and the second with root. I'm following this article – Andrés Sánchez García May 31 '13 at 11:04
up vote 3 down vote accepted

The docs mention performing tasks just for particular operating systems. You could tweak this to set the user for each OS. For example

- hosts: CentOS
  user: centos
  - # some tasks

- hosts: Debian
  user: default
  - # some tasks

- hosts: Ubuntu
  user: ubuntu
  - # some tasks

In the source, it appears that these operating systems are supported, though I could not confirm this in the official documentation at the time of this writing.

  • RedHat
  • Fedora
  • CentOS
  • Scientific
  • SLC
  • Ascendos
  • CloudLinux
  • PSBM
  • OracleLinux
  • OVS
  • OEL
  • Amazon
  • XenServer
  • Ubuntu
  • Debian
  • SLES
  • SLED
  • OpenSuSE
  • SuSE
  • Gentoo
  • ArchLinux
  • Mandriva
  • Mandrake
  • Solaris
  • Nexenta
  • OmniOS
  • OpenIndiana
  • SmartOS

You can also have per-OS variables defined in vars/<os-name>.yaml files and conditionally use them using the ansible_os_family template variable as shown here.

share|improve this answer
Ok, I think that it will solve the problem. – Andrés Sánchez García Aug 12 '13 at 10:12
Do you know what osx is? – James Lee Feb 8 at 21:21

Specify the user on the command line:

ansible-playbook foo.yml --extra-vars "user=bar"

- user: '{{ user }}'

Specify a different user per host-group in /etc/ansible/hosts


localhost              ansible_connection=local     ansible_connection=ssh        ansible_ssh_user=mpdehaan     ansible_connection=ssh        ansible_ssh_user=mdehaan
share|improve this answer
you specify the user by host. But I need specify the user by operating system. I think that a solution is specify the operating system on /etc/ansible/hosts variables. It is like your solution – Andrés Sánchez García Jun 14 '13 at 9:47

The CentOS and so on isn't automatic. It's a group over ansible provided variable. Here is a full example, dealing with CentOS plus VM or not VM.

- name: RG Ansible for ALL
  hosts: all
  - name: group by OS versions
    group_by: key="{{ ansible_distribution }}_{{ ansible_distribution_version.split('.')[0] }}"
  - name: group by physical/virtual machine
    group_by: key="{{ ansible_virtualization_role }}"
- name: RG Ansible for CentOS 6
  hosts: CentOS_6
  gather_facts: false
- name: RG Ansible for CentOS 5
  hosts: CentOS_5
  gather_facts: false

- name: RG Ansible for VM
  hosts: guest
  gather_facts: false
  - service: name=acpid state=stopped enabled=no
  - service: name=cpuspeed state=stopped enabled=no
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.