1

We have a sudoers file in /etc/sudoers.d/ops (on 10 servers). Sometimes we need to add multiple users and Cmnd_Alias to that file. How can we automate this with an ansible playbook?

Our sudoers file:

User_Alias     OPS_USERS = user1,user2,user3

Cmnd_Alias     OPS_CMD = /sbin/ifconfig, /usr/sbin/dmidecode

OPS_USERS      ALL = NOPASSWD:OPS_CMD  
3

Personally I would go with a template module (link). I would prepare a template somehow similar to this:

User_Alias     OPS_USERS = {{ users|join(', ') }}

Cmnd_Alias     OPS_CMD = {{ commands|join(', ') }}

OPS_USERS      ALL = NOPASSWD:OPS_CMD

And in variables I would put something like this:

users:
  - "user1"
  - "user2"
  - "user3"

commands: 
  - "/sbin/ifconfig"
  - "/usr/sbin/dmidecode"

Edit: Maybe a little bit of explanation would be needed.

In template I used a filter that joins strings with given separator (', '). You can find more about filters here. Of course strings to concatenate are taken from the lists 'users' or 'command' defined in variables section of your play book.

  • That's Exactly What I Wanted , Thank you so much :) – Shyam Jos Mar 15 '17 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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