I want ansible to create a csv file for me. For example I need the output to be in this format:
Server, User, Approve, Remove
serverA, user1,X,
serverA, user2,X
ServerA, user3,X
ServerB, user1,X
ServerC, user1,X
ServerC, user2,X
In order to do this I need to iterate over a var I registered. ie:
`
I have also been trying trying things similar to the following and receiving errors that the index is out of range:
`
- name: Create CSV file
lineinfile:
dest: “/tmp/prod_admin_users.csv”
line: “{{ host }},{{ item }},X,”
insertafter: EOF
state: present
delegate_to: localhost
with_items:
- “{{ other_users.split(‘\n’) }}”
`
Multiple task editing the same file seldom work.
Instead I would use the copy module with Jinja template like this
- copy:
content: |
Server,User,Approve,Remove,Reviewed by: YourNameHere
{% for host in ansible_play_hosts %}
{% for user in hostvars[host].other_users.stdout_lines %}
{{ host }},{{ user }},X,
{% endfor %}
dest: /tmp/prod_admin_users.csv
delegate_to: localhost
run_once: true
Works perfectly! I never heard of Jinja before getting into Ansilble a year ago. I will have to go study up on jinja templating. I can see how your example works though. Thank you.