Skip to content

Instantly share code, notes, and snippets.

@lae
Last active June 13, 2017 22:30
Show Gist options
  • Save lae/572a5c1e2a2884b5689b703bfe110e63 to your computer and use it in GitHub Desktop.
Save lae/572a5c1e2a2884b5689b703bfe110e63 to your computer and use it in GitHub Desktop.
Looping through Ansible/YAML dict for Django configuration
# Ansible managed
DATE_FORMAT = "N j, Y"
BANNER_BOTTOM = "Your banner text"
LOGIN_REQUIRED = True
MAX_PAGE_SIZE = 500
PREFER_IPV4 = False
PAGINATE_COUNT = 100
BASE_PATH = "netbox/"
TIME_ZONE = "UTC"
ADMINS = [
[
"Hank Hill",
"hhill@example.com"
],
[
"Dale Gribble",
"dgribble@example.com"
]
]
BANNER_TOP = "Your banner text"
MAINTENANCE_MODE = False
DEBUG = True
ALLOWED_HOSTS = [
"127.0.0.1",
"localhost"
]
SECRET_KEY = "fmn97pa3thli3uawhtoiantg3a"
CORS_ORIGIN_ALLOW_ALL = False
SHORT_DATE_FORMAT = "Y-m-d"
EMAIL = {
"FROM_EMAIL": "notifications@localhost",
"PASSWORD": "password",
"PORT": 25,
"SERVER": "localhost",
"TIMEOUT": 10,
"USERNAME": "user@localhost"
}
# {{ ansible_managed }}
{% for setting, value in netbox_config.iteritems() %}
{% if value in [True, False] %}
{{ setting }} = {{ 'True' if value else 'False' }}
{% else %}
{{ setting }} = {{ value | to_nice_json }}
{% endif %}
{% endfor %}
]$ ansible-playbook -i localhost, template_configuration.yml
PLAY [localhost] ******************************************************************************************************************************************************************************************************************************
TASK [Generate LDAP configuration for NetBox if enabled] **************************************************************************************************************************************************************************************
changed: [localhost]
PLAY RECAP ************************************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0
---
- hosts: localhost
connection: local
gather_facts: False
tasks:
- name: Generate LDAP configuration for NetBox if enabled
template:
src: "templates/configuration.py.j2"
dest: "/tmp/configuration.py"
vars:
netbox_config:
SECRET_KEY: "fmn97pa3thli3uawhtoiantg3a"
ALLOWED_HOSTS:
- 127.0.0.1
- localhost
ADMINS:
-
- Hank Hill
- hhill@example.com
-
- Dale Gribble
- dgribble@example.com
BANNER_TOP: &BANNER_TOP 'Your banner text'
BANNER_BOTTOM: *BANNER_TOP
BASE_PATH: netbox/
CORS_ORIGIN_ALLOW_ALL: no
DEBUG: yes
EMAIL:
SERVER: localhost
PORT: 25
USERNAME: user@localhost
PASSWORD: password
TIMEOUT: 10
FROM_EMAIL: notifications@localhost
LOGIN_REQUIRED: True
MAINTENANCE_MODE: false
MAX_PAGE_SIZE: 500
PAGINATE_COUNT: 100
PREFER_IPV4: no
TIME_ZONE: UTC
DATE_FORMAT: N j, Y
SHORT_DATE_FORMAT: Y-m-d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment