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 }}
Le lundi 16 octobre 2017 10:33:56 UTC+2, Mariano Obarrio Miles a écrit :
>
> 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
>
>
>
>
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/20d68160-5f2a-4857-b517-4188480f259f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.