Using Netbox Ansible Modules

微笑、不失礼 提交于 2020-07-10 10:25:09

问题


I've been wanting to try out Ansible modules available for Netbox [1].

However, I find myself stuck right in the beginning.

Here's what I've tried:

Add prefix/VLAN to netbox [2]:

cat setup-vlans.yml 
---
- hosts: netbox 
  
  tasks:
    - name: Create prefix 192.168.10.0/24 in Netbox 
      netbox_prefix:
        netbox_token: "{{ netbox_token }}"
        netbox_url: "{{ netbox_url }}"
        data:
          prefix: 192.168.10.0/24
        state: present

That gives me the following error:

ansible-playbook setup-vlans.yml 

PLAY [netbox] *********************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************
ok: [NETBOX]

TASK [Create prefix 192.168.10.0/24 in Netbox] ************************************************************************************************
fatal: [NETBOX]: FAILED! => {"changed": false, "msg": "Failed to establish connection to Netbox API"}

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

Can someone please point me where I am going wrong?

Note: The NetBox URL is an https://url setup with nginx and netbox-docker [3].

Thanks & Regards, Sana

[1] https://github.com/netbox-community/ansible_modules

[2] https://docs.ansible.com/ansible/latest/modules/netbox_prefix_module.html

[3] https://github.com/netbox-community/netbox-docker


回答1:


All playbooks using API modules like netbox (but this is the same for gcp or aws) must use as host not the target but the host that will execute the playbook to call the API. Most of the time this is localhost, but that can be also a dedicated node like a bastion.

You can see in the example on the documentation you linked that it uses hosts: localhost.

Hence I think your playbook should be

---
- hosts: localhost
  connection: local
  gather_facts: False
  
  tasks:
    - name: Create prefix 192.168.10.0/24 in Netbox 
      netbox_prefix:
        netbox_token: "{{ netbox_token }}"
        netbox_url: "{{ netbox_url }}"
        data:
          prefix: 192.168.10.0/24
        state: present



回答2:


While I could see the requests in the nginx access log when using requests library or pynetbox api: https://pastebin.com/LyLw7svm, the same was not true when using the Ansible modules.

xx.xx.xx.xx - - [08/Jul/2020:06:43:03 +0000] "GET / HTTP/1.1" 200 4342 "-" "python-requests/2.23.0"
xx.xx.xx.xx - - [08/Jul/2020:06:45:00 +0000] "GET /api/ HTTP/1.1" 200 403 "-" "python-requests/2.23.0"
xx.xx.xx.xx - - [08/Jul/2020:06:45:28 +0000] "GET /api/dcim/devices/ HTTP/1.1" 200 52 "-" "python-requests/2.23.0"

Also tried to test it with Netbox Ansible Collections as documented [1]

$ cat ansible.cfg 
[defaults]
inventory = ./inventory
collections_paths = ./collections

$ cat inventory 
NETBOX ansible_host=xx.xx.xx.xx ansible_ssh_user=user

[netbox]
NETBOX

$ cat group_vars/all.yml 
---
netbox_url: https://netbox.url
netbox_token: XXX

$ cat collections/requirements.yml 
collections:
  - name: netbox.netbox
    source: https://galaxy.ansible.com

$ ansible-galaxy collection install -r collections/requirements.yml
Process install dependency map
Starting collection install process
Installing 'netbox.netbox:0.2.3' to '/Users/myuser/Desktop/ansible/netbox/collections/ansible_collections/netbox/netbox'
Installing 'ansible.netcommon:1.0.0' to '/Users/myuser/Desktop/ansible/netbox/collections/ansible_collections/ansible/netcommon'
Installing 'community.general:0.2.1' to '/Users/myuser/Desktop/ansible/netbox/collections/ansible_collections/community/general'
Installing 'google.cloud:0.10.1' to '/Users/myuser/Desktop/ansible/netbox/collections/ansible_collections/google/cloud'
Installing 'ansible.posix:1.0.0' to '/Users/myuser/Desktop/ansible/netbox/collections/ansible_collections/ansible/posix'
Installing 'community.kubernetes:0.11.1' to '/Users/myuser/Desktop/ansible/netbox/collections/ansible_collections/community/kubernetes'

$ cat setup-vlans.yml 
---
- hosts: localhost
  connection: local
  gather_facts: False
  collections:
    - netbox.netbox

  tasks:
    - name: Printing environment variable REQUESTS_CA_BUNDLE
      debug:
        msg: "{{ lookup('env','REQUESTS_CA_BUNDLE') }}"

    - name: Create prefix within Netbox with only required information
      netbox_prefix:
        netbox_url: "{{ netbox_url }}"
        netbox_token: "{{ netbox_token }}"
        data:
          prefix: 10.156.0.0/19
        state: present

Yet, the task fails with Failed to establish connection to Netbox API

The full traceback is:
  File "/var/folders/ql/27zxd8fj19qgj2cbd4npxyt8yc8zzn/T/ansible_netbox_prefix_payload_k8j36mj6/ansible_netbox_prefix_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_utils.py", line 417, in _connect_netbox_api
    nb = pynetbox.api(url, token=token, ssl_verify=ssl_verify)
fatal: [localhost]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "data": {
                "custom_fields": null,
                "description": null,
                "family": null,
                "is_pool": null,
                "parent": null,
                "prefix": "10.156.0.0/19",
                "prefix_length": null,
                "prefix_role": null,
                "site": null,
                "status": null,
                "tags": null,
                "tenant": null,
                "vlan": null,
                "vrf": null
            },
            "first_available": false,
            "netbox_token": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "netbox_url": "https://netbox.url",
            "query_params": null,
            "state": "present",
            "validate_certs": true
        }
    },
    "msg": "Failed to establish connection to Netbox API"
}

Am I missing something?

[1] https://github.com/netbox-community/ansible_modules#how-to-use



来源:https://stackoverflow.com/questions/62768218/using-netbox-ansible-modules

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!