xoanmi / ansible-module-infoblox

Awesome infobox module for ansible
GNU General Public License v3.0
38 stars 25 forks source link

getting 'Exception: IP address for the Host record is missing.' when using Ansible to add host #35

Open jnc-cnj opened 7 years ago

jnc-cnj commented 7 years ago

Hi, I am testing Ansible module and I am able to get host. However, when adding a host, it failed. Here's what I have:

Playbook:

---
- name: query DNS
  hosts: localhost
  connection: local
  gather_facts: no
  strategy: debug
  tasks:
  - name: find out a DNS record
    infoblox:
        server: x.x.x.x
        username: user1
        password: xxxxxx
        api_version: 1.6.1
        action: add_host
        host: testhost1.nonexist.com
        address: 10.20.30.41
        dns_view: default
    register: result
  - name: show output
    debug:
     var: result

error:


ubuntu@ubuntu-xenial:~/ansible/infoblox$ ansible-playbook ./playbooks/infoblox.yaml -vvv
ansible-playbook 2.4.0.0
config file = /home/ubuntu/ansible/infoblox/ansible.cfg
configured module search path = [u'/usr/local/lib/python2.7/dist-packages/ansible/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
Using /home/ubuntu/ansible/infoblox/ansible.cfg as config file
Parsed /home/ubuntu/ansible/infoblox/ansible_inventory inventory source with ini plugin
PLAYBOOK: infoblox.yaml ************************************************************************************************************
1 plays in ./playbooks/infoblox.yaml
PLAY [query DNS] *******************************************************************************************************************
META: ran handlers
TASK [find out a DNS record] *******************************************************************************************************
task path: /home/ubuntu/ansible/infoblox/playbooks/infoblox.yaml:8
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/ansible-module-infoblox-master/infoblox.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: ubuntu
<127.0.0.1> EXEC /bin/sh -c 'echo ~ && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/ubuntu/.ansible/tmp/ansible-tmp-1508532893.25-41509055676241 `" && echo ansible-tmp-1508532893.25-41509055676241="` echo /home/ubuntu/.ansible/tmp/ansible-tmp-1508532893.25-41509055676241 `" ) && sle\
ep 0'
<127.0.0.1> PUT /tmp/tmpbg_Pb9 TO /home/ubuntu/.ansible/tmp/ansible-tmp-1508532893.25-41509055676241/infoblox.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/ubuntu/.ansible/tmp/ansible-tmp-1508532893.25-41509055676241/ /home/ubuntu/.ansible/tmp/ansible-tmp-1508532893.25-41509055676241/infoblox.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python /home/ubuntu/.ansible/tmp/ansible-tmp-1508532893.25-41509055676241/infoblox.py; rm -rf "/home/ubuntu/.ansible/tmp/ansible-tmp-1508532893.25-41509055676241/" > /dev/null 2>&1 && sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible__Our2U/ansible_module_infoblox.py", line 2205, in <module>
main()
File "/tmp/ansible__Our2U/ansible_module_infoblox.py", line 1919, in main
host, address, None, comment, ttl, extattrs)
File "/tmp/ansible__Our2U/ansible_module_infoblox.py", line 1068, in create_host_record
return self.invoke("post", "record:host", ok_codes=(200, 201, 400), json=model)
File "/tmp/ansible__Our2U/ansible_module_infoblox.py", line 229, in invoke
raise Exception(payload["text"])
Exception: IP address for the Host record is missing.
fatal: [localhost]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n File \"/tmp/ansible__Our2U/ansible_module_infoblox.py\", line 2205, in <module>\n main()\n File \"/tmp/ansible__Our2U/ansible_module_infoblox.py\", line 1919, in main\n host, address, None, comment,\
ttl, extattrs)\n File \"/tmp/ansible__Our2U/ansible_module_infoblox.py\", line 1068, in create_host_record\n return self.invoke(\"post\", \"record:host\", ok_codes=(200, 201, 400), json=model)\n File \"/tmp/ansible__Our2U/ansible_module_infoblox.py\", line 22\
9, in invoke\n raise Exception(payload[\"text\"])\nException: IP address for the Host record is missing.\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

debug output:

(debug) p task.args
{u'action': u'add_host',
 u'address': u'10.20.30.41',
 u'api_version': u'1.6.1',
 u'dns_view': u'default',
 u'host': u'testhost1.nonexist.com',
 u'password': u'xxxxx',
 u'server': u'x.x.x.x',
 u'username': u'user1'}

I think what happened is: This is the infoblox API:

{"name":"host.info.com",
"ipv4addrs":
[{"ipv4addr":"172.26.1.20",
"mac":"aa:bb:cc:11:22:21"}]}

