Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way that individual zones can be defined in a separate file? #62

Closed
WRJFontenot opened this issue Aug 2, 2018 · 8 comments
Closed

Comments

@WRJFontenot
Copy link

bind_zone_domain is manageable if you have a small number of zones and a small number of hosts in each zone. It becomes problematic if you have a large number of zones and hosts in each zone. Is there a way to have the zones/hosts defined in separate files to improve manageability?

@WRJFontenot
Copy link
Author

WRJFontenot commented Aug 3, 2018

I got this to work, so I'll share it with everyone. First create a dict file for each zone in a subdirectory named "zones". Use include_vars to read all the files in that directory, then merge all the dicts into bind_zone_domains as a pre_task. Separating the zones will make it easier to manage.

$HOME/zones/adfstest.example.com.yml

---
adfstest_example_com:
  - name: adfstest.example.com
    hostmaster_email: hostmaster
    name_servers:
      - ns2-us-east-1.test.com.
      - ns2-eu-central-1.test.com.
      - ns2-ap-southeast-1.test.com.
    also_notify:
      - 192.168.0.1
      - 192.168.1.1
    hosts:
      - name: ''
        ip:
          - 200.2.1.1
          - 200.3.1.2
...

$HOME/zones/eratest.example.com.yml

---
eratest_example_com:
  - name: eratest.example.com
    hostmaster_email: hostmaster
    name_servers:
      - ns2-us-east-1.test.com.
      - ns2-eu-central-1.test.com.
      - ns2-ap-southeast-1.test.com.
    also_notify:
      - 192.168.0.1
      - 192.168.1.1
    hosts:
      - name: ''
        ip:
          - 20.11.4.25
          - 11.20.204.22
...

$HOME/playbook.yml:

---
- hosts: localhost
  become: yes
  roles:
    - ansible-role-bind
  vars:
    bind_listen_ipv4:
      - 127.0.0.1
      - 192.168.2.1
    bind_zone_master_server_ip: 192.168.2.1
  pre_tasks:
  - name: Get dict for each zone
    include_vars:
      dir: zones
  - name: Merge zone dicts
    set_fact:
      bind_zone_domains:
        "{{ adfstest_example_com }} +
         {{ eratest_example_com }}"
...

@WRJFontenot
Copy link
Author

Is there a more elegant way to do this?

@BulatSaif
Copy link

@WRJFontenot, my workaround is the same as yours, but without pre_tasks:

group_vars/bind_server/main.yml

bind_zone_domains:
  - name: test.com
    name_servers: "{{ bind_defualt_name_servers }}"
    hosts: "{{ bind_defualt_zone_hosts + bind_test_zone_hosts }}"
  - name: prod.com
    networks: ['192.168']
    name_servers: "{{ bind_defualt_name_servers }}"
    hosts: "{{ bind_defualt_zone_hosts + bind_prod_zone_hosts }}"

bind_defualt_name_servers: ['ns1', 'ns2']
bind_defualt_zone_hosts:
  - name: ns1
    ip: "{{ bind_zone_master_server_ip }}"
  - name: ns2
    ip: "{{ bind_zone_slave_server_ip }}"

group_vars/bind_server/prod.yml

bind_prod_zone_hosts:
  - name: 'app'
    ip: "{{ app_prod_ip }}"
  - name: '*'
    ip: "{{ nginx_ip }}"

group_vars/bind_server/test.yml

bind_test_zone_hosts
  - name: 'app'
    ip: "{{ app_test_ip }}"
  - name: '*'
    ip: "{{ nginx_dev_ip }}"

@bertvv
Copy link
Owner

bertvv commented Oct 5, 2018

Would it help to allow the user to specify a list of files containing dicts that will be imported when defined?

@BenBE
Copy link

BenBE commented Oct 28, 2018

I'd actually prefer if I could hand this role a (local/templated) file already containing the full zone file (pre-rendered).

@bertvv
Copy link
Owner

bertvv commented Oct 28, 2018 via email

@mobilesfinks
Copy link

mobilesfinks commented Apr 18, 2019

I have same problem with many domains.
I try to split bind_zone_domains to separate files.

The problem with the above implementation is that the number of domains may change and I don’t want to edit the role code or play hands every time i add new domain.
So, i try to load vars by ansible module include_vars

- name: Load vars from dirs
  include_vars: 
    dir: "{{inventory_dir}}/group_vars/DNS/DNS_MASTER/zones"
    extensions: ['yml']
    name: "bind_zone_domains"
  register: vars_matched
  tags: ['bind']

- name: set dict2
  set_fact:
    dict2: "{{vars_matched.ansible_facts.bind_zone_domains }}"
  tags: ['bind']

- name: set facts
  set_fact:
    zones_prepared: "{{ zones_prepared |default([]) | union([item.value]) }}"
  with_dict: "{{ dict2 }}"
  tags: ['bind']

- name: set facts
  set_fact:
    bind_zone_domains: "{{ zones_prepared }}"
  tags: ['bind']

I have zone files in

{{inventory_dir}}/group_vars/DNS/DNS_MASTER/zones

Where DNS and DNS_MASTER parent and child groups

$ group_vars/DNS/DNS_MASTER/zones/test_ru.zone.yml

"test1":
  name: "test.ru"
  hostmaster_email: "hostmaster"
  allow_update: none

$ group_vars/DNS/DNS_MASTER/zones/test2_ru.zone.yml

"test2":
  name: "test2.ru"
  hostmaster_email: "hostmaster"
  allow_update: none

Everything works as if you are setting everything up in one variable.

@blofeldthefish
Copy link
Collaborator

@WRJFontenot @mobilesfinks there are great alternative solutions here.

Please consider submitting a PR, with a document showing how to handle a large number of seperate zone files/domains.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants