Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ansible, how can I combine a default dictionary in a role with a dictionary passed to that role as an argument?

In Ansible, how can I combine a default dictionary in a role with a dictionary passed to that role as an argument?

like image 746
Derek Mahar Avatar asked Dec 20 '16 17:12

Derek Mahar


1 Answers

As a solution by example, consider role nginx-reverse-proxy:

nginx-reverse-proxy/defaults/main.yml:

default_nginx:
  application_context: /{{ application_name }}-{{ application_component_name }}
  reverse_proxy:
    port: 8080

nginx-reverse-proxy/templates/reverse-proxy.conf:

{% set combined_nginx = default_nginx | combine(nginx, recursive=true) -%}
location {{ combined_nginx.application_context }} {
        proxy_pass http://localhost:{{ combined_nginx.reverse_proxy.port }};
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

my-nginx-reverse-proxy-meta/main.yml:

dependencies:
  - selfcertification-service
  - role: nginx-reverse-proxy
    nginx: { reverse_proxy: { port: 8095 } }
like image 127
Derek Mahar Avatar answered Nov 14 '22 21:11

Derek Mahar