New answers tagged

3

No, you cannot nest jinja2 expansion i.e. the following won't work: # Warning ! Those will fail ! some_var: "{{ some_dict[{{ some_dynamic_key }}] }}" other_var: "{{ {{ dynamicaly_prefixed }}_var }}" # Did I forget to say the above examples will fail ? But you can use your var without any problem, just as in almost any other programming ...


1

Use select() with the defined test. ssh_listen_to: "{{ [ansible_facts.default_ipv4.address, ansible_facts.default_ipv6.address] | select('defined') | list }}" As for the linked post about matching dictionary keys in JMESPath, I wouldn't use JMESPath at all for that in Ansible. As I've grown more accustomed to Jinja I've found json_query() less and ...


0

This doesn't answer the question directly, but I found that I could query the entire ansible facts dictionary as a json document using the json_query filter and a jmespath expression. For example, the following produces a list of all the ip addresses. ssh_listen_to: "{{ ansible_facts | json_query('*.address') }}" You can make this safer by just ...


1

Create the list in Jinja2 and convert it, e.g. - hosts: localhost gather_facts: true tasks: - set_fact: ssh_listen_to: "{{ _ssh_listen_to|from_yaml }}" vars: _ssh_listen_to: | {% if ansible_default_ipv4.address is defined %} - {{ ansible_default_ipv4.address }} {% endif %} {% if ...


Top 50 recent answers are included