Ansible to Create Anything in Netbox

瀏覽次數:901 次
跳到第一則未讀訊息

Scrounger117

未讀,
2022年1月8日 下午6:50:082022/1/8
收件者:NetBox
I'm having difficulty getting Ansible to create anything in Netbox.  I have reduced a playbook down to the bare minimum in an attempt to get this to work.   Some quick details with everything installed and running fine on the same local host.

OS: Ubuntu 20.04
WEB: Apache2
Netbox 3.1.5
Ansible 5.1

Here is my basic playbook:

---
- name: Created Static Region in Netbox
  gather_facts: False
  hosts: localhost

  tasks:
    - name: "Example state: present - Update"
      netbox.netbox.netbox_region:
        netbox_url: "https://hostname"
        netbox_token: "token"
        data:
          parent_region:
          name: California
          slug: california
        state: "present"

When attempting to run this playbook:

ansible-playbook cust_nbx_create_region.yaml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Created Static Region in Netbox] ************************************************************************************************************************************************************

TASK [Example state: present - Update] *********************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Must have pynetbox >=4.1.0"}

PLAY RECAP *************************************************************************************************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0 

I have tried installing pynetbox 4.1.0 and get the same message, currently have 5.1.0 installed. 

FULL TRACE:
The full traceback is:
  File "/tmp/ansible_netbox.netbox.netbox_region_payload_ecc0m_8_/ansible_netbox.netbox.netbox_region_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_utils.py", line 603, in _connect_netbox_api
    self.full_version = nb.status().get("netbox-version")
fatal: [localhost]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "cert": null,
            "data": {
                "name": "California",
                "parent_region": null,
                "slug": "california"
            },
            "netbox_token": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "netbox_url": "https://hostname",
            "query_params": null,
            "state": "present",
            "validate_certs": true
        }
    },
    "msg": "Must have pynetbox >=4.1.0"
}

Please help.

Brian Candler

未讀,
2022年1月9日 上午10:54:332022/1/9
收件者:NetBox
I believe this is the code that generates that error:

I suggest you find netbox_utils.py on your system (on my system it's ~/.ansible/collections/ansible_collections/netbox/netbox/plugins/module_utils/netbox_utils.py), and change


except AttributeError:

self.module.fail_json(msg="Must have pynetbox >=4.1.0")


to

except AttributeError:

raise
#self.module.fail_json(msg="Must have pynetbox >=4.1.0")

Then we should get a proper backtrace, and a proper description of what the error is.  That's assuming you can get a backtrace: maybe one or more -v flags to ansible-playbook will be required.

Scrounger117

未讀,
2022年1月9日 下午2:30:092022/1/9
收件者:NetBox
Brian,

I did comment out that msg, and re-ran the playbook.  Seems to point now to an issue connecting to the API.

admin@host$ ansible-playbook cust_nbx_create_region.yaml -vvv
ansible-playbook [core 2.12.1]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/admin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/admin/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible-playbook
  python version = 3.8.10 (default, Nov 26 2021, 20:14:08) [GCC 9.3.0]
  jinja version = 3.0.3
  libyaml = True
Using /etc/ansible/ansible.cfg as config file
host_list declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
script declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
auto declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Parsed /etc/ansible/hosts inventory source with ini plugin

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.

PLAYBOOK: cust_nbx_create_region.yaml **********************************************************************************************************************************************************************
1 plays in cust_nbx_create_region.yaml

PLAY [Created Static Region in Customer Netbox] ************************************************************************************************************************************************************
META: ran handlers


TASK [Example state: present - Update] *********************************************************************************************************************************************************************
task path: /home/admin/cust_nbx_create_region.yaml:9
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: admin
<127.0.0.1> EXEC /bin/sh -c 'echo ~admin && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/admin/.ansible/tmp `"&& mkdir "` echo /home/admin/.ansible/tmp/ansible-tmp-1641753792.1518397-33237-180578382023952 `" && echo ansible-tmp-1641753792.1518397-33237-180578382023952="` echo /home/admin/.ansible/tmp/ansible-tmp-1641753792.1518397-33237-180578382023952 `" ) && sleep 0'
Using module file /home/admin/.ansible/collections/ansible_collections/netbox/netbox/plugins/modules/netbox_region.py
<127.0.0.1> PUT /home/admin/.ansible/tmp/ansible-local-332328zbsaejw/tmpqsebk_8v TO /home/admin/.ansible/tmp/ansible-tmp-1641753792.1518397-33237-180578382023952/AnsiballZ_netbox_region.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/admin/.ansible/tmp/ansible-tmp-1641753792.1518397-33237-180578382023952/ /home/admin/.ansible/tmp/ansible-tmp-1641753792.1518397-33237-180578382023952/AnsiballZ_netbox_region.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python3 /home/admin/.ansible/tmp/ansible-tmp-1641753792.1518397-33237-180578382023952/AnsiballZ_netbox_region.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/admin/.ansible/tmp/ansible-tmp-1641753792.1518397-33237-180578382023952/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
  File "/tmp/ansible_netbox_region_payload__t5bh25v/ansible_netbox_region_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_utils.py", line 604, in _connect_netbox_api

    self.full_version = nb.status().get("netbox-version")
fatal: [localhost]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "cert": null,
            "data": {
                "name": "California",
                "parent_region": null,
                "slug": "california"
            },
            "netbox_token": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "netbox_url": "https://hostname",
            "query_params": null,
            "state": "present",
            "validate_certs": true
        }
    },
    "msg": "Failed to establish connection to NetBox API"

}

