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?
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?
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.
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.
ansible 2.2.1.0
. Our workaround: Use hosts: '*'
and use conditionals in the role.
May 26, 2017 at 12:54
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
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
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.