Problems with VMWARE and In-Memory Inventory

561 views
Skip to first unread message

Mariano Obarrio Miles

unread,
Oct 16, 2017, 4:33:56 AM10/16/17
to Ansible Project
Hi all,
  I don't know what I do wrong... I try create VMs (vmware) and provisioning after that. But when try add this new vms to inventory with add_host only add one.

  The task "Gather VM facts (vmware_guest_facts)" recover 2 IPs but then "Add Host to inventory" only add one IP to in-memory inventory.

  I try multiples scenarios (with_dict/set_fact....) to add_host to inventory but nothing work!

  Any idias what I do wrong, any suggestions?


Thxs,

Mariano

ansible 2.5.0
  config file
= /var/adm/ssoo/opt/ansible/ansible.cfg
  configured
module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python
module location = /usr/lib/python2.7/site-packages/ansible-2.5.0-py2.7.egg/ansible
  executable location
= /usr/bin/ansible
  python version
= 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]



# cat /etc/ansible/hosts
[centos-vms]
learn
[05:06] disk='10' datastore='LUN24_VNX5600_MIRROR' network='dvPortGroupCentral' memory='256' cpucount='1' osid='centos64Guest' ansible_ssh_private_key_file=/root/.ssh/learning.pem ansible_user=root ansible_password=xxxxxxx learn[05:06].node.consul=learn[05:06]


#!/usr/bin/ansible-playbook

---
- hosts: centos-vms
  gather_facts
: false
  connection
: local
  user
: root
 
  roles
:
   
- vmware


- hosts: in-memory-vms
  gather_facts
: false
  connection
: local
  user
: root


  roles
:
   
- common


# cat roles/vmware/tasks/main.yml
---

 
- include_tasks: create_vms_from_template.yml
    tags
:
     
- CreateVM from Template



# cat roles/vmware/tasks/create_vms_from_template.yml
---
- name: Create VM from Template
  vsphere_guest
:
    vcenter_hostname
: "{{ vcenter_hostname }}"
    username
: "{{ vcenter_user }}"
    password
: "{{ vcenter_pass }}"
    validate_certs
: no
    guest
: "{{ inventory_hostname }}"
    from_template
: yes
    template_src
: "CentOS_7.3_x64_1511-GPT-20GB_HDD"
    cluster
: VMwareAPP
    resource_pool
: "/Resources/SSOO"
    vm_extra_config
:
      notes
: "{{ notes }}"

- name: Espera asignacion de IP via DHCP
  pause
: seconds=60

- name: Gather VM facts
  vsphere_guest
:
    vcenter_hostname
: "{{ vcenter_hostname }}"
    username
: "{{ vcenter_user }}"
    password
: "{{ vcenter_pass }}"
    vmware_guest_facts
: yes
    validate_certs
: no
    guest
: "{{ inventory_hostname }}"
 
register: vm_facts

- name: IP address info
  debug
: msg="{{ vm_facts.ansible_facts.hw_eth0.ipaddresses[0] }} {{ inventory_hostname }}"

- name: Add Host to inventory
  add_host
:
    hostname
: "{{ vm_facts.ansible_facts.hw_eth0.ipaddresses[0] }}"
    groups
: in-memory-vms
    ansible_ssh_host
: "{{ vm_facts.ansible_facts.hw_eth0.ipaddresses[0] }}"
    ansible_user
: root
    ansible_password
: xxxxxxxxx
    ansible_distribution
: "CentOS"
    ansible_system
: "Linux"


- name: 'Inventory'
  debug
: var=groups


- name: 'Stop execution'
  fail
: msg="Playbook stoped."




[root@learn01 ansible]# ./createvm.yml 

PLAY [centos-vms] ***********************************************************************************************************************************************************************************************************


TASK
[vmware : include_tasks] ***********************************************************************************************************************************************************************************************
included
: /var/adm/ssoo/opt/ansible/roles/vmware/tasks/create_vms_from_template.yml for learn06, learn05


