Post by larsonjoshef on Jan 1, 2021 7:29:14 GMT
How to access inventory_hostname of all the hosts of Ansible Dynamic Inventory
Here is the code.
Text
$cat collect.yaml
---
- hosts: "{{ env }}:&{{ zone }}"
become: True
tasks:
- name: Get info on docker host and list images
docker_host_info:
containers: yes
register: result
- name: Debug dockerInfo
debug:
var: result.containers
when: result.host_info.Containers != 0
- name: adding it to groups using images and vars.
add_host:
name: "{{ inventory_hostname }}"
groups: "{{ item.Image | regex_replace('.*?/(.*?):.*', '\\1') }}"
container_name: '{{ item.Names[0] | regex_replace("^/", "") }}'
with_items: "{{ result.containers | flatten(1) }}"
i'm using dynamic inventory. Here is what i run:
Text
$ansible-playbook collect.yaml -i path_of_dynamic-inventory_folder/inventory.py -e env=dev -e zone=US
here is my output,which is only picking the first host.I would like to dynamic group the rest of the hosts in the play:
YAML
TASK [Debug dockerInfo]
ok: [vm1.nodekite.com] => {
"result.containers": [
{
"Image": "ca.docker/webproxy:1.0.0",
"Names": [
"/customer1"
],
},
{
"Image": "docker.local/legacustomer:1.0.1",
"Names": [
"/webproxy"
],
},
]}
ok: [vm2.nodekite.com ] => {
"result.containers": [
{
"Image": "ca.docker/webproxy:1.0.0",
"Names": [
"/webproxyui"
],
},
{
"Image": "cna-docker-local/lega-customer:1.0.1",
"Names": [
"/webproxy"
],
},
]}
ok: [vm3.nodekite.com ] => {
"result.containers": [
{
"Image": "ca.docker/webproxy:1.0.0",
"Names": [
"/webproxy"
],
},
{
"Image": "local.docker/lega-customer:1.0.1",
"Names": [
"/customerr
],
},
]}
TASK [adding it to groups using images]
creating host via 'add_host': hostname=vm1.nodekite.com
changed: [vm1.nodekite.com] => {
"add_host": {
"groups": [
"webproxy"
],
"host_name": "vm1.nodekite.com",
"host_vars": {
"container_name": "customer1" }
},
}
PLAY RECAP:
vm1.nodekite.com : ok=3 changed=1 unreachable=0 <=====
vm2.nodekite.com : ok=2 changed=0 unreachable=0
vm3.nodekite.com : ok=2 changed=0 unreachable=0
I need to filter "{{ inventory_hostname }}" variable of all the hosts from the dynamic inventory. so that I could call module `add_host` for all hosts of the play.
I tried to alter the below code to item.inventory_hostname which doesn't work.
YAML
add_host:
name: "{{ item.inventory_hostname }}"
Could someone please assist.
Here is the code.
Text
$cat collect.yaml
---
- hosts: "{{ env }}:&{{ zone }}"
become: True
tasks:
- name: Get info on docker host and list images
docker_host_info:
containers: yes
register: result
- name: Debug dockerInfo
debug:
var: result.containers
when: result.host_info.Containers != 0
- name: adding it to groups using images and vars.
add_host:
name: "{{ inventory_hostname }}"
groups: "{{ item.Image | regex_replace('.*?/(.*?):.*', '\\1') }}"
container_name: '{{ item.Names[0] | regex_replace("^/", "") }}'
with_items: "{{ result.containers | flatten(1) }}"
i'm using dynamic inventory. Here is what i run:
Text
$ansible-playbook collect.yaml -i path_of_dynamic-inventory_folder/inventory.py -e env=dev -e zone=US
here is my output,which is only picking the first host.I would like to dynamic group the rest of the hosts in the play:
YAML
TASK [Debug dockerInfo]
ok: [vm1.nodekite.com] => {
"result.containers": [
{
"Image": "ca.docker/webproxy:1.0.0",
"Names": [
"/customer1"
],
},
{
"Image": "docker.local/legacustomer:1.0.1",
"Names": [
"/webproxy"
],
},
]}
ok: [vm2.nodekite.com ] => {
"result.containers": [
{
"Image": "ca.docker/webproxy:1.0.0",
"Names": [
"/webproxyui"
],
},
{
"Image": "cna-docker-local/lega-customer:1.0.1",
"Names": [
"/webproxy"
],
},
]}
ok: [vm3.nodekite.com ] => {
"result.containers": [
{
"Image": "ca.docker/webproxy:1.0.0",
"Names": [
"/webproxy"
],
},
{
"Image": "local.docker/lega-customer:1.0.1",
"Names": [
"/customerr
],
},
]}
TASK [adding it to groups using images]
creating host via 'add_host': hostname=vm1.nodekite.com
changed: [vm1.nodekite.com] => {
"add_host": {
"groups": [
"webproxy"
],
"host_name": "vm1.nodekite.com",
"host_vars": {
"container_name": "customer1" }
},
}
PLAY RECAP:
vm1.nodekite.com : ok=3 changed=1 unreachable=0 <=====
vm2.nodekite.com : ok=2 changed=0 unreachable=0
vm3.nodekite.com : ok=2 changed=0 unreachable=0
I need to filter "{{ inventory_hostname }}" variable of all the hosts from the dynamic inventory. so that I could call module `add_host` for all hosts of the play.
I tried to alter the below code to item.inventory_hostname which doesn't work.
YAML
add_host:
name: "{{ item.inventory_hostname }}"
Could someone please assist.