This is the code in infoblox.py module.

 def create_host_record(self, host, address, network_ref=None, comment=None, ttl=None, extattrs=None):
 ...<snip>...
        model = {_NAME_PROPERTY: host, _IPV4_ADDRESS_PROPERTY: [{_IPV4_ADDRESS_PROPERTY: address}],
                 _VIEW_PROPERTY: self.dns_view,
                 _COMMENT_PROPERTY: comment, _EXT_ATTR_PROPERTY: extattrs}
...<snip>...

Seems the model wasn't built correctly. It builds a record like this: "ipv4addr": [{"ipv4adr": "x.x.x.x"}].

I modified the infoblox.py file and it's working for me now. However, I think this might affect other functions as well. Since I have very limited knowledge of coding, I am open this issue to let professional to take a look and fix it.

davisionz commented 7 years ago

@jnc-cnj can you elaborate on what you changed in the infoblox.py ?

jnc-cnj commented 7 years ago

@davisionz sure. I add a new variable

_IPV4_ADDRESS = "ipv4addrs" <<<<<<<<<<<<
_IPV4_ADDRESS_PROPERTY = "ipv4addr"

added it to the list


        self.model_list = [_COMMENT_PROPERTY, _TTL_PROPERTY, _USE_TTL_PROPERTY, _NAME_PROPERTY,
                           _VIEW_PROPERTY, _IPV4_ADDRESS, _IPV4_ADDRESS_PROPERTY, _IPV6_ADDRESS_PROPERTY,
                           _ID_PROPERTY, _PTRDNAME_PROPERTY, _EXT_ATTR_PROPERTY, _TXT_PROPERTY,
                           _PORT_PROPERTY, _PRIORITY_PROPERTY, _WEIGHT_PROPERTY, _TARGET_PROPERTY,
                           _MAC_PROPERTY, _CANONICAL_PROPERTY, _FQDN_PROPERTY, _FORWARD_TO_PROPERTY,
                           _NETWORK_PROPERTY, _NETWORK_VIEW_PROPERTY, _DELEGATE_TO_PROPERTY]

Then change the following in 'create_host_record` to:

        model = {_NAME_PROPERTY: host, _IPV4_ADDRESS: [{_IPV4_ADDRESS_PROPERTY: address}],
                 _VIEW_PROPERTY: self.dns_view,
                 _COMMENT_PROPERTY: comment, _EXT_ATTR_PROPERTY: extattrs}

HTH.

davisionz commented 7 years ago

many thanks!

xoanmi commented 7 years ago

Hi guys!

Could you open a Pull Request with the needed changes to solve the problem?

Thanks!

jnc-cnj commented 7 years ago

@xoanmi Hi, I wish I could help but unfortunately I don't know how. Just start learning coding. And please take a look of other functions as well. They might have the same issue.

dathelen commented 6 years ago

I am still getting this error with this playbook

   tasks:
     - name: add host
       infoblox:
         server: xxxx
         username: xxxx
         password:xxxx
         action: add_host
         api_version: 2.1
         host: "sn1-rsw-a02-40"
         dns_view: INTERNAL
         address: 172.16.0.150
       register: result

With the newest code. Anyone have any ideas how to resovle this?

dathelen commented 6 years ago

this is the current error i am getting:


The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_EcuoTp/ansible_module_infoblox.py", line 2206, in <module>
    main()
  File "/tmp/ansible_EcuoTp/ansible_module_infoblox.py", line 1920, in main
    host, address, None, comment, ttl, extattrs)
  File "/tmp/ansible_EcuoTp/ansible_module_infoblox.py", line 1069, in create_host_record
    return self.invoke("post", "record:host", ok_codes=(200, 201, 400), json=model)
  File "/tmp/ansible_EcuoTp/ansible_module_infoblox.py", line 230, in invoke
    raise Exception(payload["text"])
Exception: The action is not allowed. A parent was not found.

fatal: [localhost]: FAILED! => {
    "changed": false,
    "failed": true,
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_EcuoTp/ansible_module_infoblox.py\", line 2206, in <module>\n    main()\n  File \"/tmp/ansible_EcuoTp/ansible_module_infoblox.py\", line 1920, in main\n    host, address, None, comment, ttl, extattrs)\n  File \"/tmp/ansible_EcuoTp/ansible_module_infoblox.py\", line 1069, in create_host_record\n    return self.invoke(\"post\", \"record:host\", ok_codes=(200, 201, 400), json=model)\n  File \"/tmp/ansible_EcuoTp/ansible_module_infoblox.py\", line 230, in invoke\n    raise Exception(payload[\"text\"])\nException: The action is not allowed. A parent was not found.\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE",
    "rc": 0
}```
dathelen commented 6 years ago

figured it out. The OPs code changed did in fact do the trick the rest were mental errors on my part