TASK
[vmware : Create VM from Template] *************************************************************************************************************************************************************************************
ok
: [learn06]
ok
: [learn05]


TASK
[vmware : Espera asignacion de IP via DHCP] ****************************************************************************************************************************************************************************
Pausing for 60 seconds
(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort)
Press 'C' to continue the play or 'A' to abort
ok
: [learn05]


TASK
[vmware : Gather VM facts] *********************************************************************************************************************************************************************************************
ok
: [learn05]
ok
: [learn06]


TASK
[vmware : IP address info] *********************************************************************************************************************************************************************************************
ok
: [learn05] => {
   
"msg": "50.0.232.157 learn05"
}
ok
: [learn06] => {
   
"msg": "50.0.232.158 learn06"
}


TASK
[vmware : Add Host to inventory] ***************************************************************************************************************************************************************************************
changed
: [learn05]


TASK
[vmware : Inventory] ***************************************************************************************************************************************************************************************************
ok
: [learn05] => {
   
"groups": {
       
"all": [
           
"learn05",
           
"learn06",
           
"taurus",
           
"learn01",
           
"learn02",
           
"learn03",
           
"learn04",
           
"50.0.232.157"
       
],
       
"centos-vms": [
           
"learn05",
           
"learn06"
       
],
       
"in-memory-vms": [

           
"50.0.232.157"    <---- ONLY ONE
       
],
       
"solaris": [
           
"taurus"
       
],
       
"ungrouped": []
   
}
}
ok
: [learn06] => {
   
"groups": {
       
"all": [
           
"learn05",
           
"learn06",
           
"taurus",
           
"learn01",
           
"learn02",
           
"learn03",
           
"learn04",
           
"50.0.232.157"
       
],
       
"centos-vms": [
           
"learn05",
           
"learn06"
       
],
       
"in-memory-vms": [

           
"50.0.232.157"   <---- ONLY ONE
       
],
       
"solaris": [
           
"taurus"
       
],
       
"ungrouped": []
   
}
}


TASK
[vmware : Stop execution] **********************************************************************************************************************************************************************************************
fatal
: [learn05]: FAILED! => {"changed": false, "failed": true, "msg": "Playbook stoped."}
fatal
: [learn06]: FAILED! => {"changed": false, "failed": true, "msg": "Playbook stoped."}
        to
retry, use: --limit @/var/adm/ssoo/opt/ansible/createvm.retry


PLAY RECAP
******************************************************************************************************************************************************************************************************************
learn05                    
: ok=7    changed=1    unreachable=0    failed=1  
learn06                    
: ok=5    changed=0    unreachable=0    failed=1  



Sebastien Desbois

unread,
Oct 16, 2017, 11:03:53 AM10/16/17
to Ansible Project
Hello,

The probleme seems that Ansible do not that it needs to loop on the add_host part, since there is no parameter like {{ inventory_hostname }} to indicate it needs to loop on it.

Maybe if you try to add a false variable set to the inventory_hostname, it could help it to loop ?
Should look like this :
- name: Add Host to inventory
  add_host
:
    hostname
: "{{ vm_facts.ansible_facts.hw_eth0.ipaddresses[0] }}"
    groups
: in-memory-vms
    ansible_ssh_host
: "{{ vm_facts.ansible_facts.hw_eth0.ipaddresses[0] }}"
    ansible_user
: root
    ansible_password
: xxxxxxxxx
    ansible_distribution
: "CentOS"
    ansible_system
: "Linux"
    foo: {{ inventory_hostname }}

Mariano Obarrio Miles

unread,
Oct 17, 2017, 7:25:53 AM10/17/17
to Ansible Project
Hi Sebastien,
   I try with a loop but the result it's the same. Only add one of both. Learn05 (.232.141)

The loop:

- name: DEBUG Show inventory_hostname
  debug
: msg="inventory_hostname {{inventory_hostname}}"


- name: Add_host with Loop
  add_host
:
    name
: "{{item.value.ipv4}}"
    groups
: in-memory-vms
    ansible_ssh_host
: "{{item.value.ipv4}}"
    ansible_distribution
