ansible playbook, different user by operating system
Solution 1:
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.
Solution 2:
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
Solution 3:
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