A small script to collect information about servers in a cvs file:

- name: collect info about servers hosts: webservers tasks: - name: template list template: src="servers_report.j2" dest="~/ansible/webservers/servers_report.csv" connection: local 

A simple servers_report.j2 template with a ternary filter:

 hostname;provider;monitoring url; {% for item in play_hosts %} {{ item }};{{ hostvars[item].provider }};{{ hostvars[item].monitoring | ternary("https://{{ item }}/monitoring_url/", '') }}; {% endfor %} 

List of host parameters in the hosts_production file (sample fragment):

 server1 provider="provider1" monitoring=True server2 provider="provider2" monitoring=False 

And ... does not work: {{item}} is not counted as a variable.

 hostname;provider;monitoring url; server1;provider1;https://{{ item }}/monitoring_url/; server2;provider2;; 

Simple options like ternary ('1', '2') work without problems , questions appear in such cases more difficult when you have to guess how to arrange quotes.

    1 answer 1

    For simplicity, we can assume that inside the {{ and }} code in Python.

     {{ hostvars[item].monitoring | ternary('https://'+item+'/monitoring_url/', '') }} 

    ternary is a function. As one of the parameters, it needs to pass a string in order to get the resulting string, we add the strings 'https://' + item + '/monitoring_url/' .