I am trying to create a sudoers file with ansible template. The sudoers file should look like below:
Cmnd_Alias LS = /bin/ls
Cmnd_Alias LESS = /usr/bin/less
Cmnd_Alias DU = /usr/bin/du
%support1 ALL=(ALL) NOPASSWD: LS, LESS, DU
What I have managed so far is below:
Cmnd_Alias LS = ls
Cmnd_Alias LESS = less
Cmnd_Alias DU = du
%support1 ALL=(ALL) NOPASSWD: LS, LESS, DU
The template looks like below:
{% for item in commands %}
Cmnd_Alias {{ item|upper }} = {{ item }}
{% endfor %}
%{{ group }} ALL=(ALL) NOPASSWD: {% for item in commands %}
{{ item|upper}}{% if not loop.last %}, {% endif %}
{% endfor %}
vars
commands:
- ls
- less
- du
As far as I know, ansible template module does not have anything which will execute the command in the remote server and print the output otherwise I was thinking of changing template file to look like below:
{% for item in commands %}
Cmnd_Alias {{ item|upper }} = `which {{ item }}`
{% endfor %}
%{{ group }} ALL=(ALL) NOPASSWD: {% for item in commands %}
{{ item|upper}}{% if not loop.last %}, {% endif %}
{% endfor %}
and the output will be like what I wanted.
Is there any other method which can make it simple?
BTW I have already checked this post