PLAY RECAP *************************************************************************************************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0  

So, now I'm not sure that I have ansible working correctly for this or not.  The playbook is defined to only run on localhost, but connect to the defined URL.  The URL is working just fine from a browser, also created a small python file to verify that it works on that host to connect to the netbox API

cat nbxconnect.py
#!/usr/bin/env python3

import sys, json
import pynetbox

netboxsvr=pynetbox.api('https://hostname', token='token', threading=True)
print(netboxsvr)

Execute the file:
./nbxconnect.py
<pynetbox.core.api.Api object at 0x7fefab555b20>

So, still trying to narrow down where in the communication chain this is broken.

Scrounger117

未讀,
2022年1月9日 下午2:47:022022/1/9
收件者:NetBox
So, as I was working through some things, I found that the pynetbx version is much newer, so I installed pynetbox==6.5.0.  And now that it works...sort of.  I was able to successfully execute the playbook as originally posted in this thread; however, now it appears that the inclusion of description is a step too far to expect :-(

---
- name: Created Static Region in Netbox
  gather_facts: False
  hosts: localhost
  collections:
    - netbox.netbox


  tasks:
    - name: "Example state: present - Update"
      netbox_region:
        netbox_url: "https://hostname"
        netbox_token: "token"
        data:
          parent_region:
          name: California
          slug: california
          description: "State of California"
        state: "present"


ansible-playbook cust_nbx_create_region.yaml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Created Static Region in Customer Netbox] ************************************************************************************************************************************************************


TASK [Example state: present - Update] *********************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (netbox_region) module: data.description. Supported parameters include: cert, state, query_params, netbox_url, data, validate_certs, netbox_token."}


PLAY RECAP *************************************************************************************************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0  

Kinda ridiculous to have these types of issues, where the description (which can be imported through the UI, or if I write my own python script) doesn't allow for all of the possible parameters to be created/updated via Ansible.  If this is what I can expect to encounter with builtin fields, I can only imagine all of the issues that I will have trying to populate custom fields.

Scrounger117

未讀,
2022年1月9日 下午2:56:222022/1/9
收件者:NetBox
The lack of description field seems to be rooted here:  ansible_modules/netbox_region.py at devel · netbox-community/ansible_modules (github.com)

Martin Rødvand

未讀,
2022年1月9日 下午5:57:142022/1/9
收件者:NetBox
Just create an issue in the ansible_modules repository - https://github.com/netbox-community/ansible_modules so that we can add it to the module. Should be an easy enough addition.

Scrounger117

未讀,
2022年1月9日 下午6:22:462022/1/9
收件者:NetBox

Scrounger117

未讀,
2022年1月10日 晚上7:38:222022/1/10
收件者:NetBox
I can confirm that this issue 688 and 689 have been resolved.
回覆所有人
回覆作者
轉寄
0 則新訊息