Skip to content

Instantly share code, notes, and snippets.

@gbranchaudrubenovitch
Last active December 22, 2016 09:35
Show Gist options
  • Save gbranchaudrubenovitch/d2a13ac201fbe9c6de40 to your computer and use it in GitHub Desktop.
Save gbranchaudrubenovitch/d2a13ac201fbe9c6de40 to your computer and use it in GitHub Desktop.
Some ansible hash-objects samples...
# in your group vars
HOST_SUFFIX: "dev.apps"
SERVICE_LIST:
- {serviceName: "my_service1", destinationUrl: "http://some-random-dev-machine:5555/"}
- {serviceName: "admin.my_service1", destinationUrl: "http://some-random-dev-machine:5556/"}
- {serviceName: "other-service2", destinationUrl: "http://another-machine-another-port:4279/"}
# in your playbook's yaml
...
roles:
- { role: role-consuming-a-list-of-hash-objects, tags: commons }
# in your role's main.yaml
- name: Generate Nginx host configuration from ansible template
template: src=some-template.j2 dest=/etc/nginx/conf.d/{{item.serviceName}}.conf owner=root group=root mode=0644
with_items: "{{SERVICE_LIST}}"
# in your some-template.j2
server {
access_log /var/log/nginx/{{item.serviceName}}-access.log main;
server_name {{item.serviceName}}.{{HOST_SUFFIX}};
proxy_cookie_domain ~.+ {{HOST_SUFFIX}}; # when a domain is specified, reroute it to the public-facing domain
listen 80;
listen 443 ssl;
ssl_certificate /somewhere/my-secret.crt;
ssl_certificate_key /somewhere/my-secret.key;
location / {
proxy_pass {{item.destinationUrl}};
}
}
# note: after running, this will produce one nginx *.conf file for each entry in the list (i.e. my_service1.conf, admin.my_service1.conf, other-service2.conf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment