ansible / awx

AWX provides a web-based user interface, REST API, and task engine built on top of Ansible. It is one of the upstream projects for Red Hat Ansible Automation Platform.
Other
13.88k stars 3.4k forks source link

foreman inventory script does not properly detect IP addresses when satellite6_want_ssh_ansible_host=True #2916

Closed kdelee closed 4 years ago

kdelee commented 5 years ago
ISSUE TYPE
COMPONENT NAME
SUMMARY

We updated the foreman.py script and got https://github.com/ansible/ansible/pull/34169 According to docs, I get to override vars in foreman.ini by providing them in extra vars on source with satellite6_ prepended to front of var: https://docs.ansible.com/ansible-tower/latest/html/userguide/inventories.html#red-hat-satellite-6

If I provide satellite6_want_ssh_ansible_host: True I see NO effect and DO NOT see the expected:

"ansible_ssh_host": "192.168.0.101",

in the vars related to host in inventory

ENVIRONMENT
STEPS TO REPRODUCE

Run sync with satellite source with satellite6_want_ssh_ansible_host: True

EXPECTED RESULTS

Get the following value in my vars for host:

"ansible_ssh_host": "192.168.0.101",
ACTUAL RESULTS

No effect, same as not passing it.

ADDITIONAL INFORMATION

Contact me for reproducer satellite

wenottingham commented 5 years ago

It would need special handling, see how other vars are handled (it goes in the other section)

AlanCoding commented 5 years ago

This is now ready to be implemented.

rmkraus commented 4 years ago

This seems to be stale since March. What is the status on this one? Can I help with this in any way?

For anyone that stumbles upon this trying to find out the issue: All of the relevant configuration for the dynamic inventory plugin goes into the [ansible] section. When Tower creates the configuration file when running the inventory script, it puts everything into the [foreman] section. Most dynamic inventory scripts read their config out of a section with the same name as the plugin, so this behavior is usually correct, but not for the Foreman plugin.

AlanCoding commented 4 years ago

Some information goes into [ansible] and some goes into [foreman]. You have to go by the example config file.

Let's identify the exact syntax expected for this issue. The example config is:

https://github.com/keithresar/ansible/blob/devel/contrib/inventory/foreman.ini

this contains

[ansible]
# Whether to populate the ansible_ssh_host variable to explicitly specify the
# connection target.  Only tested with Katello (Red Hat Satellite).
# If the foreman 'ip' fact exists then the ansible_ssh_host varibale is populated
# to permit connections where DNS resolution fails.
want_ansible_ssh_host = False

But the parameter needs to get passed through the inventory logic.

This new parameter is identical in nature to satellite6_want_hostcollections

https://github.com/ansible/awx/blob/b5fa1606bd14bc96d6f884dfee15bd38d06d8441/awx/main/models/inventory.py#L2594-L2616

I don't want to do any refactoring here. We have one new parameter to add, which should follow the same patterns as existing parameters.

rmkraus commented 4 years ago

I see, thanks for pointing me in that direction. That works.

What do you think is the best approach for documenting this?

rmkraus commented 4 years ago

Also, what are your thoughts on enabling control of the rich_params variable as well?

AlanCoding commented 4 years ago

it's in foreman.ini, so 👍 to adding that in this pattern too.

elyezer commented 4 years ago

I was trying to verify this and need some guidance. The original issue states that AWX should support the new variables added to the foreman.ini and I tried to run an inventory sync with the following vars:

satellite6_want_ansible_ssh_host: true
satellite6_group_prefix: sat6_
satellite6_want_facts: false

And I got proper prefix and didn't get the facts as expected but the satellite6_want_ansible_ssh_host seems not to be working. This is what the vars of a given host look like after the sync:

foreman:
  host_group: null
  id: 2
  ipv4: <ip redcted>
  location: Default Location
  name: <name redacted>
  organization: Default Organization
  smart_proxies:
    - <redacted proxy>
  subnet: null
foreman_params: {}

Am I doing something wrong?

elyezer commented 4 years ago

I tried couple more times and I still don't see the ansible_ssh_host being set when satellite6_want_ansible_ssh_host is set to true on the inventory source:

Screenshot from 2020-02-11 15-52-54

Then after a successful inventory source sync the ansible_ssh_host is not present on the host's variables.

ryanpetrello commented 4 years ago

@AlanCoding @jladdjr @jainnikhil30 any of you have any feedback or thoughts on this? Sounds like this one maybe isn't squashed just yet?

AlanCoding commented 4 years ago

Looking back at:

https://github.com/ansible/awx/issues/2916#issuecomment-584834026

Made me think, cross-referencing with:

https://github.com/ansible/ansible/blob/devel/contrib/inventory/foreman.py#L642

                if self.want_ansible_ssh_host and 'ip' in self.cache[hostname]:

Indeed, it is true that those hostvars given do not have "ip" in them. It has ipv4 instead. So that speculates some possibilities... does this server have newer stuff that the script needs to be updated for? Was the param want_IPv4 also set, and does that have an incompatibility with this?

ryanpetrello commented 4 years ago

That's what it is @AlanCoding, it's just a bug in the script:

image

Honestly, there's probably not going to be much we can do about this outside of our upcoming move to the plugin.

ryanpetrello commented 4 years ago

This (hypothetical) diff to the script fixes it for me:

diff --git a/awx/plugins/inventory/foreman.py b/awx/plugins/inventory/foreman.py
index c3f97710d2..d65dcecfed 100755
--- a/awx/plugins/inventory/foreman.py
+++ b/awx/plugins/inventory/foreman.py
@@ -644,8 +644,10 @@ class ForemanInventory(object):
                     'foreman': self.cache[hostname],
                     'foreman_params': self.params[hostname],
                 }
-                if self.want_ansible_ssh_host and 'ip' in self.cache[hostname]:
-                    self.inventory['_meta']['hostvars'][hostname]['ansible_ssh_host'] = self.cache[hostname]['ip']
+                for key in ('ip', 'ipv4', 'ipv6'):
+                    if self.want_ansible_ssh_host and key in self.cache[hostname]:
+                        self.inventory['_meta']['hostvars'][hostname]['ansible_ssh_host'] = self.cache[hostname][key]
+                        break
                 if self.want_facts:
                     self.inventory['_meta']['hostvars'][hostname]['foreman_facts'] = self.facts[hostname]

image

ryanpetrello commented 4 years ago

@ares @sjha4 either of you have any thoughts on this?

jladdjr commented 4 years ago

If that change makes sense to others, sounds good to me. I confirmed that @elyezer's instance doesn't have any instances with the ip field set. When I changed the snippet @ryanpetrello mentioned to ipv4, the hostvars were populated.

Echoing @ryanpetrello 's comment, with the script about to be replaced with the plugin, we should be thinking about that, too. If @ryanpetrello's change looks good, we should apply it to the plugin as well (which also only keys off of the ip field)

sjha4 commented 4 years ago

@ryanpetrello : Although I don't have much context around this, the change in the snippet above looks good to me. :+1:

ryanpetrello commented 4 years ago

@jladdjr if you want to turn this into a PR for the plugin, that would awesome. Personally, I wouldn't bother with the script at this point given our plans to move away soon.

AlanCoding commented 4 years ago

If @ryanpetrello's change looks good, we should apply it to the plugin as well (which also only keys off of the ip field)

Good find there, I agree with Ryan's comment, that should be patched in the plugin where you linked.

jladdjr commented 4 years ago

@AlanCoding @ryanpetrello - patched ^

jladdjr commented 4 years ago

The foreman plugin has been patched. When awx switches to using the plugin, it should pick up the correct behavior.

@kdelee - seems like we can close this now.

elyezer commented 4 years ago

@jladdjr is there a separate issue to track the switch to the plugin? I think it would be good to link here.

ryanpetrello commented 4 years ago

@elyezer @jladdjr probably this one?

https://github.com/ansible/awx/issues/3509

jladdjr commented 4 years ago

Yep, that's it 👍

kdelee commented 4 years ago

Closing this as a duplicate, because fixing this is equivalent to fixing https://github.com/ansible/awx/issues/3509. When the plugin lands, we should test this