Created
January 19, 2021 15:20
-
-
Save egeneralov/40c2a628198c3f1118d73566a41be6b8 to your computer and use it in GitHub Desktop.
how to add hosts to ansible inventory in dynamic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - hosts: localhost | |
| gather_facts: no | |
| vars: | |
| api_token: "${API_TOKEN_HERE}" | |
| limit: 25 | |
| tasks: | |
| - uri: | |
| url: "https://api.digitalocean.com/v2/droplets?page=1&per_page={{ limit }}" | |
| return_content: yes | |
| headers: | |
| Content-Type: application/json | |
| Authorization: "Bearer {{ api_token }}" | |
| register: _result | |
| # - debug: | |
| # msg: "{{ item.networks.v4[1].ip_address }}" | |
| # with_items: "{{ _result.json.droplets }}" | |
| # loop_control: | |
| # label: "{{ item.name }}" | |
| - add_host: | |
| name: "{{ item.name|default(item.id) }}" | |
| ansible_host: "{{ item.networks.v4[1].ip_address|default(item.networks.v4[0].ip_address) }}" | |
| groups: digitalocean | |
| id: "{{ item.id }}" | |
| size: "{{ item.size_slug }}" | |
| ip: "{{ item.networks.v4[0].ip_address }}" | |
| region: "{{ item.region.slug }}" | |
| with_items: "{{ _result.json.droplets }}" | |
| loop_control: | |
| label: "{{ item.name }}" | |
| - hosts: digitalocean | |
| gather_facts: no | |
| tasks: | |
| - debug: | |
| msg: "{{ inventory_hostname }} {{ id }} {{ ip }} {{ size }} {{ region }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment