theforeman / foreman-ansible-modules

Ansible modules for interacting with the Foreman API and various plugin APIs such as Katello
GNU General Public License v3.0
147 stars 163 forks source link

Adding puppet classes to nested hostgroup #1402

Open gvde opened 2 years ago

gvde commented 2 years ago
SUMMARY

Running against a katello 4.3 or 4.4 server my playbook doesn't set puppet classes for nested hostgroups.

    - role: theforeman.foreman.hostgroups
      vars:
        foreman_hostgroups:
...
          - name: "centos7-base"
            organization: "ORG"
            lifecycle_environment: "Production"
            content_view: "CentOS 7"
...
            puppetclasses:
              - "org_base"
              - "org_ipa"
          - name: "org_dns"
            parent: "centos7-base"
            puppetclasses:
              - "org_dns"

It creates the host group org_dns with centos7-base as parent and as expected almost everything is inherited from centos7-base. The group is inheriting the two classes org_base and org_ipa as well but it doesn't add org_dns

ANSIBLE VERSION
$ ansible --version
ansible 2.9.27
  config file = /home/k/k111111/git/foreman-ansible/ansible.cfg
  configured module search path = ['/home/k/k111111/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Nov 17 2021, 16:10:06) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
COLLECTION VERSION

Using latest version 3.3.0 from ansible-galaxy with some patches from pull requests.

KATELLO/FOREMAN VERSION
katello-4.4.0-1.el8.noarch
foreman-3.2.0-1.el8.noarch
STEPS TO REPRODUCE

See playbook above.

EXPECTED RESULTS

Add puppet classes to group.

gvde commented 2 years ago

O.K. I think I've found the reason. The problem is here: https://github.com/theforeman/foreman-ansible-modules/blob/770800e6a4ecf3092252936371f7ad03a19dd8d1/plugins/modules/hostgroup.py#L197-L198

It expects an environment_id. However, it's a nested hostgroup and it inherits the environment from the parent hostgroup. Due to that, environment_id is null:

$ curl -n https://foreman8.example.com/api/hostgroups/4 | python3 -m json.tool
...
    "parameters": [],
    "environment_id": null,
    "environment_name": "production",
    "inherited_environment_id": 1,
    "template_combinations": [],
...

I have double-checked: I have chose a specific environment in the hostgroup and during the next run it sets the additional puppetclass.

And during hostgroup creation it's the same: if I set an environment for the nested group it adds the additional puppetclasses. If I set no environment (i.e. it inherits it from the parent hostgroup) it doesn't work.