jenkins ansible template

Hi,

i am trying to add multiple sudoers line with template.

so, i am passing sudoers=abc,def within jenkins and use -e (extended vars) while running playbook

in template:

{% for item in sudoers %}
{{ item }} ALL=(ALL) NOPASSWD:ALL
{% endfor %}

yml files looks like :

  • name: sudoers file
    template:
    src: /templates/app.j2
    dest: /etc/sudoers.d/app
    owner: root
    group: root
    mode: 0440
    with_items: ‘{{ sudoers }}’

Out put is :

a ALL=(ALL) NOPASSWD:ALL
b ALL=(ALL) NOPASSWD:ALL
c ALL=(ALL) NOPASSWD:ALL
, ALL=(ALL) NOPASSWD:ALL
d ALL=(ALL) NOPASSWD:ALL
e ALL=(ALL) NOPASSWD:ALL
f ALL=(ALL) NOPASSWD:ALL

i was expecting :
abc (ALL) NOPASSWD:ALL
def (ALL) NOPASSWD:ALL

any ideas ?

Your ‘sudoer’ is a string, you want the with_items to iterate over a list. Try this:

with_items: “{{ sudoers.split(‘,’) }}”

kind regards
Pshem

Thanks Pshem but i am still getting same result.

Regards,
Sudhir

I just realised you have in fact two loops in there - one in the task (with_items) and one in the template (for). I think the easiest way is to drop the with_items and use the one inside the template:

{% for item in sudoers.split(‘,’ %}

kind regards
Pshem

Yeah, just did this and it worked :

Great. thanks.

Regards,
Sudhir