4

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?

2
  • 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. May 31, 2013 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 May 31, 2013 at 11:04

3 Answers 3

4

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
  tasks:
  - # 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.

3
  • Ok, I think that it will solve the problem. Aug 12, 2013 at 10:12
  • Do you know what osx is?
    – James Lee
    Feb 8, 2016 at 21:21
  • This does not work for us with ansible 2.2.1.0. Our workaround: Use hosts: '*' and use conditionals in the role. May 26, 2017 at 12:54
3

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
  tasks:
  - 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
  tasks:
...
- name: RG Ansible for CentOS 5
  hosts: CentOS_5
  gather_facts: false
  tasks:

- name: RG Ansible for VM
  hosts: guest
  gather_facts: false
  tasks:
  - service: name=acpid state=stopped enabled=no
  - service: name=cpuspeed state=stopped enabled=no
2

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

[targets]

localhost              ansible_connection=local
other1.example.com     ansible_connection=ssh        ansible_ssh_user=mpdehaan
other2.example.com     ansible_connection=ssh        ansible_ssh_user=mdehaan
1
  • 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 Jun 14, 2013 at 9:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .