Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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

share|improve this answer

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 of access.

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

[xyz]
www.xyz.com ansible_ssh_user=xyz_user

[abc]
www.abc.com

[abs: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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.