: "CentOS"
    ansible_system
: "Linux"
    ansible_ssh_private_key_file
: /root/.ssh/ansible.pem
  with_dict
: "{{ instance_facts }}"
 
when: item.key in ['instance']
  changed_when
: false


When execute the new playbook, only added learn05, but the debug in "TASK [vmware : DEBUG Show inventory_hostname]" show two hosts.

In the common role I added a debug line "TASK [common : debug]" to show all groups outside of vmware role. If you can see the in-memory-vms group  have only one IP learn05 (.232.141)


[root@learn01 ansible]# ./createvm.yml

PLAY [centos-vms] ***********************************************************************************************************************************************************************************************************************


TASK
[vmware : include_tasks] ***********************************************************************************************************************************************************************************************************
included
: /var/adm/ssoo/opt/ansible/roles/vmware/tasks/create_vms_from_template.yml for learn05, learn06


TASK
[vmware : Create VM from Template] *************************************************************************************************************************************************************************************************
changed
: [learn06 -> localhost]
changed
: [learn05 -> localhost]


TASK
[vmware : Espera asignacion de IP via DHCP] ****************************************************************************************************************************************************************************************

Pausing for 60 seconds
(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort)

ok
: [learn05]


TASK
[vmware : Gather instance facts] ***************************************************************************************************************************************************************************************************
ok
: [learn06]
ok
: [learn05]


TASK
[vmware : DEBUG Show inventory_hostname] *******************************************************************************************************************************************************************************************
ok
: [learn05] => {
   
"msg": "inventory_hostname learn05"
}
ok
: [learn06] => {
   
"msg": "inventory_hostname learn06"
}


TASK
[vmware : Add_host with Loop] ******************************************************************************************************************************************************************************************************
ok
: [learn05] => (item={'key': u'instance', 'value': {u'hw_name': u'learn05', u'hw_power_status': u'poweredOn', u'hw_processor_count': 1, u'hw_memtotal_mb': 256, u'module_hw': True, u'snapshots': [], u'customvalues': {}, u'hw_guest_full_name': u'CentOS 4/5/6/7 (64-bit)', u'hw_interfaces': [u'eth0'], u'hw_guest_id': u'centos64Guest', u'current_snapshot': None, u'ipv6': None, u'guest_tools_version': u'10249', u'hw_eth0': {u'macaddress': u'00:50:56:81:16:fe', u'addresstype': u'assigned', u'macaddress_dash': u'00-50-56-81-16-fe', u'label': u'Network adapter 1', u'ipaddresses': [u'50.0.232.141'], u'summary': u'DVSwitch: 79 24 01 50 c4 9c af f0-ce 89 4a 9a 00 1a d2 e8'}, u'hw_product_uuid': u'4201f0a4-fa12-f243-a715-227d401193bf', u'annotation': u'', u'guest_tools_status': u'guestToolsRunning', u'ipv4': u'50.0.232.141'}})
skipping
: [learn05] => (item={'key': 'attempts', 'value': 1})
skipping
: [learn05] => (item={'key': 'changed', 'value': False})
skipping
: [learn05] => (item={'key': u'failed', 'value': False})


PLAY
[in-memory-vms] ********************************************************************************************************************************************************************************************************************


TASK
[common : debug] *******************************************************************************************************************************************************************************************************************
ok
: [50.0.232.141] => {

   
"groups": {
       
"all": [
           
"learn05",
           
"learn06",
           
"taurus",
           
"learn01",
           
"learn02",
           
"learn03",
           
"learn04",

           
"50.0.232.141"

       
],
       
"centos-vms": [
           
"learn05",
           
"learn06"
       
],
       
"in-memory-vms": [

           
"50.0.232.141"

       
],
       
"solaris": [
           
"taurus"
       
],
       
"ungrouped": []
   
}
}

...
...
...

PLAY RECAP
******************************************************************************************************************************************************************************************************************************
50.0.232.141               : ok=14   changed=0    unreachable=0    failed=0  
learn05                    
: ok=6    changed=1    unreachable=0    failed=0  
learn06                    
: ok=4    changed=1    unreachable=0    failed=0  

