ansible-collections / community.vmware

Ansible Collection for VMware
GNU General Public License v3.0
352 stars 336 forks source link

vmware_vm_inventory: ansible_host value #902

Open fvahid opened 3 years ago

fvahid commented 3 years ago

My VM have multiple IP address after inventory sync on AWX , ansible_host value is not match to my need, is any solution exist to fix the problem and filter one desire ip?

I used compose var like this ansible_host: net.guest | json_parser (...) but did not works

Thanks

ansibullbot commented 3 years ago

Files identified in the description: None

If these files are inaccurate, please update the component name section of the description or use the !component bot command.

click here for bot help

ansibullbot commented 3 years ago

@fvahid: Greetings! Thanks for taking the time to open this issue. In order for the community to handle your issue effectively, we need a bit more information.

Here are the items we could not find in your description:

Please set the description of this issue with this template: https://raw.githubusercontent.com/ansible/ansible/devel/.github/ISSUE_TEMPLATE/bug_report.md

click here for bot help

Akasurde commented 3 years ago

!component =plugins/inventory/vmware_vm_inventory.py

ansibullbot commented 3 years ago

Files identified in the description:

If these files are inaccurate, please update the component name section of the description or use the !component bot command.

click here for bot help

ansibullbot commented 3 years ago

cc @Tomorrow9 @goneri @pgbidkar @warthog9 click here for bot help

Akasurde commented 3 years ago

@fvahid Thanks for reporting this issue.

What is net.guest? This is not a valid value. You may want to refer https://docs.ansible.com/ansible/latest/scenario_guides/vmware_scenarios/vmware_inventory_vm_attributes.html#ipaddress-str-optional for a valid value.

needs_info

fvahid commented 3 years ago

Thanks @Akasurde I use this document https://docs.ansible.com/ansible/latest/scenario_guides/vmware_scenarios/vmware_inventory_vm_attributes.html#ipaddress-str-optional but ipAddress (str, optional) return only first ip in vmware , I need to get second IP for example, when my vmware quest have got multiple IP address problem occurred

fvahid commented 3 years ago

if you see this picture image_2021_06_13T08_54_06_532Z I need to get 100.0.0.71 and put to ansible_host vars

Akasurde commented 3 years ago

@fvahid you can do like this

compose:
    ansible_host: 'guest.net[1].ipAddress[0]'

for me -

{
    "_meta": {
        "hostvars": {
            "centos_7": {
                "ansible_host": "192.168.101.10",
                "config": {
                    "guestId": "centos7_64Guest",
                    "name": "centos_7"
                },
Screenshot 2021-06-18 at 19 22 11

But this is NOT RECOMMENDED since not every VM has a valid IP address at net[1] and ipAddress[0].

needs_info

fvahid commented 3 years ago

maybe some vm have 'guest.net[0].ipAddress[1]' or 'guest.net[2].ipAddress[1]' valid IP address how can I filter correct ip, I teste json_parser I did not work something like this ansible_host: "guest.net | json_parser(json_query.....)"

every time that I restart guest vm IP order in guest.net changed and absolutely 'guest.net[0].ipAddress[1] value change to another IP

fvahid commented 3 years ago

any update @Akasurde

cscal commented 3 years ago

I have the same issue, using ansible 2.9.23. Our datacenter has servers with multiple NICs on different networks, and only one of those NICs can connect with our Ansible nodes, and it's usually not the first one. So I want to be able to select the IP address from the list of dicts in guest.net.

I have tried this: compose: ansible_host: "{{ guest.net|select('ipAddress', 'contains', '10.177.') }}"

And I have tried this: compose: ansible_host: "{% for item in guest.net %} {% if '10.177.' in item.ipAddress[0] %} {{ item.ipAddress[0] }} {% endif %} {% endfor %}"

And I have tried many variations on these with additional jinja filters, specifying the object with [0] or [1], but nothing returns an IP address for ansible_host. The only expression that sometimes works is: "{{ guest.net[2].ipAddress[0] }}"

However this only selects the third IP address, which isn't always the correct one. I need to be able to choose the subnet. This is a big issue for us and prevents us from being able to use this plugin.

jdranchman commented 3 years ago

My current workaround is to set the vcenter vm name to the DNS entry of the interface I want to talk to via ansible (and use config.name). OK for me but not optimal for the admittedly few vm's that are sitting on several distributed switches. It would be way easier to filter using something like the '| select' that @cscal above tried - or better yet a parameter specifying a substring pattern to match.

goneri commented 3 years ago

This looks like a dup with https://github.com/ansible-collections/community.vmware/issues/1008.

pidreher commented 1 year ago

@goneri @Akasurde since #1008 has been resolved by #1310, I think this issue here can be closed as well.