7

Recently started using ansible. We have servers where the application is setup under different users, like in server xyz.com, unix user is xyz_user and so on.

So in case of xyz.com,

ansible xyz.com -a 'command' -u xyz_user -K

How can we set the sudo user in ansible config so as to automatically sudo to the particular user defined for the server?

2 Answers 2

8

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 :)

15

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

You must log in to answer this question.

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