List comprehension like in defaults/main.yml

80 views
Skip to first unread message

Ju Ju

unread,
Sep 14, 2016, 1:52:26 PM9/14/16
to Ansible Project
Hi,

I need to create a list of fqdn according to à list of subdomain and a domain:

app__domain: '{{ ansible_local.core.domain
if (ansible_local|d() and ansible_local.core|d() and
ansible_local.core.domain|d())
else (ansible_domain if ansible_domain else ansible_hostname) }}'

app_subdomains: [ '', 'api' , 'www' ]

app_fqdns: .... ?

app_fqdns must be ['mydomain.com', 'api.mydomain.com', 'www.mydomain.com ]

How could I do this ?

PS: I'm using debops.

Azzz

Maciej Delmanowski

unread,
Sep 15, 2016, 7:02:40 AM9/15/16
to Ansible Project
Hello,

I would do it in a template, but it can be done in a variable as well. An example in a playbook:

---

- hosts: localhost
  gather_facts: False

  vars:
    app__domain: 'example.com'
    app__subdomains: [ '', 'api', 'www' ]
    app__fqdns: |
      {% set my_list = [] %}
      {% for subdomain in app__subdomains %}
      {% if subdomain %}
      {% set _ = my_list.append(subdomain + '.' + app__domain) %}
      {% else %}
      {% set _ = my_list.append(app__domain) %}
      {% endif %}
      {% endfor %}
      {{ my_list }}


  tasks:

    - debug: var=app__fqdns

An example output:

PLAY [localhost] ***************************************************************

TASK [debug] *******************************************************************
ok: [localhost] => {
    "app__fqdns": [
        "example.com", 
        "api.example.com", 
        "www.example.com"
    ]
}


How do you like DebOps?

Cheers,
Maciej
Reply all
Reply to author
Forward
0 new messages