0
votes

Here is the issue:

I have a playbook:

 tasks:

- include_vars: vhosts_vars.yml


- name: SSL vhost creation
  template: src=templates/proxy_ssl_vhost.DOMAIN.j2 dest=/etc/httpd/vhosts.d/ssl_{{ item.servername }}.conf
  with_items:
    - "{{ proxy_apache_vhosts_dev }}"

and vars in vars/vhosts_vars.yml

    proxy_apache_vhosts_dev:
    - {servername: www.DOMAIN.com , machinename: domainsrv, documentroot: /var/www/html, vhost_ip: 1.2.3.4, vhost_name: DOMAIN.com.conf,  serveradmin: "[email protected]" }


   proxyPass:
    - {dev: DOMAINdev , qual: DOMAINqual , prod: DOMAINprod}

Now, in my template, I also try to access the variables in ProxyPass, like: dev, qual or prod.

However, when I put this in my template:

{{ item.proxyPass.dev }}

It say that it's undefined... But, the other top variables are OK... I know that the items in proxy_apache_vhost_dev are "called" in the playbook via the "with_items:".... But How do I do to directly call the one in ProxyPass ?

2

2 Answers

2
votes

Since you only have a single item in the list then the task will run once, with item set to this value:

{servername: www.DOMAIN.com , machinename: domainsrv, documentroot: /var/www/html, vhost_ip: 1.2.3.4, vhost_name: DOMAIN.com.conf,  serveradmin: "[email protected]" }

Using {{ item.proxyPass.dev }} in your template throws an error because there is nothing named 'proxyPass' in that above value of the variable item.

If you want to simply reference your proxyPass variable then just remove the 'item.' prefix from the front of it:

{{ proxyPass.dev }}

0
votes

In your situation there's no need to use with_items (that said, nothing stops you from doing it) as proxyPass and proxy_apache_vhosts_dev are two separate variables. If you want to access proxyPass using {{ item.proxyPass.dev }} you could define your vars this way:

proxy_apache_vhosts_dev:
    - {
         servername: www.DOMAIN.com ,
         machinename: domainsrv,
         documentroot: /var/www/html,
         vhost_ip: 1.2.3.4,
         vhost_name: DOMAIN.com.conf,
         serveradmin: "[email protected]",
         proxyPass: {
            dev: DOMAINdev,
            qual: DOMAINqual,
            prod: DOMAINprod
            }
     }

With this definition, proxyPass will just be another element of the proxy_apache_vhosts_dev var, and you will be able to access it as {{ item.proxyPass.dev if using with_items or as {{ proxy_apache_vhosts_dev.proxyPass.dev }} if not using with_items