theforeman / foreman_ansible_inventory

Foreman dynamic inventory script for ansible - Now merged into Ansible itself
GNU General Public License v3.0
70 stars 31 forks source link

Systems nested hostgroups are only grouped by top-level hostgroup #27

Open wzzrd opened 8 years ago

wzzrd commented 8 years ago

I have a hostgroup hierarchy like this: RHEL7/Webserver/Production. With the current script, I only get automatic grouping on "Production", which is ambiguous, since I might as well have RHEL7/Database/Production.

What would be great if the script could either group automatically on all nested hostgroups (group for RHEL7, Webserver / Database and Production), or on the whole hierarchy (automatic group in Ansible would be RHEL7/Webserver/Production).

agx commented 8 years ago

On Fri, Aug 19, 2016 at 03:40:15AM -0700, Maxim Burgerhout wrote:

I have a hostgroup hierarchy like this: RHEL7/Webserver/Production. With the current script, I only get automatic grouping on "Production", which is ambiguous, since I might as well have RHEL7/Database/Production.

It will also create groups named RHEL7_Database_Production

What would be great if the script could either group automatically on all nested hostgroups (group for RHEL7, Webserver / Database and Production), or on the whole hierarchy (automatic group in Ansible would be RHEL7/Webserver/Production).

You can achieve that using parameters on the hostgroups and group_patterns.

wzzrd commented 8 years ago

Mmh. I must be doing something wrong then, because I only get the 'Production' one.

agx commented 8 years ago

If I create a nested hostgroup a / b / c I get a ansible group foreman_hostgroup_a_b_c . You can check all ansible groups in the generated .index file.

smnmtzgr commented 7 years ago

Hello, I can confirm that the issue of wzzrd exists. I have nested groups: datacenter / lan / distris-lan and with the dynamic inventory script I only get back "foreman_hostgroup_distris-lan" and "all". I've also checked the .index file.

I am using foreman 1.13 and Ansible 2.1.0

Regards, Simon

agx commented 7 years ago

On Mon, Oct 10, 2016 at 11:18:21PM -0700, Simon wrote:

Hello, I can confirm that the issue of wzzrd exists. I have nested groups: datacenter / lan / distris-lan and with the dynamic inventory script I only get back "distris-lan".

Please tell us how you came to this conclusion so we can debug this. Please allo paste your *.index file. It has all the generated host groups.

smnmtzgr commented 7 years ago

I came to this conclusion by executing the inventory script manually or called by ansible-playbook. In both situations I get the following groups:

{ "all": [ "rz1-distri-05", ], "foreman_hostgroup_distris_lan": [ "rz1-distri-05", ] }%

But in foreman I have configured: datacenter/lan/distris-lan

agx commented 7 years ago

On Mon, Oct 10, 2016 at 11:24:13PM -0700, Simon wrote:

{ "all": [ "rz1-distri-05", ], "foreman_hostgroup_distris_lan": [ "rz1-distri-05", ] }%

Hmmm...see my comment:

https://github.com/theforeman/foreman_ansible_inventory/issues/27#issuecomment-241629145

We're "simply" grabbing the hostgroup_name from the host object so maybe this changed in 1.13 and foreman puts the short hostgroup name there now instead of the title as before? You can check by performing this api call:

 curl https://theforeman.org/api/1.13/apidoc/v2/hosts/<hostid>

If that's the case then there's a bug in Foreman itself.

timogoebel commented 7 years ago

We're "simply" grabbing the hostgroup_name from the host object so maybe this changed in 1.13 and foreman puts the short hostgroup name there now instead of the title as before?

I don't recall such a change and just confirmed with 1.12. If you want to get the nested hierarchy, you need to use the hostgroup_title parameter. Let me give you an example: If you have two hostgroups: CoreOS and k8s-master-lxsbx (the latter being a child of the former) you get the following:

"hostgroup_name":"k8s-master-lxsbx","hostgroup_title":"CoreOS/k8s-master-lxsbx"
smnmtzgr commented 7 years ago

I've checked this and the following is returned by the API:

_"hostgroup_name":"distris-lan","hostgrouptitle":"datacenter/lan/distris-lan"

smnmtzgr commented 7 years ago

I have created this pull request: Pull Request #38

agx commented 7 years ago

On Tue, Oct 11, 2016 at 12:03:10AM -0700, Timo Goebel wrote:

We're "simply" grabbing the hostgroup_name from the host object so maybe this changed in 1.13 and foreman puts the short hostgroup name there now instead of the title as before?

I don't recall such a change and just confirmed with 1.12. If you want to get the nested hierarchy, you need to use the hostgroup_title parameter. Let me give you an example: If you have two hostgroups: CoreOS and k8s-master-lxsbx (the latter being a child of the former) you get the following:

"hostgroup_name":"k8s-master-lxsbx","hostgroup_title":"CoreOS/k8s-master-lxsbx"

The 1.10 instance I had when writing the inventory hat _name == _title. I double check tomorrow but I think we should just switch to

hostname_title

timogoebel commented 7 years ago

That makes sense, the new parameter was added in 1.12.0, see http://projects.theforeman.org/issues/14190

agx commented 7 years ago

@timogoebel Thanks for digging this out. I've added a PR that prefers title of name.

gbus commented 7 years ago

Please note that the release package download doesn't contain this fix. I had to clone from the repo to fix the issue, but a new release is probably needed.