jean...@lenhof.eu.org

unread,
Oct 17, 2017, 8:07:52 AM10/17/17
to ansible...@googlegroups.com
Hi,

Ansible 2.5 is not stable as of now.... I would recommend to use 2.3.x or 2.4.y version to use in production...
Anyway this is not the problem here.
As written in documentation, add_host broke loop_play (in note section : This module bypasses the play host loop and only runs once for all the hosts in the play, if you need it to iterate use a with_ directive). So it works as defined

Regards,

JYL
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b55dd768-b9b0-4621-909f-9e8cfdfbf36e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jean...@lenhof.eu.org

unread,
Oct 17, 2017, 8:12:10 AM10/17/17
to ansible...@googlegroups.com
Hi,

There's a loop indeed, but I thinks there's something wrong in your variable because it is looping with
first iteration item.key ==> 'instance"
second iteration item.key ==> 'attempts'
third_iteration item.key ==> 'changed',
fourth iteration item.key ==> 'failed'

So it's more a bug in your variable you are using than the loop itself which the problem lies...

Regards,

JYL
--

You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Sebastien Desbois

unread,
Oct 17, 2017, 8:57:28 AM10/17/17
to Ansible Project
Hello,

For my part, I'm using the way described by JYL.

  - include_role:
      name
: myrolewhoaddsininventory
    with_items
: "{{ mylist }}"

the add_host task is included amonfg other things in the role.
And at the end, I get the in-memory inventory properly filed.

Mariano Obarrio Miles

unread,
Oct 17, 2017, 9:00:37 AM10/17/17
to Ansible Project
Hi Jean,

About this:

There's a loop indeed, but I thinks there's something wrong in your variable because it is looping with
first iteration item.key ==> 'instance"
second iteration item.key ==> 'attempts'
third_iteration item.key ==> 'changed',
fourth iteration item.key ==> 'failed'

In the loop I use "when: item.key in ['instance']" because the facts have others keys I need ignore.

ASK [vmware : Add_host with Loop] ******************************************************************************************************************************************************************************************************

ok
: [learn05] => (item={'key': u'instance', 'value': {u'hw_name': u'learn05', u'hw_power_status': u'poweredOn', u'hw_processor_count': 1, u'hw_memtotal_mb': 256, u'module_hw': True, u'snapshots': [], u'customvalues': {}, u'hw_guest_full_name': u'CentOS 4/5/6/7 (64-bit)', u'hw_interfaces': [u'eth0'], u'hw_guest_id': u'centos64Guest', u'current_snapshot': None, u'ipv6': None, u'guest_tools_version': u'10249', u'hw_eth0': {u'macaddress': u'00:50:XX:81:XX:XX', u'addresstype': u'assigned', u'macaddress_dash': u'00-XX-XX-XX-XX-XX', u'label': u'Network adapter 1', u'ipaddresses': [u'50.0.232.141'], u'summary': u'DVSwitch: 79 24 01 50 c4 9c af f0-ce 89 4a 9a 00 1a d2 e8'}, u'hw_product_uuid': u'4201f0a4-fa12-f243-a715-227d401193bf', u'annotation': u'', u'guest_tools_status': u'guestToolsRunning', u'ipv4': u'50.0.232.141'}})

skipping
: [learn05] => (item={'key': 'attempts', 'value': 1})
skipping
: [learn05] => (item={'key': 'changed', 'value': False})
skipping
: [learn05] => (item={'key': u'failed', 'value': False})

