NETBOX как dynamic inventory для Ansible — различия между версиями
Root (обсуждение | вклад) |
Root (обсуждение | вклад) |
||
| (не показаны 24 промежуточные версии этого же участника) | |||
| Строка 1: | Строка 1: | ||
| + | Для начала подготовим виртуалочку для управления инфрой через ansible. Использовать будем Debian 12 | ||
| + | |||
| + | Ставим python3.11 (т.к. он будет в зависимостях в нашем Pipfile) и pip | ||
| + | apt install python3.11-full | ||
| + | apt install pip | ||
| + | |||
| + | |||
| + | Устанавливаем pipenv | ||
| + | pip install pipenv --break-system-packages | ||
| + | |||
| + | |||
| + | Подготавливаем ansible.cfg и Pipfile с зависимостями<br> | ||
| + | ansible.cfg<br><spoiler> | ||
| + | [defaults] | ||
| + | roles_path = ../../roles:roles<br> | ||
| + | callback_plugins = /root/ansible/ansible_collection/ansible_collections<br> | ||
| + | interpreter_python = /usr/bin/python3<br> | ||
| + | force_valid_group_names = ignore<br> | ||
| + | host_key_checking = False<br> | ||
| + | stdout_callback = yaml<br> | ||
| + | |||
| + | timeout = 20<br> | ||
| + | config_context = True<br> | ||
| + | fact_caching = jsonfile<br> | ||
| + | fact_caching_connection = inventory/.cache<br> | ||
| + | fact_caching_timeout = 86400<br> | ||
| + | |||
| + | module_defaults:<br> | ||
| + | ansible.builtin.setup:<br> | ||
| + | gather_timeout: 10<br> | ||
| + | ansible.builtin.gather_facts:<br> | ||
| + | gather_timeout: 10<br> | ||
| + | |||
| + | [ssh_connection]<br> | ||
| + | scp_if_ssh = True<br> | ||
| + | delay=10<br> | ||
| + | retries=2<br> | ||
| + | pipelining=True<br> | ||
| + | ssh_args = -o ControlMaster=auto -o ControlPersist=5m -o ConnectionAttempts=3 -o UserKnownHostsFile=/dev/null<br> | ||
| + | </spoiler> | ||
| + | |||
| + | Pipfile<br> | ||
| + | <spoiler> | ||
| + | [ [source] ] | ||
| + | url = "https://pypi.org/simple"<br> | ||
| + | verify_ssl = true<br> | ||
| + | name = "pypi"<br> | ||
| + | |||
| + | [packages]<br> | ||
| + | requests = "==2.26.0"<br> | ||
| + | proxmoxer = "==2.0.1"<br> | ||
| + | ansible = "==9.7.0"<br> | ||
| + | ansible-lint = "==6.14.4"<br> | ||
| + | hvac = "==0.11.2"<br> | ||
| + | dnspython = "==2.3.0"<br> | ||
| + | pyvcloud = "==23.0.4"<br> | ||
| + | lxml = "==4.9.2"<br> | ||
| + | pynetbox = "==6.6.2"<br> | ||
| + | zabbix-api = "==0.5.5"<br> | ||
| + | pure-sasl = "==0.5.1"<br> | ||
| + | netaddr = "==0.8.0"<br> | ||
| + | pytz = "==2023.3"<br> | ||
| + | "molecule[ansible]" = "==4.0.1"<br> | ||
| + | molecule-proxmox = "==0.7.0"<br> | ||
| + | ansible-compat = "==25.1.4"<br> | ||
| + | "requests[socks]"="==2.26.0"<br> | ||
| + | pywinrm = "==0.5.0" | ||
| + | setuptools = "==80.9.0" | ||
| + | |||
| + | [dev-packages] | ||
| + | |||
| + | [requires]<br> | ||
| + | python_version = "3.11"<br> | ||
| + | </spoiler> | ||
| + | Создадим директории и положим конфиги и прочее | ||
| + | /inventories | ||
| + | /iwad | ||
| + | /group_vars | ||
| + | all.yaml | ||
| + | netbox-inventory.yaml | ||
| + | /playbooks | ||
| + | /base | ||
| + | ansible.cfg | ||
| + | base.yaml | ||
| + | Pipfile | ||
| + | /roles | ||
| + | /base | ||
| + | |||
| + | |||
| + | Клонируем репу с базовой ролью | ||
| + | |||
| + | Создаём виртуальное окружение(поставим пару пакетов которые пригодятся) | ||
| + | cd /root/ansible/playbooks/base | ||
| + | pipenv install | ||
| + | pipenv shell | ||
| + | pip install dotenv loguru | ||
| + | |||
| + | |||
| + | Переходим к подключению динамического инвентаря. Приводим <code>/inventories/iwad/netbox-inventory.yaml</code> примерно к такому виду | ||
--- | --- | ||
plugin: netbox.netbox.nb_inventory | plugin: netbox.netbox.nb_inventory | ||
api_endpoint: https://netbox.iwad.ru | api_endpoint: https://netbox.iwad.ru | ||
| − | token: | + | token: fc58b84c25grbc5506627a8c32r32a397213ee |
validate_certs: false | validate_certs: false | ||
config_context: false | config_context: false | ||
| Строка 22: | Строка 121: | ||
| − | https://netboxlabs.com/docs/integrations/tool-integrations/netbox-ansible-collection/#use-case-1---netbox-as-a-dynamic-inventory-source-for-ansible | + | Проверяем что инвентарь парсится(конечно же в netbox уже должны быть внесены устройства) |
| − | https://docs.ansible.com/projects/ansible/latest/collections/netbox/netbox/nb_inventory_inventory.html | + | ansible-inventory -i ../../inventories/iwad --graph |
| − | https://habr.com/ru/companies/ruvds/articles/962710/ | + | @all: |
| + | |--@ungrouped: | ||
| + | |--@proxmox: | ||
| + | | |--pve36.iwad.ru | ||
| + | | |--pve37.iwad.ru | ||
| + | | |--pve38.iwad.ru | ||
| + | | |--pve42.iwad.ru | ||
| + | |||
| + | или | ||
| + | ansible-inventory -i ../../inventories/iwad --list | ||
| + | |||
| + | кладём id_rsa поьзователю ansible(не умно, признаю) и пробуем катать базовую роль | ||
| + | |||
| + | РАБОТАЕМ!!! | ||
| + | |||
| + | |||
| + | https://netboxlabs.com/docs/integrations/tool-integrations/netbox-ansible-collection/#use-case-1---netbox-as-a-dynamic-inventory-source-for-ansible<br> | ||
| + | |||
| + | https://docs.ansible.com/projects/ansible/latest/collections/netbox/netbox/nb_inventory_inventory.html<br> | ||
| + | |||
| + | https://habr.com/ru/companies/ruvds/articles/962710/<br> | ||
| + | |||
https://demo.netbox.dev/api/schema/swagger-ui/#/ | https://demo.netbox.dev/api/schema/swagger-ui/#/ | ||
Текущая версия на 17:47, 23 марта 2026
Для начала подготовим виртуалочку для управления инфрой через ansible. Использовать будем Debian 12
Ставим python3.11 (т.к. он будет в зависимостях в нашем Pipfile) и pip
apt install python3.11-full apt install pip
Устанавливаем pipenv
pip install pipenv --break-system-packages
Подготавливаем ansible.cfg и Pipfile с зависимостями
ansible.cfg
Spoiler
Pipfile
Spoiler
Создадим директории и положим конфиги и прочее
/inventories /iwad /group_vars all.yaml netbox-inventory.yaml /playbooks /base ansible.cfg base.yaml Pipfile /roles /base
Клонируем репу с базовой ролью
Создаём виртуальное окружение(поставим пару пакетов которые пригодятся)
cd /root/ansible/playbooks/base pipenv install pipenv shell pip install dotenv loguru
Переходим к подключению динамического инвентаря. Приводим /inventories/iwad/netbox-inventory.yaml примерно к такому виду
--- plugin: netbox.netbox.nb_inventory api_endpoint: https://netbox.iwad.ru token: fc58b84c25grbc5506627a8c32r32a397213ee validate_certs: false config_context: false group_names_raw: True group_by: - device_roles compose: nbx_role: role.name primary_ip_address: primary_ip.address query_filters: - status: 'active' - cf_device_class: server - tenant: org_iwad device_query_filters: - has_primary_ip: 'true' vm_query_filters: - has_primary_ip: 'true'
Проверяем что инвентарь парсится(конечно же в netbox уже должны быть внесены устройства)
ansible-inventory -i ../../inventories/iwad --graph @all: |--@ungrouped: |--@proxmox: | |--pve36.iwad.ru | |--pve37.iwad.ru | |--pve38.iwad.ru | |--pve42.iwad.ru
или
ansible-inventory -i ../../inventories/iwad --list
кладём id_rsa поьзователю ansible(не умно, признаю) и пробуем катать базовую роль
РАБОТАЕМ!!!