On 03.12.2017 02:18, Adolphson wrote:
Sorry, one more thing. I want to identify if the title equals Hostname
and
both the title and list index match.
I don't understand what your mean, could you rephrase.
I have it working below, but was
hoping there is a cleaner way of doing this.
{% set title = csvfile.split("\n")[0].split(',') %}
{% for item in csvfile.split("\n")[1:] %}
{% set list = item.split(",") %}
{% if title[0] == "Hostname" and list[0] %}
{{ list[0]|trim() }}:
{% elif title[1] == "Hostname" and list[1] %}
{{ list[1]|trim() }}:
{% elif title[1] == "Hostname" and list[1] %}
{{ list[1]|trim() }}:
{% elif title[2] == "Hostname" and list[2] %}
{{ list[2]|trim() }}:
{% elif title[3] == "Hostname" and list[3] %}
{{ list[3]|trim() }}:
{% elif title[4] == "Hostname" and list[4] %}
{{ list[4]|trim() }}:
{% elif title[5] == "Hostname" and list[5] %}
{{ list[5]|trim() }}:
{% endif %}
To me, it looks like this code just print out the value correspond to
the "Hostname".
If that is the goal, then this for loop does that.
{% for element in range(list | count) %}
{% if title[element] == "Hostname" %}
{{ list[element]|trim }}:
{% endif %}
{% endfor %}
{{ title[0] }}: {{ list[0]|trim() }}
{{ title[1] }}: {{ list[1]|trim() }}
{{ title[2] }}: {{ list[2]|trim() }}
{{ title[3] }}: {{ list[3]|trim() }}
{{ title[4] }}: {{ list[4]|trim() }}
{{ title[5] }}: {{ list[5]|trim() }}
This section can be replaced with this for loop, it's dynamic so it will
work with any number for column in the csv file.
{% for element in range(list | count) %}
{{ title[element] }}: {{ list[element] | trim }}
{% endfor %}
--
Kai Stian Olstad
--
You received this message because you are subscribed to the Google Groups "Ansible
Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/5b811fd708184226755268e2613bd713%40olstad.com.
For more options, visit https://groups.google.com/d/optout.