ok: [learn05] => {
   
"instance_facts": {
       
"attempts": 1,
       
"changed": false,
       
"failed": false,
       
"instance": {
           
"annotation": "",
           
"current_snapshot": null,
           
"customvalues": {},
           
"guest_tools_status": "guestToolsRunning",
           
"guest_tools_version": "10249",
           
"hw_eth0": {
               
"addresstype": "assigned",
               
"ipaddresses": [
                   
"50.0.232.152"
               
],
               
"label": "Network adapter 1",
               
"macaddress": "00:X0:5X:81:60:Xa",
               
"macaddress_dash": "00-X0-5X-81-60-Xa",
               
"summary": "DVSwitch: 79 24 01 50 c4 9c af f0-ce 89 4a 9a 00 1a d2 e8"
           
},
           
"hw_guest_full_name": "CentOS 4/5/6/7 (64-bit)",
           
"hw_guest_id": "centos64Guest",
           
"hw_interfaces": [
               
"eth0"
           
],
           
"hw_memtotal_mb": 256,
           
"hw_name": "learn05",
           
"hw_power_status": "poweredOn",
           
"hw_processor_count": 1,
           
"hw_product_uuid": "4201c0c8-179f-8f8b-eb84-ad1684f493bf",
           
"ipv4": "50.0.232.152",
           
"ipv6": null,
           
"module_hw": true,
           
"snapshots": []
       
}
   
}
}



About now, I downgrade ansible to 2.4 and remove the loop but the behaviour it's the same.

[root@learn01 ansible]# ansible --version
ansible 2.4.0.0
  config file
= /etc/ansible/ansible.cfg
  configured
module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']

  ansible python
module location = /usr/lib/python2.7/site-packages/
ansible
  executable location
= /usr/bin/ansible
  python version
= 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]


---
- name: Create VM from Template

  vmware_guest
:

    hostname
: "{{ vcenter_hostname }}"
    username
: "{{ vcenter_user }}"
    password
: "{{ vcenter_pass }}"

    datacenter
: "xxxxxx"
    cluster
: VMwareAPP
    validate_certs
: no
    resource_pool
: "SSOO"
    name
: "{{ inventory_hostname }}"
    state
: poweredon
   
template: "CentOS_7.3_x64_1511-GPT-20GB_HDD"
#    wait_for_ip_address: yes
    guest_id
: "{{osid}}"
    disk
:
   
- size_gb: "{{disk_size}}"
      type
: "{{disk_type}}"
      datastore
: "{{ds_name}}"
    hardware
:
      memory_mb
: "{{memory_size}}"
      num_cpus
: "{{cpucount}}"
    networks
:
   
- name: "{{network_name}}"
  delegate_to
: localhost
 
register: deploy


- name: Espera asignacion de IP via DHCP
  pause
: seconds=60 ## Hace una pausa para esperar que encienda la VM y vCenter capture la IP


- name: Gather instance facts
  vmware_guest_facts
:
    hostname
: "{{ vcenter_hostname }}"
    validate_certs
: false
    username
: "{{ vcenter_user }}"
    password
: "{{ vcenter_pass }}"
    datacenter
: "xxxxxxx"
    uuid
: "{{deploy.instance.hw_product_uuid}}"
 
register: instance_facts
 
until: instance_facts != -1
  changed_when
: false
  retries
: 30
  delay
: 2


- name: Add vSphere host to inventory
  add_host
:
    name
: "{{instance_facts.instance.ipv4}}"
    groups
: in-memory-vms
    ansible_ssh_host
: "{{instance_facts.instance.ipv4}}"
    ansible_user
: root
    ansible_password
: xxxxxx
    ansible_distribution
: "CentOS"
    ansible_system
: "Linux"
    ansible_ssh_private_key_file
: /root/.ssh/ansible.pem



[root@learn01 ansible]# ./createvm.yml

PLAY [centos-vms] ***********************************************************************************************************************************************************************************************************************


TASK
[vmware : include_tasks] ***********************************************************************************************************************************************************************************************************
included
: /var/adm/ssoo/opt/ansible/roles/vmware/tasks/create_vms_from_template.yml for learn05, learn06


TASK
[vmware : Create VM from Template] *************************************************************************************************************************************************************************************************

changed
: [learn05 -> localhost]
changed
: [learn06 -> localhost]



TASK
[vmware : Espera asignacion de IP via DHCP] ****************************************************************************************************************************************************************************************
Pausing for 60 seconds
(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort)
ok
: [learn05]


