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