I need to check all the cities in the country variable list to see if they contain the city name in the Ansible hostname variable.
It means running hosts can contain a city name in its own hostname.
- name: Find city
gather_facts: true
hosts: 10.72.45.12
vars:
counties:
Canada: ["ontario", "toronto", "montreal"]
Germany: ["berlin", "munich", "hamburg"]
USA: ["chicago", "ostin", "seattle"]
tasks:
- name: Getting counties by city
set_fact:
country: >-
{% for city in counties %}
{% if '{{ city }}' in '{{ ansible_hostname }}' %}
Canada
{% elif '{{ city }}' in '{{ ansible_hostname }}' %}
Germany
{% elif '{{ city }}' in '{{ ansible_hostname }}' %}
USA
{% else %}
Earth
{% endif %}
{% endfor %}
- debug:
var: country
Some errors in your code
{% ... %}
, you do not need an expression delimiter {{ ... }}
:
{% if city in ansible_hostname %}
countries
dictionary{% for key, value in my_dict.items() %}
With all this, you can construct the two tasks:
- set_fact:
country: >-
{% for country, cities in countries.items() -%}
{% for city in cities if city in ansible_hostname -%}
{{ country }}
{%- endfor %}
{%- endfor %}
vars:
countries:
Canada: ["ontario", "toronto", "montreal"]
Germany: ["berlin", "munich", "hamburg"]
USA: ["chicago", "austin", "seattle"]
- debug:
var: country | default('Earth', true)
Which could yield something like:
ok: [foo_ontario_bar] =>
country | default('Earth', true): Canada
ok: [foo_berlin_bar] =>
country | default('Earth', true): Germany
ok: [foo_seattle_bar] =>
country | default('Earth', true): USA
ok: [foo_brussels_bar] =>
country | default('Earth', true): Earth