{"id":42221891,"url":"https://github.com/device42/ansible_device42","last_synced_at":"2026-01-27T02:11:32.753Z","repository":{"id":54543683,"uuid":"77928976","full_name":"device42/ansible_device42","owner":"device42","description":"Scripts to generate static inventory file or generate dynamic inventory for ansible from Device42 data","archived":false,"fork":false,"pushed_at":"2023-09-15T18:59:31.000Z","size":87,"stargazers_count":40,"open_issues_count":1,"forks_count":20,"subscribers_count":11,"default_branch":"master","last_synced_at":"2024-03-26T07:56:46.745Z","etag":null,"topics":["ansible","devops","inventory"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/device42.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-03T15:27:13.000Z","updated_at":"2023-12-06T07:03:09.000Z","dependencies_parsed_at":"2022-08-13T19:10:47.009Z","dependency_job_id":null,"html_url":"https://github.com/device42/ansible_device42","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/device42/ansible_device42","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/device42%2Fansible_device42","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/device42%2Fansible_device42/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/device42%2Fansible_device42/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/device42%2Fansible_device42/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/device42","download_url":"https://codeload.github.com/device42/ansible_device42/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/device42%2Fansible_device42/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28796962,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-27T01:07:07.743Z","status":"online","status_checked_at":"2026-01-27T02:00:07.755Z","response_time":168,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ansible","devops","inventory"],"created_at":"2026-01-27T02:11:32.598Z","updated_at":"2026-01-27T02:11:32.747Z","avatar_url":"https://github.com/device42.png","language":"Python","readme":"[Device42](http://www.device42.com/) is a Continuous Discovery software for your IT Infrastructure. It helps you automatically maintain an up-to-date inventory of your physical, virtual, and cloud servers and containers, network components, software/services/applications, and their inter-relationships and inter-dependencies.\n\nThis Ansible Collection contains the inventory and lookup plugins for Device42. \n1. Inventory (device42.d42.d42) - can be used to dynamically generate Ansible inventory from Device42 devices.\n2. Lookup (device42.d42.d42)- This lookup plugin allows a playbook to fetch passwords or ip addresses for hosts from Device42.\n3. Lookup (device42.d42.d42_prompt) - This lookup plugin allows a playbook to fetch passwords or ip addresses for hosts from Device42 with prompt. \n\nIn the `contrib` directory you will find the legacy inventory scripts. Please favor the plugin over the legacy scripts:\n1. `contrib\\inventory\\d42_ansible_inventory_hostfile.py` can be used to create a static inventory file for ansible. You can group hosts by tags, customers, building or service level from Device42 data.\n2. `contrib\\inventory\\d42_ansible_dynamic_inventory.py` can be used to dynamically by ansible to get hosts from Device42 based on certain filters.\n\n-----------------------------\n### Requirements\n- ansible 2.9+\n- python 3.6.x+\n- Device42 16.12.00+\n- requests (you can install it with pip install requests or apt-get install python-requests)\n- Ansible must have an available connection to your Device42 instance in order to collect devices for inventory\n\n### Installation Methods\n\n#### Galaxy \n```bash\nansible-galaxy collection install device42.d42\n```\n\n#### Automation Hub\nTo consume content from hub as part of your automation workflows the content can also be accessed via CLI. \nFor this an offline token is required which can be obtained via the web UI at [automation hub](https://cloud.redhat.com/ansible/automation-hub/token), \nand needs to be added to the configuration file as follows:\n\n```bash\n[galaxy]\nserver_list = automation_hub, galaxy\n\n[galaxy_server.automation_hub]\nurl=https://cloud.redhat.com/api/automation-hub/\nauth_url=https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token\ntoken=AABBccddeeff112233gghh...\n\n[galaxy_server.galaxy]\nurl=https://galaxy.ansible.com/\n```\n\nOnce configured the collection can be installed by running the following command\n```bash\n$ ansible-galaxy collection install device42.d42\n```\n\n#### Ansible Tower:\n* Create a collections requirement file in the SCM \n```bash\ntouch requirements.yml\n```\n\n* add the following to your file and save it\n```bash\ncollections:\n- name: device42.d42\nversion: 1.1.2\nsource: https://galaxy.ansible.com/\n\n* Install the Ansible collection by running command\n```bash\nansible-galaxy collection install -r requirements.yml -p \u003cjob tmp location\u003e\n```\nStarting with Ansible Tower 3.6, the project folder will be copied for each job run. This allows playbooks to make \nlocal changes to the source tree for convenience, such as creating temporary files, \nwithout the possibility of interference with other jobs.\n\n* Set the following environmental variables (optional)\n```\nD42_USER = 'device42 user'\nD42_PWD = 'device42 password'\nD42_URL = 'https:// device42 server IP address'\nD42_SKIP_SSL_CHECK = False\nD42_CLEAN_DEVICE_NAME = True\n```\n\nFor more information on installing collections please follow documentation here https://docs.ansible.com/ansible/latest/user_guide/collections_using.html\n\n-----------------------------\n## Inventory Plugin\n\n### Configuration\nDefine an inventory file (`*.d42.yml`) \n\nView documentation using `ansible-doc -t inventory device42.d42.d42`\n\nEnvironmental variables not defined:\n```\nplugin: device42.d42.d42\nurl: https://10.10.10.10\nusername: admin\npassword: password\nssl_check: False\ndebug: False\nclean_device_name: True\nkeyed_groups:\n    - key: d42_service_level\n      prefix: ''\n      separator: ''\n```\n\nEnvironmental variables defined:\n```\nplugin: device42.d42.d42\nkeyed_groups:\n    - key: d42_service_level\n      prefix: ''\n      separator: ''\n```\nSee [Ansible documentation](https://docs.ansible.com/ansible/latest/plugins/inventory/constructed.html) for more constructed examples.\n\n### How to run\nfrom the directory of your newly created file run the following command.\n\n```bash\nansible-inventory -i *.d42.yml --graph\n```\n\n-----------------------------\n## Lookup Plugin\n\n### Configuration\n\nIf using environmental variables skip this step, if not create a d42.py or d42_prompt.py in ansible/lib/ansible/plugins/lookup/\nwith the following information\n```\nD42_USER = 'device42 user'\nD42_PWD = 'device42 password'\nD42_URL = 'https:// device42 server IP address'\nD42_SKIP_SSL_CHECK = False\nD42_DEBUG = False\nD42_CLEAN_DEVICE_NAME = True\n```\n\n### How to run\n```\nTo get password call: lookup('d42', 'device_name', 'password', 'username')\n```\ndevice_name and username need to be filled in\n```\nTo run any doql request: lookup('d42', 'SELECT ... FROM ...', 'doql', 'list')\n```\ndoql query need to be filled in + we need to set data type of returned result ( 'string', 'list', 'list_dicts' )\n* string - return single string without column headers\n* list - return list of string ( we split DOQL result line by line without column headers )\n* list_dicts - return list of dicts with column headers\n\nAll above works the same for the `prompt` version, we just add 3 more arguments in the yaml file, please check reference in promt example.\n\nThe following was tested in a playbook using the included example template `example_playbook.yaml`\nplaybooks can be run by using the following command\n\n```bash\nansible-playbook *.yaml -f 10\n```\n## Legacy Inventory Usage\n-----------------------------\n\n### Requirements\n- ansible 2.9+\n- python 3.6.x+ or python 2.7.x\n- Device42 16.12.00+\n- requests (you can install it with pip install requests or apt-get install python-requests)\n- Ansible must have an available connection to your Device42 instance in order to collect devices for inventory\n\n    * rename conf.sample.ini to conf.ini\n    * in conf add D42 URL/credentials ( also instead of conf file, possible to use environment variables )\n```\n# ====== Device42 upload settings =========\n[DEFAULT]\nD42_USER = admin\nD42_PWD = adm!nd42\nD42_URL = https://10.10.10.10\nD42_SKIP_SSL_CHECK = True\nD42_DEBUG = FALSE\n\n# ====== Ansible settings =========\n[DOQL]\nGROUP_BY_QUERY = select name, service_level from view_device_v1\nGROUP_BY_FIELD = service_level\nGROUP_BY_REFERENCE_FIELD = name\nSPLIT_GROUP_BY_COMMA = False\n```\n\nNavigate to the root folder of the script `.../ansible_device42`\n\nRun the `python -m contrib.inventory.d42_ansible_inventory_hostfile` and enjoy!\n\nAlso you may use automatic version with Ansible commands ex.\n\nping :\n\n`ansible all -i d42_ansible_dynamic_inventory.py -m ping`\n\ncopy ssh file :\n\n`ansible all -i d42_ansible_dynamic_inventory.py -m authorized_key -a \"user=root key='ssh-rsa AAAA...XXX == root@hostname'\"`\n\nmodify file :\n\n`ansible all -i d42_ansible_dynamic_inventory.py -m lineinfile -a \"dest=/etc/group regexp='^(users:x:100:)(.*)' line='\\1ldapusername,\\2' state=present backrefs=yes\"`\n\n\n\nand much more! [Please read Ansible docs.](https://ansible-tips-and-tricks.readthedocs.io/en/latest/ansible/commands/)\n\nIf you have any questions - feel free to reach out to us at support at device42.com\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevice42%2Fansible_device42","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevice42%2Fansible_device42","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevice42%2Fansible_device42/lists"}