TASK
[vmware : Gather instance facts] ***************************************************************************************************************************************************************************************************

ok
: [learn05]
ok
: [learn06]


TASK
[vmware : Add vSphere host to inventory] *******************************************************************************************************************************************************************************************
changed
: [learn05]



PLAY
[in-memory-vms] ********************************************************************************************************************************************************************************************************************


TASK
[common : debug] *******************************************************************************************************************************************************************************************************************

ok
: [50.0.232.144] => {

   
"groups": {
       
"all": [
           
"learn05",
           
"learn06",
           
"taurus",
           
"learn01",
           
"learn02",
           
"learn03",
           
"learn04",

           
"50.0.232.144"

       
],
       
"centos-vms": [
           
"learn05",
           
"learn06"
       
],
       
"in-memory-vms": [

           
"50.0.232.144"

       
],
       
"solaris": [
           
"taurus"
       
],
       
"ungrouped": []
   
}
}


...
... Others Tasks
...


PLAY RECAP
******************************************************************************************************************************************************************************************************************************
50.0.232.144               : ok=14   changed=0    unreachable=0    failed=0  
learn05                    
: ok=5    changed=2    unreachable=0    failed=0  
learn06                    
: ok=3    changed=1    unreachable=0    failed=0  


Mariano

Mariano Obarrio Miles

unread,
Oct 17, 2017, 9:22:17 AM10/17/17
to Ansible Project
Hi Sebastien,
   You  have a role to create VMs and other to add_host to inventory? I try it and send you a feedback soon.

Thxs!

M.

Sebastien Desbois

unread,
Oct 17, 2017, 12:54:08 PM10/17/17
to Ansible Project
Hello

I use the same role to create the VM and add_host.
This way, with the first item, it creates the VM and add it to the in-memory inventory.
Then it loops and do the same with the second item.
Etc...
Unless you destroy your in-memory inventory at some point, it will be available and used through all the play.

Best Regards,

Mariano Obarrio Miles

unread,
Oct 18, 2017, 6:19:45 AM10/18/17
to Ansible Project
Ok now all work fine, I make some mistakes!!! 

Downgrade to stable Ansible version just like suggested Jean solved different issues like "wait_for_ip_address: true" in ansible 2.4 work but in 2.5 not.

Now I don't use in-memory inventory,  I solve it setting ansible_ssh_host, because the server exist in the inventory and I don't need duplicate it.

#!/usr/bin/ansible-playbook
---
- hosts: centos-vms
  gather_facts
: false
  connection
: local
  user
: root
 
  roles
:
   
- vmware


- hosts: centos-vms
  user
: root
  connection
: ssh


  roles
:
   
- common

---
- name: Create VM from Template

  vmware_guest
:

    hostname
: "{{ vcenter_hostname }}"
    username
: "{{ vcenter_user }}"
    password
: "{{ vcenter_pass }}"

    datacenter
: "xxxxx"

    cluster
: VMwareAPP
    validate_certs
: no
    resource_pool
: "SSOO"
    name
: "{{ inventory_hostname }}"
    state
:
poweredon
   
template: "CentOS_7.4_x64_1708-GPT-20GB_HDD"
    wait_for_ip_address
: true

    guest_id
: "{{osid}}"
    disk
:
   
- size_gb: "{{disk_size}}"
      type
: "{{disk_type}}"
      datastore
: "{{ds_name}}"
    hardware
:
      memory_mb
: "{{memory_size}}"
      num_cpus
: "{{cpucount}}"
    networks
:
   
- name: "{{network_name}}"
  delegate_to
: localhost
 
register: deploy


- name: Set Facts
  set_fact
:
    uuid
: "{{deploy.instance.hw_product_uuid}}"
    ansible_ssh_host
: "{{deploy.instance.ipv4}}"
    ansible_user
: root
    ansible_password
: xxxxx
    ansible_ssh_private_key_file
: /root/.ssh/ansible.pem


Thanks all,

Best Regards,

Mariano
Reply all
Reply to author
Forward
0 new messages