Ansible : using different sudo user for different hosts

Solution 1:

You can leverage the ansible playbooks for these kind of stuffs.

e.g.

--- 
- hosts: host1:host2 
  user: user1 
  sudo: yes
  tasks: 
  - name: update package list 
    action: command /usr/bin/apt-get update 
  - name: upgrade packages 
    action: command /usr/bin/apt-get -u -y dist-upgrade 
- hosts: host3 
  user: ubuntu
  sudo: yes 
  tasks: 
  - name: update package list 
    action: command /usr/bin/apt-get update 
  - name: upgrade packages 
    action: command /usr/bin/apt-get -u -y dist-upgrade

Hope it works for you :)

Solution 2:

I would recommend using host variables based on the inventory or group vars. This allows you to set the ssh user per host or per group of hosts. The advantage of this approach is that it's transparent to your playbooks. In the playbooks you then only have to worry about installing things and not access.

An example on how to set the user by host and group:

[xyz]
www.xyz.com ansible_ssh_user=xyz_user

[abc]
www.abc.com

[abc:vars]
ansible_ssh_user=abc_user

And here is the documentation that explains how it works much better than me : http://docs.ansible.com/intro_inventory.html