saltstack-formulas / powerdns-formula

http://docs.saltstack.com/en/latest/topics/development/conventions/formulas.html
Apache License 2.0
6 stars 29 forks source link

sls.example does not work on its own #10

Closed skinlayers closed 7 years ago

skinlayers commented 7 years ago

_module/powerdns.py mentions passing pdns.url, pdns.server_id, pdns.api_key to a pillar, but I have been unable to determine how. It appears this is required for the sls.example state to work.

records.sls state:

pdnsapi python module:
  pip.installed:
    - name: pdnsapi >= 0.3.0b3
    - reload_modules: True
    - require:
      - cmd: /usr/bin/python /usr/local/sbin/get-pip.py

subdomain.domain.net.present:
  powerdns.zone_present:
    - name: subdomain.domain.net
    - require:
      - pip: pdnsapi >= 0.3.0b3

subdomain.domain.net.SOA:
  powerdns.record_present:
    - zone: subdomain.domain.net
    - name: subdomain.domain.net
    - record_type: 'SOA'
    - records:
      - netsvc.subdomain.domain.net. hostmaster.subdomain.domain.net. 1 10800 3600 604800 300
    - require:
      - powerdns: subdomain.domain.net.present

salt state.apply output:

    Function: powerdns.zone_present
        Name: subdomain.domain.net
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1746, in call
                  **cdata['kwargs'])
                File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1704, in wrapper
                  return f(*args, **kwargs)
                File "/var/cache/salt/minion/extmods/states/powerdns.py", line 29, in zone_present
                  if __salt__['powerdns.zone_exists'](name):
                File "/var/cache/salt/minion/extmods/modules/powerdns.py", line 96, in zone_exists
                  zone = conn.get_zone(name)
                File "/usr/local/lib/python2.7/dist-packages/pdnsapi/api.py", line 251, in get_zone
                  zone_name))
                File "/usr/local/lib/python2.7/dist-packages/pdnsapi/api.py", line 103, in perform_request
                  response = method(url, headers={'X-API-Key': api_key}, data=data)
                File "/usr/lib/python2.7/dist-packages/requests/api.py", line 69, in get
                  return request('get', url, params=params, **kwargs)
                File "/usr/lib/python2.7/dist-packages/requests/api.py", line 50, in request
                  response = session.request(method=method, url=url, **kwargs)
                File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 451, in request
                  prep = self.prepare_request(req)
                File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 382, in prepare_request
                  hooks=merge_hooks(request.hooks, self.hooks),
                File "/usr/lib/python2.7/dist-packages/requests/models.py", line 304, in prepare
                  self.prepare_url(url, params)
                File "/usr/lib/python2.7/dist-packages/requests/models.py", line 362, in prepare_url
                  to_native_string(url, 'utf8')))
              MissingSchema: Invalid URL '/api/v1/servers//zones/subdomain.domain.net': No schema supplied. Perhaps you meant http:///api/v1/servers//zones/subdomain.domain.net?
     Started: 13:13:14.353211
    Duration: 6.897 ms
     Changes:
----------
          ID: subdomain.domain.net.SOA
    Function: powerdns.record_present
        Name: subdomain.domain.net
      Result: False
     Comment: One or more requisite failed: powerdns.records.subdomain.domain.net.present
     Changes:
aboe76 commented 7 years ago

@skinlayers did you have something like this in the pillar of the minion:

pdns.sls

pdns.url: "http://192.168.10.65:8081"
pdns.server_id: "localhost"
pdns.api_key: "f5d2abcd"

top.sls:

base:
  'minion':
    - pdns.sls
skinlayers commented 7 years ago

Ah! That did it! I assumed the pillar structure was:

pdns:
  url: "http://192.168.10.65:8081"
  server_id: "localhost"
  api_key: "f5d2abcd"

It may be worth making a comment in the example pillar to clarify.

skinlayers commented 7 years ago

I came across one other issue with using the powerdns _state and documentation. I could add a SOA and 'A' records, but I got errors when trying to add a 'CNAME' or 'NS' record. I eventually figured out that their records need to end with a period ('.'). So, I recommend adding 'CNAME' and 'NS' record examples to the sls.example state, though this could probably be fixed in the _module. IIRC, there's code in there that will append a '.' when it is needed. Here's what my working pillar and states look like now:

pdnsapi python module:
  pip.installed:
    - name: pdnsapi >= 0.3.0b3
    - reload_modules: True
    - require:
      - cmd: /usr/bin/python /usr/local/sbin/get-pip.py

{% for domain, conf in salt['pillar.get']('powerdns:domains', []).items() %}

{{ domain }}.present:
  powerdns.zone_present:
    - name: {{ domain }}
    - require:
      - pip: pdnsapi >= 0.3.0b3

{{ domain }}.SOA:
  powerdns.record_present:
    - zone: {{ domain }}
    - name: {{ domain }}
    - record_type: 'SOA'
    - records:
      - {{ conf["primary-nameserver"] }}. {{ conf["email"] }}. {{ conf["serial"] }} 10800 3600 604800 300
    - require:
      - powerdns: {{ domain }}.present

{% for ns in conf['ns-records'] %}
{{ domain }}.NS:
  powerdns.record_present:
    - zone: {{ domain }}
    - name: {{ domain }}
    - record_type: 'NS'
    - records:
      - {{ ns }}.
    - require:
      - powerdns: {{ domain }}.present
{% endfor %}

{% for host, ip in conf['a-records'].items() %}
{{ host }}.{{ domain }}.A:
  powerdns.record_present:
    - zone: {{ domain }}
    - name: {{ host }}.{{ domain }}
    - record_type: 'A'
    - records:
      - {{ ip }}
    - require:
      - powerdns: {{ domain }}.present
{% endfor %}

{% for alias, fqdn in conf['cname-records'].items() %}
{{ host }}.{{ domain }}.CNAME:
  powerdns.record_present:
    - zone: {{ domain }}
    - name: {{ host }}.{{ domain }}
    - record_type: 'CNAME'
    - records:
      - {{ fqdn }}.
    - require:
      - powerdns: {{ domain }}.present
{% endfor %}

{% endfor %}

pillar:

powerdns:
  domains:
    zone.domain.net:
      serial: 1
      primary-nameserver: ns1.zone.domain.net
      email: hostmaster.zone.domain.net
      ns-records:
        - ns1.zone.domain.net
      cname-records:
        salt: ns1.zone.domain.net
      a-records:
        debian-8-base: 192.168.99.4
        ns1: 192.168.99.5

{% for index in range(3) %}
        redis-{{ index }}: 192.168.99.1{{ index }}
{% endfor %}

{% for index in range(3) %}
        cassandra-{{ index }}: 192.168.99.1{{ index + 5 }}
{% endfor %}
bentwire commented 7 years ago

@skinlayers You should consider submitting your state as a patch, good stuff! Perhaps submit it as manage_zones.sls or something? Only thing I would do different is change 'domains' to 'zones', but that is just a nit really.