{"id":360560,"date":"2024-05-21T00:55:45","date_gmt":"2024-05-21T00:55:45","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=360560"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-05-21T00:55:45","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=360560","title":{"rendered":"<span>Ansible + SoT \u0438\u043b\u0438 \u0415\u0449\u0451 \u0440\u0430\u0437 \u043f\u0440\u043e \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 inventory<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>Ansible \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435, \u0438 \u043c\u044b \u043d\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435. \u0412 \u043e\u0442\u0434\u0435\u043b\u0435 \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043c\u044b \u0441 \u0435\u0433\u043e \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0434\u043e\u0441\u0442\u0430\u0432\u043a\u0443 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0439 \u043d\u0430 \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u044b \u0438 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0442\u043e\u0440\u044b. \u041d\u0430\u0448\u0430 \u0441\u0435\u0442\u044c \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u0442\u0441\u044f, \u0432\u043d\u0435\u0434\u0440\u044f\u044e\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0435 \u0444\u0438\u0447\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0442\u044f\u043d\u0443\u0442 \u0437\u0430 \u0441\u043e\u0431\u043e\u0439 \u043d\u043e\u0432\u044b\u0435 \u043f\u043b\u0435\u0439\u0431\u0443\u043a\u0438 \u0438 \u0440\u043e\u043b\u0438, \u043d\u043e \u043e\u0434\u043d\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0441\u0442\u0430\u0432\u0430\u043b\u043e\u0441\u044c \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u044b\u043c \u2014 \u0441\u043f\u0438\u0441\u043e\u043a \u0445\u043e\u0441\u0442\u043e\u0432 \u043c\u044b \u0434\u0435\u0440\u0436\u0430\u043b\u0438 \u0432 ini-\u0444\u0430\u0439\u043b\u0435. \u0418 \u043a\u043e\u0433\u0434\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0445\u043e\u0441\u0442\u043e\u0432 \u0441\u0442\u0430\u043b\u043e \u0438\u0441\u0447\u0438\u0441\u043b\u044f\u0442\u044c\u0441\u044f \u0441\u043e\u0442\u043d\u044f\u043c\u0438, \u043f\u0440\u0438\u0448\u043b\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c inventory \u0432 \u0444\u0430\u0439\u043b\u0435 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c-\u0442\u043e \u0438 \u0443\u0434\u043e\u0431\u043d\u043e.  <\/p>\n<p>\u00ab<em>\u041f\u043e\u0441\u0442\u043e\u0439\u0442\u0435<\/em>\u00bb &#8212; \u043f\u043e\u0434\u0443\u043c\u0430\u043b\u0438 \u043c\u044b &#8212; \u00ab<em>\u0443 \u043d\u0430\u0441 \u0436\u0435 \u0435\u0441\u0442\u044c \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432 \u0432 Nautobot, \u0438 \u0432 \u043d\u0430\u0448\u0435\u0439 \u0441\u0435\u0442\u0438 \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u043c \u0438\u0441\u0442\u0438\u043d\u044b. \u041d\u0430\u0434\u043e \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043f\u043e\u0434\u0440\u0443\u0436\u0438\u0442\u044c Ansible \u0438 Nautobo<\/em>t\u00bb. \u0427\u0442\u043e \u0436, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0443\u043c\u0435\u044e\u0442 \u0434\u043e\u0433\u043e\u0432\u0430\u0440\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u044d\u0442\u0438 \u0434\u0432\u043e\u0435. <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/ec6\/fd9\/1e6\/ec6fd91e6e820151444ce4bc3cdc9b10.jpg\" width=\"768\" height=\"550\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ec6\/fd9\/1e6\/ec6fd91e6e820151444ce4bc3cdc9b10.jpg\" data-blurred=\"true\"\/><\/figure>\n<p>\u041d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0431\u044b\u043b \u0443\u0441\u0442\u0440\u043e\u0435\u043d \u043d\u0430\u0448 \u0444\u0430\u0439\u043b inventory. \u0412\u0441\u0435 \u0445\u043e\u0441\u0442\u044b \u0432 \u043d\u0451\u043c \u0440\u0430\u0437\u0431\u0438\u0442\u044b \u043d\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u0434\u0440\u0443\u0433 \u0432 \u0434\u0440\u0443\u0433\u0430 \u0433\u0440\u0443\u043f\u043f\u044b: \u043f\u043e \u0440\u043e\u043b\u0438 (\u0440\u043e\u0443\u0442\u0435\u0440, \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440), \u043f\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044e, \u043f\u043e \u0442\u0438\u043f\u0443 \u043b\u043e\u043a\u0430\u0446\u0438\u0438 (\u0446\u0435\u043d\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0444\u0438\u0441, \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u044b, \u0441\u043a\u043b\u0430\u0434\u044b), \u043f\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e (\u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0440\u043e\u0443\u0442\u0435\u0440, \u0440\u0435\u0437\u0435\u0440\u0432\u043d\u044b\u0439, \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439). \u0412\u044b\u0433\u043b\u044f\u0434\u0435\u043b inventory \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"yaml\"># \u0432\u0441\u0435 \u0440\u043e\u0443\u0442\u0435\u0440\u044b Cisco [all-cr:children] dc-cr hq-cr branch-cr-r0 branch-cr-r1 test-cr  # \u0440\u043e\u0443\u0442\u0435\u0440\u044b Cisco \u0432 \u0426\u041e\u0414 [dc-cr] DC_R0 DC_R1  # \u0440\u043e\u0443\u0442\u0435\u0440\u044b Cisco \u0432 \u0446\u0435\u043d\u0442\u0440\u0430\u043b\u044c\u043d\u043e\u043c \u043e\u0444\u0438\u0441\u0435 [hq-cr] HQ_R0 HQ_R1  # \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0440\u043e\u0443\u0442\u0435\u0440\u044b Cisco \u0432 \u0444\u0438\u043b\u0438\u0430\u043b\u0430\u0445 [branch-cr-r0:children] st-cr-r0 wh-cr-r0  # \u0440\u0435\u0437\u0435\u0440\u0432\u043d\u044b\u0435 \u0440\u043e\u0443\u0442\u0435\u0440\u044b Cisco \u0432 \u0444\u0438\u043b\u0438\u0430\u043b\u0430\u0445 [branch-cr-r1:children] st-cr-r1 wh-cr-r1  # \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0440\u043e\u0443\u0442\u0435\u0440\u044b Cisco \u0432 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430\u0445 [st-cr-r0] 111_R0 112_R0  # \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0440\u043e\u0443\u0442\u0435\u0440\u044b Cisco \u043d\u0430 \u0441\u043a\u043b\u0430\u0434\u0430\u0445 [wh-cr-r0] 1119_R0 1129_R0  # output omitted <\/code><\/pre>\n<p>\u041e\u0442\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u0445 \u0433\u0440\u0443\u043f\u043f \u043c\u044b \u043d\u0435 \u0441\u043e\u0431\u0438\u0440\u0430\u043b\u0438\u0441\u044c, \u0442\u0430\u043a \u0447\u0442\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u044f\u043b\u043e \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c, \u043a\u0430\u043a \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u0430\u0432 \u044d\u0442\u0438\u0445 \u0433\u0440\u0443\u043f\u043f \u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 Nautobot.\u00a0<\/p>\n<p>\u0414\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u0435\u0449\u0451 \u043d\u0435 \u0441\u043b\u044b\u0448\u0430\u043b \u043f\u0440\u043e Nautobot, \u0441\u043a\u0430\u0436\u0435\u043c \u043f\u0430\u0440\u0443 \u0441\u043b\u043e\u0432 \u043e \u043d\u0451\u043c \u0438 \u0435\u0433\u043e \u043c\u0435\u0441\u0442\u0435 \u0432 \u043d\u0430\u0448\u0435\u0439 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435:<\/p>\n<ul>\n<li>\n<p>\u044d\u0442\u043e \u0444\u043e\u0440\u043a Netbox,<\/p>\n<\/li>\n<li>\n<p>\u043c\u044b \u043f\u0440\u043e \u043d\u0435\u0433\u043e \u0443\u0436\u0435 <a href=\"https:\/\/habr.com\/ru\/companies\/hofftech\/articles\/715838\/\"><u>\u043f\u0438\u0441\u0430\u043b\u0438<\/u><\/a>,<\/p>\n<\/li>\n<li>\n<p>\u0432 \u043d\u0451\u043c \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u0432\u0441\u0435 \u043d\u0430\u0448\u0438 \u0441\u0435\u0442\u0435\u0432\u044b\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430, \u0441 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430\u043c\u0438 \u0438 \u0430\u0434\u0440\u0435\u0441\u0430\u043c\u0438, \u044d\u0442\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u0430;<\/p>\n<\/li>\n<li>\n<p>\u0442\u0430\u043a\u0436\u0435 \u043c\u044b \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0432 Nautobot \u0441\u0435\u0440\u0432\u0435\u0440\u044b \u0438 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u043c\u0430\u0448\u0438\u043d\u044b;<\/p>\n<\/li>\n<li>\n<p>\u0432\u0441\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u044b \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u044b \u043a \u043b\u043e\u043a\u0430\u0446\u0438\u044f\u043c;<\/p>\n<\/li>\n<li>\n<p>\u0435\u0441\u043b\u0438 \u0440\u0430\u0437\u0440\u0435\u0437\u043e\u0432 \u0443\u0447\u0451\u0442\u0430 \u00ab\u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438\u00bb \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442, \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c custom fields \u0438\u043b\u0438 \u043f\u0438\u0448\u0435\u043c \u043f\u043b\u0430\u0433\u0438\u043d\u044b;<\/p>\n<\/li>\n<li>\n<p>\u0443 Nautobot \u0435\u0441\u0442\u044c \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 API;<\/p>\n<\/li>\n<li>\n<p>\u0432 Nautobot \u0435\u0441\u0442\u044c \u0432\u0441\u044f \u043d\u0443\u0436\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0434\u043b\u044f inventory.<\/p>\n<\/li>\n<\/ul>\n<p>\u041a\u0430\u043a\u0438\u0435 \u0436\u0435 \u0435\u0441\u0442\u044c \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432\u043e\u0437\u043d\u0438\u043a\u0448\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438? \u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u2014 \u0441\u043a\u0440\u0438\u043f\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 Nautobot \u0438 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442 \u0444\u0430\u0439\u043b \u0441 \u0445\u043e\u0441\u0442\u0430\u043c\u0438 \u043f\u043e \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u043c\u0443 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0443.  <\/p>\n<h2>\u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u2116 1: \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e \u0440\u0430\u0441\u043f\u0438\u0441\u0430\u043d\u0438\u044e    <\/h2>\n<p>API \u0443 Nautobot \u0432\u043f\u043e\u043b\u043d\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u043b\u044e\u0431\u0438\u043c\u044b\u0439 \u044f\u0437\u044b\u043a \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u0432 cron. \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439: \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f \u043a API \u043f\u043e HTTP(s), \u0434\u0451\u0440\u0433\u0430\u0435\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432 \u043f\u043e \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c \u0438\u00a0 \u043f\u0438\u0448\u0435\u043c \u0432 \u0444\u0430\u0439\u043b. \u041d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0442 Nautobot \u0441\u0432\u0435\u0434\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0442 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0445\u043e\u0441\u0442\u044b \u043f\u043e \u043d\u0443\u0436\u043d\u044b\u043c \u0433\u0440\u0443\u043f\u043f\u0430\u043c. \u0414\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u0435\u0435\u043c\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0444\u0430\u0439\u043b \u0431\u0443\u0434\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 INI, \u0445\u043e\u0442\u044f, \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, YAML \u0431\u044b\u043b \u0431\u044b \u043f\u043e\u043f\u0440\u043e\u0449\u0435.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440\u043d\u044b\u0439 \u043a\u043e\u0434 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u043d\u0430 Python \u043c\u043e\u0433 \u0431\u044b \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<details class=\"spoiler\">\n<summary>nautobot2ansible_v1.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">import os, requests  url = os.getenv('NAUTOBOT_GRAPHQL_URL') headers = {     \"Content-Type\": \"application\/json\",     \"Accept-Encoding\": \"gzip\",     \"Authorization\": f\"Token {os.getenv('NAUTOBOT_TOKEN')}\", }  query = \"\"\" {   devices(     status: [\"active\"],     manufacturer: \"Cisco\",     has_primary_ip: true   ){     name     primary_ip4 { address }     device_role { slug }     site {         slug         cf_format     }     tags { slug }   } } \"\"\"  response = requests.post(url, headers=headers, json={\"query\": query}) if response.status_code == 200:     inventory = configparser.ConfigParser(allow_no_value=True)     for device in response.json()[\"data\"][\"devices\"]:         device_name = device[\"name\"]         device_ip = device[\"primary_ip4\"][\"address\"].split(\"\/\")[0]          # \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u0441\u0435\u043a\u0446\u0438\u044e \u0438\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043d\u0430\u0448\u0435\u0439 \u043b\u043e\u0433\u0438\u043a\u0438         # \u043f\u0435\u0440\u0432\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u0444\u0438\u043b\u0438\u0430\u043b\u0430, \u0434\u0430\u043d\u043d\u044b\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 custom field 'format'         section_part_1 = {             \"store\": \"st\",             \"warehouse\": \"wh\"         }.get(device[\"site\"][\"cf_format\"], \"other\")         # \u0432\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u043e\u043b\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430         section_part_2 = {             \"router\": \"cr\",             \"access_switch\": \"cs\",         }.get(device[\"device_role\"][\"slug\"], \"cd\")         # \u0442\u0440\u0435\u0442\u044c\u044f \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0442\u0435\u0433\u0430, \u043d\u0430\u0432\u0435\u0448\u0435\u043d\u043d\u043e\u0433\u043e \u043d\u0430 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e         tags = [tag[\"slug\"] for tag in device[\"tags\"]]         if \"primary_router\" in tags:             section_part_3 = \"r0\"         elif \"secondary_router\" in tags:             section_part_3 = \"r1\"         else:             section_part_3 = \"rx\"         if device[\"device_role\"][\"slug\"] == \"router\":             section = f\"{section_part_1}-{section_part_2}-{section_part_3}\"         else:             section = f\"{section_part_1}-{section_part_2}\"          if not inventory.has_section(section):             inventory.add_section(section)         inventory.set(section, f\"{device_name} ansible_host={device_ip}\")      with open(\"cisco_hosts\", 'w') as inventory_file:         inventory.write(inventory_file)<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0412 \u043a\u043e\u0434\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u043a \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u043e\u043c\u0443 \u0432\u0441\u0435\u043c \u043d\u0430\u043c REST API, \u0430 \u043a \u0431\u043e\u043b\u0435\u0435 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c\u0443 GraphQL. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043f\u0440\u043e \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 GraphQL \u0432 Nautobot \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"https:\/\/blog.networktocode.com\/post\/leveraging-the-power-of-graphql-with-nautobot\/\"><u>\u0442\u0443\u0442<\/u><\/a>. \u041f\u043e\u0447\u0435\u043c\u0443 GraphQL? \u041e\u043d \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u043f\u0440\u043e\u0449\u0435, \u0438 \u043e\u0442\u0434\u0430\u0451\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0443\u0436\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<p>\u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0442\u0430\u043a\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f: \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438, \u043f\u043e\u0436\u0430\u043b\u0443\u0439, \u043d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0451.<\/p>\n<p>\u041d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438:<\/p>\n<ul>\n<li>\n<p>\u0414\u0430\u043d\u043d\u044b\u0435 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441\u043a\u0440\u0438\u043f\u0442\u0430, \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u043f\u0435\u0440\u0438\u043e\u0434 \u043c\u0435\u0436\u0434\u0443 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0435\u0439 inventory \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u043e\u043c \u043f\u043b\u0435\u0439\u0431\u0443\u043a\u0430 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0435 \u0431\u0443\u0434\u0443\u0442.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0431\u043e\u0440 \u0433\u0440\u0443\u043f\u043f \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u043d \u0438 \u0437\u0430\u0445\u0430\u0440\u0434\u043a\u043e\u0436\u0435\u043d \u0432 \u0441\u043a\u0440\u0438\u043f\u0442\u0435.<\/p>\n<\/li>\n<\/ul>\n<p>\u0418\u0442\u0430\u043a, \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u043e\u0442 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u043e\u0432. \u041f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u043b\u043e\u043c \u043d\u0430\u0434\u043e \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c, \u043a\u0430\u043a \u0430\u043a\u0442\u0443\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0445\u043e\u0441\u0442\u043e\u0432 \u043d\u0430 \u043c\u043e\u043c\u0435\u043d\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043f\u043b\u0435\u0439\u0431\u0443\u043a\u043e\u0432.<\/p>\n<h2>\u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u2116 2: Dynamic inventory (\u0441\u0432\u043e\u0439)    <\/h2>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 ansible.cfg, \u0442\u043e \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u043d\u0430\u0439\u0434\u0451\u0442\u0435 \u0432 \u043d\u0451\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438:<\/p>\n<blockquote>\n<p>[inventory]<br \/># enable inventory plugins, default: &#8216;host_list&#8217;, &#8216;script&#8217;, &#8216;auto&#8217;, &#8216;yaml&#8217;, &#8216;ini&#8217;, &#8216;toml&#8217; <\/p>\n<\/blockquote>\n<p>\u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 script \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u00ab\u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438\u00bb Ansible \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 \u0445\u043e\u0441\u0442\u043e\u0432 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u0435 \u043d\u0430\u043c YAML \u0438\u043b\u0438 INI, \u0430 \u043b\u044e\u0431\u044b\u0435 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0435 \u0441\u043a\u0440\u0438\u043f\u0442\u044b. \u0414\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0441\u043a\u0440\u0438\u043f\u0442 \u0432 \u043d\u0443\u0436\u043d\u0443\u044e \u043f\u0430\u043f\u043a\u0443, \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0448\u0435\u0431\u0430\u043d\u0433 \u0438 \u0434\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0430 \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432 <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/dev_guide\/developing_inventory.html#inventory-script-conventions\"><u>\u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u0435<\/u><\/a>.<\/p>\n<p>\u041a\u043e\u0434 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0447\u0442\u0438 \u0442\u0430\u043a\u043e\u0439 \u0436\u0435, \u043a\u0430\u043a \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435. \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043e\u0442\u043b\u0438\u0447\u0438\u044f:\u00a0<\/p>\n<ul>\n<li>\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u0438\u043b\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0445\u043e\u0441\u0442\u043e\u0432 (&#8212;list), \u0438\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0445\u043e\u0441\u0442\u0430 (&#8212;host {hostname}).<\/p>\n<\/li>\n<li>\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442 \u0444\u0430\u0439\u043b, \u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 JSON.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u0434\u0430\u043d\u043d\u044b\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e\u0433\u043e \u0445\u043e\u0441\u0442\u0430, \u0442\u043e \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043a Nautobot \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u0435.<\/p>\n<\/li>\n<\/ul>\n<details class=\"spoiler\">\n<summary>nautobot2ansible_v2.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">#! \/usr\/bin\/python3  import requests import argparse import json import os, sys  parser = argparse.ArgumentParser() parser.add_argument(     \"--list\",     action='store_true' ) parser.add_argument(     \"--host\",     action='store', ) args = parser.parse_args()  inventory = {     \"_meta\": {         \"hostvars\": {}     } }  # \u0412\u044b\u0432\u043e\u0434\u0438\u043c \u043f\u0443\u0441\u0442\u044b\u0435 vars \u0438 \u0432\u044b\u0445\u043e\u0434\u0438\u043c if not args.list and not args.host:     print(json.dumps(inventory, indent=4))     sys.exit(0)   url = os.getenv('NAUTOBOT_GRAPHQL_URL') headers = {     \"Content-Type\": \"application\/json\",     \"Accept-Encoding\": \"gzip\",     \"Authorization\": f\"Token {os.getenv('NAUTOBOT_TOKEN')}\", }  if args.host:     limit_device = f'name: \"{args.host}\",' else:     limit_device = \"\" query = \"\"\" {   devices(%s status: [\"active\"], manufacturer:\"Cisco\", has_primary_ip:true) {     name     primary_ip4 { address }     device_role { slug }     site {         slug         cf_format     }     tags { slug }   } } \"\"\" % (limit_device)  response = requests.post(url, headers=headers, json={\"query\": query}, verify=False) if response.status_code == 200:     for device in response.json()[\"data\"][\"devices\"]:         device_name = device[\"name\"]         device_ip = device[\"primary_ip4\"][\"address\"].split(\"\/\")[0]         # \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u0441\u0435\u043a\u0446\u0438\u044e \u0438\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043d\u0430\u0448\u0435\u0439 \u043b\u043e\u0433\u0438\u043a\u0438         # \u043f\u0435\u0440\u0432\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u0444\u0438\u043b\u0438\u0430\u043b\u0430, \u0434\u0430\u043d\u043d\u044b\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 custom field 'format'         section_part_1 = {             \"store\": \"st\",             \"warehouse\": \"wh\"         }.get(device[\"site\"][\"cf_format\"], \"other\")         # \u0432\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u043e\u043b\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430         section_part_2 = {             \"router\": \"cr\",             \"access_switch\": \"cs\",         }.get(device[\"device_role\"][\"slug\"], \"cd\")         # \u0442\u0440\u0435\u0442\u044c\u044f \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0442\u0435\u0433\u0430, \u043d\u0430\u0432\u0435\u0448\u0435\u043d\u043d\u043e\u0433\u043e \u043d\u0430 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e         tags = [tag[\"slug\"] for tag in device[\"tags\"]]         if \"primary_router\" in tags:             section_part_3 = \"r0\"         elif \"secondary_router\" in tags:             section_part_3 = \"r1\"         else:             section_part_3 = \"rx\"          if device[\"device_role\"][\"slug\"] == \"router\":             section = f\"{section_part_1}-{section_part_2}-{section_part_3}\"         else:             section = f\"{section_part_1}-{section_part_2}\"         if section not in inventory:             inventory[section] = {                 \"hosts\": []             }          inventory[section][\"hosts\"].append(device_name)         inventory[\"_meta\"][\"hostvars\"][device_name] = {             \"ansible_host\": device_ip         }  # \u0412\u044b\u0432\u043e\u0434\u0438\u043c hosts \u0438 vars if args.list:     print(json.dumps(inventory, indent=4))  # \u0412\u044b\u0432\u043e\u0434\u0438\u043c vars \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u0445\u043e\u0441\u0442\u0430 elif args.host:     host_vars = {         \"_meta\": {             \"hostvars\": inventory[\"_meta\"][\"hostvars\"][args.host]         }     }     print(json.dumps(host_vars, indent=4))<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0422\u0430\u043a\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u0435 \u0433\u0440\u0443\u043f\u043f\u044b \u0438 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0445\u043e\u0441\u0442\u043e\u0432 \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u043b\u0435\u0439\u0431\u0443\u043a\u0430. \u042d\u0442\u043e \u0443\u0436\u0435 \u0441\u0430\u043c\u044b\u0439 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 inventory, \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0439 \u043d\u0430 \u043b\u0435\u0442\u0443. \u041d\u043e \u0432\u0441\u0451 \u0435\u0449\u0451 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0441 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u0433\u0440\u0443\u043f\u043f. \u0425\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0433\u0440\u0443\u043f\u043f\u044b \u0438 \u0440\u0435\u0433\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u043d\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0447\u0435\u0440\u0435\u0437 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 Nautobot. \u0422\u0430\u043a \u0447\u0442\u043e \u0434\u0432\u0438\u0433\u0430\u0435\u043c\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435 \u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0434\u0435\u0438.    <\/p>\n<h2>\u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u2116 3 &#8212; Dynamic inventory (\u0447\u0443\u0436\u043e\u0439)   <\/h2>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u0442\u0430\u043c \u0432 \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u0435? \u041c\u043e\u0436\u0435\u0442, \u0435\u0441\u0442\u044c \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0441 \u0443\u0441\u0442\u0440\u043e\u0438\u0442? \u0418 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 Nautobot \u043f\u043e\u0437\u0430\u0431\u043e\u0442\u0438\u043b\u0438\u0441\u044c \u043e \u043b\u0435\u043d\u0438\u0432\u044b\u0445 \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u0438\u043d\u0436\u0435\u043d\u0435\u0440\u0430\u0445 \u0438 \u0432 \u0441\u043e\u0441\u0442\u0430\u0432\u0435 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0434\u043b\u044f Ansible galaxy \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u043b\u0438 \u043f\u043b\u0430\u0433\u0438\u043d <a href=\"https:\/\/nautobot-ansible.readthedocs.io\/en\/latest\/plugins\/inventory_inventory.html\"><u>networktocode.nautobot.inventory<\/u><\/a>. \u041d\u0438\u043a\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0435 \u043d\u0430\u0434\u043e: \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0438 \u043f\u043b\u0430\u0433\u0438\u043d, \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u043b\u0438 \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u0444\u0430\u0439\u043b inventory, \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0445\u043e\u0441\u0442\u043e\u0432, \u0440\u0430\u0437\u0431\u0438\u0442\u044b\u0439 \u043f\u043e \u0433\u0440\u0443\u043f\u043f\u0430\u043c. \u041f\u0440\u0438\u0447\u0451\u043c \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u043e \u0448\u0438\u0440\u043e\u043a\u043e\u043c\u0443 \u043d\u0430\u0431\u043e\u0440\u0443 \u043f\u043e\u043b\u0435\u0439: \u043f\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044e, \u0440\u043e\u043b\u0438 \u043e\u0431\u043e\u0440\u0443\u0434\u043e\u0432\u0430\u043d\u0438\u044f, \u043b\u043e\u043a\u0430\u0446\u0438\u0438, \u0434\u0430\u0436\u0435 \u043f\u043e \u0442\u0435\u0433\u0430\u043c. \u0412\u0441\u0451 \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043d\u0430 \u043b\u0435\u0442\u0443: \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u043d\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u2014 \u0441\u0440\u0430\u0437\u0443 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u0445\u043e\u0441\u0442\u043e\u0432 \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0433\u0440\u0443\u043f\u043f\u0430 \u0441 \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c.<\/p>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0444\u0430\u0439\u043b inventory.yml:<\/p>\n<pre><code class=\"yaml\">plugin: networktocode.nautobot.inventory api_endpoint: https:\/\/nautobot validate_certs: True config_context: False group_by:   - device_roles   - tags   - manufacturers   - sites query_filters:   - status: active   - role: router   - role: access_switch device_query_filters:   - has_primary_ip: 'true'<\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c (\u0432\u044b\u0432\u043e\u0434 \u043d\u0435\u043f\u043e\u043b\u043d\u044b\u0439, \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u043e\u0432 \u0438 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0442\u043e\u0440\u043e\u0432 \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430):<\/p>\n<pre><code>ansible-inventory --graph -i inventory.yml @all:   |--@device_roles_access_switch:   |  |--111_SW1   |  |--111_SW2   |  |--112_SW1   |  |--112_SW2   |--@device_roles_router:   |  |--111_R0   |  |--111_R1   |  |--112_R0   |  |--112_R1   |--@manufacturers_Cisco:   |  |--111_R0   |  |--111_R1   |  |--112_R0   |  |--112_R1   |  |--111_SW1   |  |--111_SW2   |  |--112_SW1   |  |--112_SW2   |--@sites_111:   |  |--111_R0   |  |--111_R1   |  |--111_SW1   |  |--111_SW2   |--@sites_112:   |  |--112_R0   |  |--112_R1   |  |--112_SW1   |  |--112_SW2   |--@tags_primary_router:   |  |--111_R0   |  |--112_R0   |--@tags_secondary_router:   |  |--111_R1   |  |--112_R1<\/code><\/pre>\n<p>\u0414\u043e\u0441\u0442\u043e\u0438\u043d\u0441\u0442\u0432\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u044f:<\/p>\n<ul>\n<li>\n<p>\u041d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0435, \u0430 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u0435.<\/p>\n<\/li>\n<li>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0445\u043e\u0441\u0442\u0430 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 Nautobot (custom fields, local context \u0438 \u0442. \u0434.).<\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0437\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u043d\u044b\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0438 \u0445\u043e\u0441\u0442\u043e\u0432.<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0443 \u0438 \u043a\u0443\u0434\u0430 \u0436\u0435 \u0431\u0435\u0437 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u043e\u0432:<\/p>\n<ul>\n<li>\n<p>\u041d\u0435\u043b\u044c\u0437\u044f \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u0430\u043a \u0432\u0437\u044f\u0442\u044c \u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0433\u0440\u0443\u043f\u043f\u044b.<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u0446\u0435\u043b\u043e\u043c, \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u043d\u0435\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043d\u0430 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e\u0433\u043e, \u043d\u043e \u0441\u0432\u043e\u0435\u0433\u043e, \u0440\u0435\u0448\u0435\u043d\u0438\u044f. \u041d\u0430\u0434\u043e \u0431\u044b\u043b\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0432\u044b\u043a\u043d\u0443\u0442\u044c \u043a \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f\u043c \u043d\u043e\u0432\u044b\u0445 \u0433\u0440\u0443\u043f\u043f \u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044e \u0441\u0442\u0430\u0440\u044b\u0445.\u00a0\u041d\u043e \u043c\u044b \u043d\u0435 \u0441\u0442\u0430\u043b\u0438 \u043c\u0438\u0440\u0438\u0442\u044c\u0441\u044f \u0441 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0430\u043c\u0438 \u0438 \u0437\u0430\u0445\u043e\u0442\u0435\u043b\u0438 \u0435\u0449\u0451 \u0431\u043e\u043b\u044c\u0448\u0435\u0433\u043e \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0438 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438.<\/p>\n<h2>\u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u2116 4: Dynamic inventory (\u0441\u0432\u043e\u0439, \u0443\u0434\u043e\u0431\u043d\u044b\u0439, \u043a\u0440\u0430\u0441\u0438\u0432\u044b\u0439) <\/h2>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u043c\u044b \u043f\u0440\u0438\u0448\u043b\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438:\u00a0<\/p>\n<p><em>\u0412 Nautobot \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u0441\u043f\u0438\u0441\u043e\u043a \u0433\u0440\u0443\u043f\u043f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u0444\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u044b \u043f\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c \u0438 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c \u043c\u0430\u0448\u0438\u043d\u0430\u043c. \u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0442\u044c \u0441\u0440\u0430\u0437\u0443 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0445\u043e\u0441\u0442\u043e\u0432, \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0432 \u0433\u0440\u0443\u043f\u043f\u0443 \u043f\u043e \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c. \u0421\u043a\u0440\u0438\u043f\u0442 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e inventory \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043f\u043e API \u0441\u043f\u0438\u0441\u043e\u043a \u0433\u0440\u0443\u043f\u043f \u0441 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c\u0438, \u0438 \u0437\u0430\u0442\u0435\u043c \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0443\u0441\u043b\u043e\u0432\u0438\u044e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0445\u043e\u0441\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043d\u043e\u0441\u0438\u0442 \u0432 \u0441\u043e\u0441\u0442\u0430\u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u0433\u0440\u0443\u043f\u043f\u044b.<\/em>\u00a0<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u0435\u0442\u0435\u0432\u043e\u043c\u0443 \u0438\u043d\u0436\u0435\u043d\u0435\u0440\u0443 \u043d\u0443\u0436\u043d\u0430 \u0433\u0440\u0443\u043f\u043f\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0432\u0445\u043e\u0434\u044f\u0442 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0442\u043e\u0440\u044b, \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0449\u0438\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c:<\/p>\n<ul>\n<li>\n<p>\u043c\u043e\u0434\u0435\u043b\u044c: Cisco 4300 series;<\/p>\n<\/li>\n<li>\n<p>\u0442\u0435\u0433: primary_router;<\/p>\n<\/li>\n<li>\n<p>\u043b\u043e\u043a\u0430\u0446\u0438\u044f: \u0441\u043a\u043b\u0430\u0434\u044b;<\/p>\n<\/li>\n<li>\n<p>\u0441\u0442\u0430\u0442\u0443\u0441: active.<\/p>\n<\/li>\n<\/ul>\n<p>\u0418\u043d\u0436\u0435\u043d\u0435\u0440 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0432 Nautobot \u0433\u0440\u0443\u043f\u043f\u0443 \u0441 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c cr4300_wh_r0 \u0438 \u0432 \u0435\u0451 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0443\u0436\u043d\u044b\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u044b. \u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432, \u043f\u043e\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0445 \u0432 \u0433\u0440\u0443\u043f\u043f\u0443, \u0438 \u043f\u0440\u043e\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0444\u0438\u043b\u044c\u0442\u0440\u0430. \u042d\u0442\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u0430\u044f \u0433\u0440\u0443\u043f\u043f\u0430 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0432 Ansible inventory, \u043d\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0444\u0430\u0439\u043b\u044b inventory \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u0441 Ansible. \u0418, \u0440\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432, \u043d\u043e \u0438 \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432, \u0438 \u0434\u043b\u044f \u0412\u041c, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 Nautobot.<\/p>\n<p>\u0414\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0437\u0430\u0434\u0443\u043c\u0430\u043d\u043d\u043e\u0433\u043e \u0431\u044b\u043b\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u043f\u043b\u0430\u0433\u0438\u043d \u0434\u043b\u044f Nautobot \u0438 \u0441\u043a\u0440\u0438\u043f\u0442 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e inventory. \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0435 \u043f\u043b\u0430\u0433\u0438\u043d\u0430:<\/p>\n<ul>\n<li>\n<p>\u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0430\u043d\u043d\u044b\u0445;<\/p>\n<\/li>\n<li>\n<p>View, \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u0439 \u0437\u0430 \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0445\u043e\u0441\u0442\u043e\u0432 \u043f\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044e \u0433\u0440\u0443\u043f\u043f\u044b;<\/p>\n<\/li>\n<li>\n<p>\u0448\u0430\u0431\u043b\u043e\u043d HTML \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 \u0445\u043e\u0441\u0442\u043e\u0432 \u043f\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044e \u0433\u0440\u0443\u043f\u043f\u044b.<\/p>\n<\/li>\n<\/ul>\n<p>\u041c\u043e\u0434\u0435\u043b\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u0435\u0434\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430: \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0445\u0440\u0430\u043d\u0438\u043c \u0438\u043c\u044f \u0433\u0440\u0443\u043f\u043f\u044b, \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0444\u0438\u043b\u044c\u0442\u0440\u0430 \u0432 JSON \u0438 \u0441\u0442\u0430\u0442\u0443\u0441. \u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e GraphQL, \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0430.<\/p>\n<details class=\"spoiler\">\n<summary>models.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">from nautobot.core.models.generics import PrimaryModel from nautobot.extras.models import StatusModel from nautobot.extras.utils import extras_features  @extras_features(     \"statuses\",     \"graphql\", ) class AnsibleGroup(PrimaryModel, StatusModel):     name = models.CharField(max_length=20, unique=True, blank=False)     device_filters = models.JSONField(encoder=DjangoJSONEncoder)     description = models.CharField(max_length=150, blank=False, default=\"\")<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>View \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0443, \u043a\u043b\u0430\u0441\u0441 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439, \u043a\u0430\u043a\u00a0\u0434\u043b\u044f \u043b\u044e\u0431\u043e\u0433\u043e \u043f\u043b\u0430\u0433\u0438\u043d\u0430, \u0438 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u043b\u0438\u0447\u0438\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 get_extra_context, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0443 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u0438\u0437 View \u0432 \u0448\u0430\u0431\u043b\u043e\u043d. \u0418\u043c\u0435\u043d\u043d\u043e \u0432 \u043d\u0435\u0439 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043f\u0438\u0441\u043e\u043a \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432 \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u0444\u0438\u043b\u044c\u0442\u0440\u0430:<\/p>\n<details class=\"spoiler\">\n<summary>views.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">def get_extra_context(self, request, instance):     # \u0444\u0438\u043b\u044c\u0442\u0440\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435     DEVICE_PREFILTER_KEYS = {         \"status\": \"status__slug__in\",         \"role\": \"device_role__slug__in\",         \"name\": \"name__in\",         \"manufacturer\": \"platform__manufacturer__slug__in\",         \"site\": \"site__slug__in\",     }     VM_PREFILTER_KEYS = {         \"status\": \"status__slug__in\",         \"role\": \"role__slug__in\",         \"name\": \"name__in\",         \"site\": \"cluster__site__slug__in\",     }      # \u0444\u0438\u043b\u044c\u0442\u0440\u044b \u0434\u043b\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445     DEVICE_POSTFILTER_KEYS =  [\"model\", \"cf_format\", \"platform\", \"tag\", \"tag_any\"]     VM_POSTFILTER_KEYS = [\"cf_format\", \"platform\", \"tag\", \"tag_any\"]      context = super().get_extra_context(request, instance)     if self.action == \"retrieve\":         dev_filters = instance.device_filters         # \u0444\u0438\u043b\u044c\u0442\u0440 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e         device_kw_args = {             \"status__slug__in\": [\"active\"],         }         for key, filter_value in dev_filters.items():             if type(filter_value) is str:                 cur_filter = [filter_value]             else:                 cur_filter = filter_value             if key in DEVICE_PREFILTER_KEYS:                 device_kw_args.update({DEVICE_PREFILTER_KEYS[key]: cur_filter})          # \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432, \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u0435\u0444\u0438\u043b\u044c\u0442\u0440\u0430\u043c\u0438         devices_raw = (             Device.objects.restrict(request.user, \"view\")             .filter(**device_kw_args)         )         devices = []         for device in devices_raw:             device_tags = device.tags.slugs()             for key in DEVICE_POSTFILTER_KEYS:                 cur_filter = dev_filters.get(key)                 if cur_filter:                     if type(cur_filter) is str:                         cur_filter = [cur_filter]                     if key == \"model\" and not any(filter_value.lower() in device.device_type.slug.lower() for filter_value in cur_filter):                         # \u0438\u0449\u0435\u043c \u0432\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u0430 \"model\" \u0432 device_type.slug                         break                     elif key == \"cf_format\" and not any(filter_value.lower() == device.site.cf.get(\"Format\", \"Other\").lower() for filter_value in cur_filter):                         # \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u043d\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u0443 \u0441\u0430\u0439\u0442\u0430 (custom field \"format\")                         break                     elif key == \"platform\" and not any(filter_value.lower() == device.platform.napalm_driver.lower() for filter_value in cur_filter):                         # \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u043d\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 (\u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0443\u044e \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0443 \u0445\u0440\u0430\u043d\u0438\u043c \u0432 \u043f\u043e\u043b\u0435 napalm_driver)                         break                     elif key == \"tag\" and not all(filter_value in device_tags for filter_value in cur_filter):                         # \u0432\u0441\u0435 \u0442\u0435\u0433\u0438 \u0434\u043e\u043b\u0436\u043d\u044b \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0442\u044c \u0441 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u043c                         break                     elif key == \"tag_any\" and not any(filter_value in device_tags for filter_value in cur_filter):                         # \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u0438\u043d \u0442\u0435\u0433 \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0442\u044c \u0441 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u043c                         break             else:                 devices.append(device)          device_table = DeviceTable(devices)         # \u0441\u043f\u0440\u044f\u0447\u0435\u043c \u043b\u0438\u0448\u043d\u0438\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b         for column in (\"tenant\", \"rack\", \"location\"):             device_table.columns.hide(column)          paginate = {             \"paginator_class\": EnhancedPaginator,             \"per_page\": get_paginate_count(request),         }         RequestConfig(request, paginate).configure(device_table)         # \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u043c \u0432 \u0448\u0430\u0431\u043b\u043e\u043d \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443         context[\"device_table\"] = device_table  # \u0434\u0430\u043b\u044c\u0448\u0435 \u0438\u0434\u0451\u0442 \u043a\u043e\u0434 \u0434\u043b\u044f \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0438 \u0441\u043f\u0438\u0441\u043a\u0430 VirtualMachine, \u043e\u043d \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u0435\u043d \u043a\u043e\u0434\u0443 \u0434\u043b\u044f Device      return context<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043f\u043e\u043b\u044f \u0432 \u0444\u0438\u043b\u044c\u0442\u0440\u0435 \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430 \u0434\u0432\u0430 \u0442\u0438\u043f\u0430:<\/p>\n<ul>\n<li>\n<p>\u041f\u0435\u0440\u0432\u044b\u0435 \u0443\u0447\u0430\u0441\u0442\u0432\u0443\u044e\u0442 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043a \u0434\u0430\u043d\u043d\u044b\u043c \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0411\u0414.<\/p>\n<\/li>\n<li>\n<p>\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u0442\u043e\u0440\u044b\u0445 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0442\u0441\u044f \u0443\u0436\u0435 \u043d\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.\u00a0<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u043e\u043b\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a \u0442\u0430\u0431\u043b\u0438\u0446\u0435 Device. \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0432\u0435\u0440\u044f\u044e\u0442\u0441\u044f \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u043f\u043e\u043b\u0435\u0439 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0442\u0438\u043f\u0430. \u0418\u0442\u043e\u0433\u043e\u0432\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0445\u043e\u0441\u0442\u043e\u0432 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0432 HTML-\u0448\u0430\u0431\u043b\u043e\u043d\u0435. \u041d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 Ansible \u0432 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u043c inventory \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0436\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c: \u0441\u043a\u0440\u0438\u043f\u0442 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0433\u0440\u0443\u043f\u043f \u0432 Nautobot \u0438 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0437 \u043d\u0438\u0445 \u0434\u0435\u043b\u0430\u0435\u0442 GraphQL-\u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0439 \u0441\u043f\u0438\u0441\u043a\u0430 \u0445\u043e\u0441\u0442\u043e\u0432.<\/p>\n<p>\u0412\u043e\u0442 \u0442\u0430\u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432\u0438\u0434\u0438\u0442 \u0433\u0440\u0443\u043f\u043f\u0443 \u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0445\u043e\u0441\u0442\u043e\u0432:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b82\/a08\/0db\/b82a080dba088f0b09becaa4dde2bdc7.png\" width=\"1410\" height=\"736\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b82\/a08\/0db\/b82a080dba088f0b09becaa4dde2bdc7.png\"\/><\/figure>\n<p>\u0410 \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0439 \u0438\u043b\u0438 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u0433\u0440\u0443\u043f\u043f\u044b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0438\u0434\u0435\u043b\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u043b\u0430\u0433\u0438\u043d\u0430, \u043d\u0430 \u044d\u043a\u0440\u0430\u043d \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0441\u043f\u0440\u0430\u0432\u043a\u0430 \u043f\u043e \u0444\u0438\u043b\u044c\u0442\u0440\u0443: <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/128\/089\/ac4\/128089ac42652f40573c926c9744afcc.png\" width=\"1889\" height=\"770\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/128\/089\/ac4\/128089ac42652f40573c926c9744afcc.png\"\/><\/figure>\n<p>\u041f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a\u043e\u0434 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e inventory, \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0435\u0433\u043e \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 Ansible, \u0441\u043c\u044b\u0441\u043b\u0430 \u0443\u0436\u0435 \u043d\u0435 \u0432\u0438\u0436\u0443, \u0434\u043b\u044f \u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u043b\u0438 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0438\u0437 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0438 \u043b\u043e\u0433\u0438\u043a\u0443 \u0438\u0437 View \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043b\u0430\u0433\u0438\u043d\u0430. \u0422\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441\u0432\u044f\u0437\u043a\u0438 Nautobot + Ansible \u043e\u043a\u0430\u0437\u0430\u043b\u0441\u044f \u0441\u0430\u043c\u044b\u043c \u0443\u0434\u043e\u0431\u043d\u044b\u043c \u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438. \u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u044b\u0439, \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439, \u0438 \u0432 \u0442\u043e \u0436\u0435 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u044b\u0435 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0439 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u044b\u0445 \u0433\u0440\u0443\u043f\u043f.\u00a0<\/p>\n<p>\u041e \u0442\u043e\u043c, \u043a\u0430\u043a \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043b\u0430\u0433\u0438\u043d\u044b \u0434\u043b\u044f Nautobot \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043d\u0430 <a href=\"https:\/\/habr.com\/ru\/articles\/661281\/\"><u>\u0425\u0430\u0431\u0440\u0435<\/u><\/a> \u0438 \u0432 <a href=\"https:\/\/docs.nautobot.com\/projects\/core\/en\/stable\/development\/apps\/\"><u>\u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/u><\/a>.<\/p>\n<p>\u041d\u0443 \u0438, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435, \u043d\u0435 \u043c\u043e\u0433\u0443 \u043d\u0435 \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044c \u043c\u043e\u0435\u0433\u043e \u0434\u0440\u0443\u0433\u0430 \u0438 \u0431\u044b\u0432\u0448\u0435\u0433\u043e \u043a\u043e\u043b\u043b\u0435\u0433\u0443 <a href=\"https:\/\/habr.com\/ru\/users\/nikkor\/\"><u>\u041d\u0438\u043a\u043e\u043b\u0430\u044f \u041d\u0438\u043a\u0438\u0444\u043e\u0440\u043e\u0432\u0430<\/u><\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043d\u0435\u0441 \u043d\u0435\u043e\u0446\u0435\u043d\u0438\u043c\u044b\u0439 \u0432\u043a\u043b\u0430\u0434 \u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/774864\/\"> https:\/\/habr.com\/ru\/articles\/774864\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>Ansible \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435, \u0438 \u043c\u044b \u043d\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435. \u0412 \u043e\u0442\u0434\u0435\u043b\u0435 \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043c\u044b \u0441 \u0435\u0433\u043e \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0434\u043e\u0441\u0442\u0430\u0432\u043a\u0443 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0439 \u043d\u0430 \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440\u044b \u0438 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0442\u043e\u0440\u044b. \u041d\u0430\u0448\u0430 \u0441\u0435\u0442\u044c \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u0442\u0441\u044f, \u0432\u043d\u0435\u0434\u0440\u044f\u044e\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0435 \u0444\u0438\u0447\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0442\u044f\u043d\u0443\u0442 \u0437\u0430 \u0441\u043e\u0431\u043e\u0439 \u043d\u043e\u0432\u044b\u0435 \u043f\u043b\u0435\u0439\u0431\u0443\u043a\u0438 \u0438 \u0440\u043e\u043b\u0438, \u043d\u043e \u043e\u0434\u043d\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0441\u0442\u0430\u0432\u0430\u043b\u043e\u0441\u044c \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u044b\u043c \u2014 \u0441\u043f\u0438\u0441\u043e\u043a \u0445\u043e\u0441\u0442\u043e\u0432 \u043c\u044b \u0434\u0435\u0440\u0436\u0430\u043b\u0438 \u0432 ini-\u0444\u0430\u0439\u043b\u0435. \u0418 \u043a\u043e\u0433\u0434\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0445\u043e\u0441\u0442\u043e\u0432 \u0441\u0442\u0430\u043b\u043e \u0438\u0441\u0447\u0438\u0441\u043b\u044f\u0442\u044c\u0441\u044f \u0441\u043e\u0442\u043d\u044f\u043c\u0438, \u043f\u0440\u0438\u0448\u043b\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c inventory \u0432 \u0444\u0430\u0439\u043b\u0435 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c-\u0442\u043e \u0438 \u0443\u0434\u043e\u0431\u043d\u043e.  <\/p>\n<p>\u00ab<em>\u041f\u043e\u0441\u0442\u043e\u0439\u0442\u0435<\/em>\u00bb &#8212; \u043f\u043e\u0434\u0443\u043c\u0430\u043b\u0438 \u043c\u044b &#8212; \u00ab<em>\u0443 \u043d\u0430\u0441 \u0436\u0435 \u0435\u0441\u0442\u044c \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432 \u0432 Nautobot, \u0438 \u0432 \u043d\u0430\u0448\u0435\u0439 \u0441\u0435\u0442\u0438 \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u043c \u0438\u0441\u0442\u0438\u043d\u044b. \u041d\u0430\u0434\u043e \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043f\u043e\u0434\u0440\u0443\u0436\u0438\u0442\u044c Ansible \u0438 Nautobo<\/em>t\u00bb. \u0427\u0442\u043e \u0436, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0443\u043c\u0435\u044e\u0442 \u0434\u043e\u0433\u043e\u0432\u0430\u0440\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u044d\u0442\u0438 \u0434\u0432\u043e\u0435. <\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0431\u044b\u043b \u0443\u0441\u0442\u0440\u043e\u0435\u043d \u043d\u0430\u0448 \u0444\u0430\u0439\u043b inventory. \u0412\u0441\u0435 \u0445\u043e\u0441\u0442\u044b \u0432 \u043d\u0451\u043c \u0440\u0430\u0437\u0431\u0438\u0442\u044b \u043d\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u0434\u0440\u0443\u0433 \u0432 \u0434\u0440\u0443\u0433\u0430 \u0433\u0440\u0443\u043f\u043f\u044b: \u043f\u043e \u0440\u043e\u043b\u0438 (\u0440\u043e\u0443\u0442\u0435\u0440, \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u043e\u0440), \u043f\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044e, \u043f\u043e \u0442\u0438\u043f\u0443 \u043b\u043e\u043a\u0430\u0446\u0438\u0438 (\u0446\u0435\u043d\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0444\u0438\u0441, \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u044b, \u0441\u043a\u043b\u0430\u0434\u044b), \u043f\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e (\u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0440\u043e\u0443\u0442\u0435\u0440, \u0440\u0435\u0437\u0435\u0440\u0432\u043d\u044b\u0439, \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439). \u0412\u044b\u0433\u043b\u044f\u0434\u0435\u043b inventory \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"yaml\"># \u0432\u0441\u0435 \u0440\u043e\u0443\u0442\u0435\u0440\u044b Cisco [all-cr:children] dc-cr hq-cr branch-cr-r0 branch-cr-r1 test-cr  # \u0440\u043e\u0443\u0442\u0435\u0440\u044b Cisco \u0432 \u0426\u041e\u0414 [dc-cr] DC_R0 DC_R1  # \u0440\u043e\u0443\u0442\u0435\u0440\u044b Cisco \u0432 \u0446\u0435\u043d\u0442\u0440\u0430\u043b\u044c\u043d\u043e\u043c \u043e\u0444\u0438\u0441\u0435 [hq-cr] HQ_R0 HQ_R1  # \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0440\u043e\u0443\u0442\u0435\u0440\u044b Cisco \u0432 \u0444\u0438\u043b\u0438\u0430\u043b\u0430\u0445 [branch-cr-r0:children] st-cr-r0 wh-cr-r0  # \u0440\u0435\u0437\u0435\u0440\u0432\u043d\u044b\u0435 \u0440\u043e\u0443\u0442\u0435\u0440\u044b Cisco \u0432 \u0444\u0438\u043b\u0438\u0430\u043b\u0430\u0445 [branch-cr-r1:children] st-cr-r1 wh-cr-r1  # \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0440\u043e\u0443\u0442\u0435\u0440\u044b Cisco \u0432 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0430\u0445 [st-cr-r0] 111_R0 112_R0  # \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0440\u043e\u0443\u0442\u0435\u0440\u044b Cisco \u043d\u0430 \u0441\u043a\u043b\u0430\u0434\u0430\u0445 [wh-cr-r0] 1119_R0 1129_R0  # output omitted <\/code><\/pre>\n<p>\u041e\u0442\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u0445 \u0433\u0440\u0443\u043f\u043f \u043c\u044b \u043d\u0435 \u0441\u043e\u0431\u0438\u0440\u0430\u043b\u0438\u0441\u044c, \u0442\u0430\u043a \u0447\u0442\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u044f\u043b\u043e \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c, \u043a\u0430\u043a \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u0430\u0432 \u044d\u0442\u0438\u0445 \u0433\u0440\u0443\u043f\u043f \u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 Nautobot.\u00a0<\/p>\n<p>\u0414\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u0435\u0449\u0451 \u043d\u0435 \u0441\u043b\u044b\u0448\u0430\u043b \u043f\u0440\u043e Nautobot, \u0441\u043a\u0430\u0436\u0435\u043c \u043f\u0430\u0440\u0443 \u0441\u043b\u043e\u0432 \u043e \u043d\u0451\u043c \u0438 \u0435\u0433\u043e \u043c\u0435\u0441\u0442\u0435 \u0432 \u043d\u0430\u0448\u0435\u0439 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435:<\/p>\n<ul>\n<li>\n<p>\u044d\u0442\u043e \u0444\u043e\u0440\u043a Netbox,<\/p>\n<\/li>\n<li>\n<p>\u043c\u044b \u043f\u0440\u043e \u043d\u0435\u0433\u043e \u0443\u0436\u0435 <a href=\"https:\/\/habr.com\/ru\/companies\/hofftech\/articles\/715838\/\"><u>\u043f\u0438\u0441\u0430\u043b\u0438<\/u><\/a>,<\/p>\n<\/li>\n<li>\n<p>\u0432 \u043d\u0451\u043c \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u0432\u0441\u0435 \u043d\u0430\u0448\u0438 \u0441\u0435\u0442\u0435\u0432\u044b\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430, \u0441 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430\u043c\u0438 \u0438 \u0430\u0434\u0440\u0435\u0441\u0430\u043c\u0438, \u044d\u0442\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u0430;<\/p>\n<\/li>\n<li>\n<p>\u0442\u0430\u043a\u0436\u0435 \u043c\u044b \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0432 Nautobot \u0441\u0435\u0440\u0432\u0435\u0440\u044b \u0438 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u043c\u0430\u0448\u0438\u043d\u044b;<\/p>\n<\/li>\n<li>\n<p>\u0432\u0441\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u044b \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u044b \u043a \u043b\u043e\u043a\u0430\u0446\u0438\u044f\u043c;<\/p>\n<\/li>\n<li>\n<p>\u0435\u0441\u043b\u0438 \u0440\u0430\u0437\u0440\u0435\u0437\u043e\u0432 \u0443\u0447\u0451\u0442\u0430 \u00ab\u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438\u00bb \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442, \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c custom fields \u0438\u043b\u0438 \u043f\u0438\u0448\u0435\u043c \u043f\u043b\u0430\u0433\u0438\u043d\u044b;<\/p>\n<\/li>\n<li>\n<p>\u0443 Nautobot \u0435\u0441\u0442\u044c \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 API;<\/p>\n<\/li>\n<li>\n<p>\u0432 Nautobot \u0435\u0441\u0442\u044c \u0432\u0441\u044f \u043d\u0443\u0436\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0434\u043b\u044f inventory.<\/p>\n<\/li>\n<\/ul>\n<p>\u041a\u0430\u043a\u0438\u0435 \u0436\u0435 \u0435\u0441\u0442\u044c \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432\u043e\u0437\u043d\u0438\u043a\u0448\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438? \u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u2014 \u0441\u043a\u0440\u0438\u043f\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 Nautobot \u0438 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442 \u0444\u0430\u0439\u043b \u0441 \u0445\u043e\u0441\u0442\u0430\u043c\u0438 \u043f\u043e \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u043c\u0443 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0443.  <\/p>\n<h2>\u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u2116 1: \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e \u0440\u0430\u0441\u043f\u0438\u0441\u0430\u043d\u0438\u044e    <\/h2>\n<p>API \u0443 Nautobot \u0432\u043f\u043e\u043b\u043d\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u043b\u044e\u0431\u0438\u043c\u044b\u0439 \u044f\u0437\u044b\u043a \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u0432 cron. \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439: \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f \u043a API \u043f\u043e HTTP(s), \u0434\u0451\u0440\u0433\u0430\u0435\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432 \u043f\u043e \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c \u0438\u00a0 \u043f\u0438\u0448\u0435\u043c \u0432 \u0444\u0430\u0439\u043b. \u041d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0442 Nautobot \u0441\u0432\u0435\u0434\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0442 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0445\u043e\u0441\u0442\u044b \u043f\u043e \u043d\u0443\u0436\u043d\u044b\u043c \u0433\u0440\u0443\u043f\u043f\u0430\u043c. \u0414\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u0435\u0435\u043c\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0444\u0430\u0439\u043b \u0431\u0443\u0434\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 INI, \u0445\u043e\u0442\u044f, \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, YAML \u0431\u044b\u043b \u0431\u044b \u043f\u043e\u043f\u0440\u043e\u0449\u0435.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440\u043d\u044b\u0439 \u043a\u043e\u0434 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u043d\u0430 Python \u043c\u043e\u0433 \u0431\u044b \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<details class=\"spoiler\">\n<summary>nautobot2ansible_v1.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">import os, requests  url = os.getenv('NAUTOBOT_GRAPHQL_URL') headers = {     \"Content-Type\": \"application\/json\",     \"Accept-Encoding\": \"gzip\",     \"Authorization\": f\"Token {os.getenv('NAUTOBOT_TOKEN')}\", }  query = \"\"\" {   devices(     status: [\"active\"],     manufacturer: \"Cisco\",     has_primary_ip: true   ){     name     primary_ip4 { address }     device_role { slug }     site {         slug         cf_format     }     tags { slug }   } } \"\"\"  response = requests.post(url, headers=headers, json={\"query\": query}) if response.status_code == 200:     inventory = configparser.ConfigParser(allow_no_value=True)     for device in response.json()[\"data\"][\"devices\"]:         device_name = device[\"name\"]         device_ip = device[\"primary_ip4\"][\"address\"].split(\"\/\")[0]          # \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u0441\u0435\u043a\u0446\u0438\u044e \u0438\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043d\u0430\u0448\u0435\u0439 \u043b\u043e\u0433\u0438\u043a\u0438         # \u043f\u0435\u0440\u0432\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u0444\u0438\u043b\u0438\u0430\u043b\u0430, \u0434\u0430\u043d\u043d\u044b\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 custom field 'format'         section_part_1 = {             \"store\": \"st\",             \"warehouse\": \"wh\"         }.get(device[\"site\"][\"cf_format\"], \"other\")         # \u0432\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u043e\u043b\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430         section_part_2 = {             \"router\": \"cr\",             \"access_switch\": \"cs\",         }.get(device[\"device_role\"][\"slug\"], \"cd\")         # \u0442\u0440\u0435\u0442\u044c\u044f \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0442\u0435\u0433\u0430, \u043d\u0430\u0432\u0435\u0448\u0435\u043d\u043d\u043e\u0433\u043e \u043d\u0430 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e         tags = [tag[\"slug\"] for tag in device[\"tags\"]]         if \"primary_router\" in tags:             section_part_3 = \"r0\"         elif \"secondary_router\" in tags:             section_part_3 = \"r1\"         else:             section_part_3 = \"rx\"         if device[\"device_role\"][\"slug\"] == \"router\":             section = f\"{section_part_1}-{section_part_2}-{section_part_3}\"         else:             section = f\"{section_part_1}-{section_part_2}\"          if not inventory.has_section(section):             inventory.add_section(section)         inventory.set(section, f\"{device_name} ansible_host={device_ip}\")      with open(\"cisco_hosts\", 'w') as inventory_file:         inventory.write(inventory_file)<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0412 \u043a\u043e\u0434\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u043a \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u043e\u043c\u0443 \u0432\u0441\u0435\u043c \u043d\u0430\u043c REST API, \u0430 \u043a \u0431\u043e\u043b\u0435\u0435 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c\u0443 GraphQL. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043f\u0440\u043e \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 GraphQL \u0432 Nautobot \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"https:\/\/blog.networktocode.com\/post\/leveraging-the-power-of-graphql-with-nautobot\/\"><u>\u0442\u0443\u0442<\/u><\/a>. \u041f\u043e\u0447\u0435\u043c\u0443 GraphQL? \u041e\u043d \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u043f\u0440\u043e\u0449\u0435, \u0438 \u043e\u0442\u0434\u0430\u0451\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0443\u0436\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<p>\u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0442\u0430\u043a\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f: \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438, \u043f\u043e\u0436\u0430\u043b\u0443\u0439, \u043d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0451.<\/p>\n<p>\u041d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438:<\/p>\n<ul>\n<li>\n<p>\u0414\u0430\u043d\u043d\u044b\u0435 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441\u043a\u0440\u0438\u043f\u0442\u0430, \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u043f\u0435\u0440\u0438\u043e\u0434 \u043c\u0435\u0436\u0434\u0443 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0435\u0439 inventory \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u043e\u043c \u043f\u043b\u0435\u0439\u0431\u0443\u043a\u0430 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0435 \u0431\u0443\u0434\u0443\u0442.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0431\u043e\u0440 \u0433\u0440\u0443\u043f\u043f \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u043d \u0438 \u0437\u0430\u0445\u0430\u0440\u0434\u043a\u043e\u0436\u0435\u043d \u0432 \u0441\u043a\u0440\u0438\u043f\u0442\u0435.<\/p>\n<\/li>\n<\/ul>\n<p>\u0418\u0442\u0430\u043a, \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u043e\u0442 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u043e\u0432. \u041f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u043b\u043e\u043c \u043d\u0430\u0434\u043e \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c, \u043a\u0430\u043a \u0430\u043a\u0442\u0443\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0445\u043e\u0441\u0442\u043e\u0432 \u043d\u0430 \u043c\u043e\u043c\u0435\u043d\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043f\u043b\u0435\u0439\u0431\u0443\u043a\u043e\u0432.<\/p>\n<h2>\u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u2116 2: Dynamic inventory (\u0441\u0432\u043e\u0439)    <\/h2>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 ansible.cfg, \u0442\u043e \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u043d\u0430\u0439\u0434\u0451\u0442\u0435 \u0432 \u043d\u0451\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438:<\/p>\n<blockquote>\n<p>[inventory]<br \/># enable inventory plugins, default: &#8216;host_list&#8217;, &#8216;script&#8217;, &#8216;auto&#8217;, &#8216;yaml&#8217;, &#8216;ini&#8217;, &#8216;toml&#8217; <\/p>\n<\/blockquote>\n<p>\u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 script \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u00ab\u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438\u00bb Ansible \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 \u0445\u043e\u0441\u0442\u043e\u0432 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u0435 \u043d\u0430\u043c YAML \u0438\u043b\u0438 INI, \u0430 \u043b\u044e\u0431\u044b\u0435 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0435 \u0441\u043a\u0440\u0438\u043f\u0442\u044b. \u0414\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0441\u043a\u0440\u0438\u043f\u0442 \u0432 \u043d\u0443\u0436\u043d\u0443\u044e \u043f\u0430\u043f\u043a\u0443, \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0448\u0435\u0431\u0430\u043d\u0433 \u0438 \u0434\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0430 \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432 <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/dev_guide\/developing_inventory.html#inventory-script-conventions\"><u>\u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u0435<\/u><\/a>.<\/p>\n<p>\u041a\u043e\u0434 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0447\u0442\u0438 \u0442\u0430\u043a\u043e\u0439 \u0436\u0435, \u043a\u0430\u043a \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435. \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043e\u0442\u043b\u0438\u0447\u0438\u044f:\u00a0<\/p>\n<ul>\n<li>\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u0438\u043b\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0445\u043e\u0441\u0442\u043e\u0432 (&#8212;list), \u0438\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0445\u043e\u0441\u0442\u0430 (&#8212;host {hostname}).<\/p>\n<\/li>\n<li>\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442 \u0444\u0430\u0439\u043b, \u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 JSON.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u0434\u0430\u043d\u043d\u044b\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e\u0433\u043e \u0445\u043e\u0441\u0442\u0430, \u0442\u043e \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043a Nautobot \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u0435.<\/p>\n<\/li>\n<\/ul>\n<details class=\"spoiler\">\n<summary>nautobot2ansible_v2.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">#! \/usr\/bin\/python3  import requests import argparse import json import os, sys  parser = argparse.ArgumentParser() parser.add_argument(     \"--list\",     action='store_true' ) parser.add_argument(     \"--host\",     action='store', ) args = parser.parse_args()  inventory = {     \"_meta\": {         \"hostvars\": {}     } }  # \u0412\u044b\u0432\u043e\u0434\u0438\u043c \u043f\u0443\u0441\u0442\u044b\u0435 vars \u0438 \u0432\u044b\u0445\u043e\u0434\u0438\u043c if not args.list and not args.host:     print(json.dumps(inventory, indent=4))     sys.exit(0)   url = os.getenv('NAUTOBOT_GRAPHQL_URL') headers = {     \"Content-Type\": \"application\/json\",     \"Accept-Encoding\": \"gzip\",     \"Authorization\": f\"Token {os.getenv('NAUTOBOT_TOKEN')}\", }  if args.host:     limit_device = f'name: \"{args.host}\",' else:     limit_device = \"\" query = \"\"\" {   devices(%s status: [\"active\"], manufacturer:\"Cisco\", has_primary_ip:true) {     name     primary_ip4 { address }     device_role { slug }     site {         slug         cf_format     }     tags { slug }   } } \"\"\" % (limit_device)  response = requests.post(url, headers=headers, json={\"query\": query}, verify=False) if response.status_code == 200:     for device in response.json()[\"data\"][\"devices\"]:         device_name = device[\"name\"]         device_ip = device[\"primary_ip4\"][\"address\"].split(\"\/\")[0]         # \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u0441\u0435\u043a\u0446\u0438\u044e \u0438\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043d\u0430\u0448\u0435\u0439 \u043b\u043e\u0433\u0438\u043a\u0438         # \u043f\u0435\u0440\u0432\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u0444\u0438\u043b\u0438\u0430\u043b\u0430, \u0434\u0430\u043d\u043d\u044b\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 custom field 'format'         section_part_1 = {             \"store\": \"st\",             \"warehouse\": \"wh\"         }.get(device[\"site\"][\"cf_format\"], \"other\")         # \u0432\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u043e\u043b\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430         section_part_2 = {             \"router\": \"cr\",             \"access_switch\": \"cs\",         }.get(device[\"device_role\"][\"slug\"], \"cd\")         # \u0442\u0440\u0435\u0442\u044c\u044f \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0442\u0435\u0433\u0430, \u043d\u0430\u0432\u0435\u0448\u0435\u043d\u043d\u043e\u0433\u043e \u043d\u0430 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e         tags = [tag[\"slug\"] for tag in device[\"tags\"]]         if \"primary_router\" in tags:             section_part_3 = \"r0\"         elif \"secondary_router\" in tags:             section_part_3 = \"r1\"         else:             section_part_3 = \"rx\"          if device[\"device_role\"][\"slug\"] == \"router\":             section = f\"{section_part_1}-{section_part_2}-{section_part_3}\"         else:             section = f\"{section_part_1}-{section_part_2}\"         if section not in inventory:             inventory[section] = {                 \"hosts\": []             }          inventory[section][\"hosts\"].append(device_name)         inventory[\"_meta\"][\"hostvars\"][device_name] = {             \"ansible_host\": device_ip         }  # \u0412\u044b\u0432\u043e\u0434\u0438\u043c hosts \u0438 vars if args.list:     print(json.dumps(inventory, indent=4))  # \u0412\u044b\u0432\u043e\u0434\u0438\u043c vars \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u0445\u043e\u0441\u0442\u0430 elif args.host:     host_vars = {         \"_meta\": {             \"hostvars\": inventory[\"_meta\"][\"hostvars\"][args.host]         }     }     print(json.dumps(host_vars, indent=4))<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0422\u0430\u043a\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u0435 \u0433\u0440\u0443\u043f\u043f\u044b \u0438 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0445\u043e\u0441\u0442\u043e\u0432 \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u043b\u0435\u0439\u0431\u0443\u043a\u0430. \u042d\u0442\u043e \u0443\u0436\u0435 \u0441\u0430\u043c\u044b\u0439 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 inventory, \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0439 \u043d\u0430 \u043b\u0435\u0442\u0443. \u041d\u043e \u0432\u0441\u0451 \u0435\u0449\u0451 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0441 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u0433\u0440\u0443\u043f\u043f. \u0425\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0433\u0440\u0443\u043f\u043f\u044b \u0438 \u0440\u0435\u0433\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u043d\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0447\u0435\u0440\u0435\u0437 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 Nautobot. \u0422\u0430\u043a \u0447\u0442\u043e \u0434\u0432\u0438\u0433\u0430\u0435\u043c\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435 \u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0434\u0435\u0438.    <\/p>\n<h2>\u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u2116 3 &#8212; Dynamic inventory (\u0447\u0443\u0436\u043e\u0439)   <\/h2>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u0442\u0430\u043c \u0432 \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u0435? \u041c\u043e\u0436\u0435\u0442, \u0435\u0441\u0442\u044c \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0441 \u0443\u0441\u0442\u0440\u043e\u0438\u0442? \u0418 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 Nautobot<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-360560","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/360560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=360560"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/360560\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=360560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=360560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=360560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}