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
146 stars 163 forks source link

ansible roles parameter breaks hostgroup module idempotency #1443

Open gardar opened 2 years ago

gardar commented 2 years ago
SUMMARY

Adding the ansible_roles parameter to the hostgroup module gives a changed status on each run which makes it no longer idempotent. And to add to the confusion if you register the output you will not see any changes in the diff, the before and after are exactly the same as the ansible roles aren't displayed in the diff output.

ISSUE TYPE
ANSIBLE VERSION
ansible 2.9.27
  config file = /etc/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
  executable location = /bin/ansible
  python version = 2.7.5 (default, Aug 13 2020, 02:51:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
COLLECTION VERSION
ansible-galaxy collection list isn't available on 2.9 but the collection version is 3.4.0
KATELLO/FOREMAN VERSION
tfm-rubygem-katello-4.1.1.57-1.el7sat.noarch
foreman-2.5.2.21-1.el7sat.noarch
STEPS TO REPRODUCE
- name: "Create hostgroups"
  theforeman.foreman.hostgroup:
    server_url: "{{ lookup('env', 'SATELLITE_SERVER_URL') }}"
    username: "{{ lookup('env', 'SATELLITE_USERNAME') }}"
    password: "{{ lookup('env', 'SATELLITE_PASSWORD') }}"
    validate_certs: "{{ lookup('env', 'SATELLITE_VALIDATE_CERTS') }}"
    name: "{{ item.name }}"
    ansible_roles: "{{ item.ansible_roles }}"
    description: "{{ item.description }}"
    subnet: "{{ item.subnet }}"
  delegate_to: localhost
  register: created
  loop: "{{ hostgroups }}"
  vars:
    hostgroups:
      - name: mygroup
        description: mydesc
        subnet: mysubnet
        ansible_roles:
          - role1
EXPECTED RESULTS
    "msg": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": true,
                "diff": {
                    "after": {
                        "hostgroups": [
                            {
                                "config_group_ids": [],
                                "description": "mydesc",
                                "group_parameters_attributes": [],
                                "id": 187,
                                "location_ids": [
                                    2
                                ],
                                "name": "mygroup",
                                "organization_ids": [
                                    3
                                ],
                                "parent_id": 186,
                                "puppetclass_ids": [],
                                "pxe_loader": "Grub2 UEFI",
                                "subnet_id": 20
                            }
                        ]
                    },
                    "before": {
                        "hostgroups": [
                            {
                                "config_group_ids": [],
                                "description": "mydesc",
                                "group_parameters_attributes": [],
                                "id": 187,
                                "location_ids": [
                                    2
                                ],
                                "name": "mygroup",
                                "organization_ids": [
                                    3
                                ],
                                "parent_id": 186,
                                "puppetclass_ids": [],
                                "pxe_loader": "Grub2 UEFI",
                                "subnet_id": 20
                            }
                        ]
                    }
                },
ACTUAL RESULTS
    "msg": {
        "changed": true,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": true,
                "diff": {
                    "after": {
                        "hostgroups": [
                            {
                                "config_group_ids": [],
                                "description": "mydesc",
                                "group_parameters_attributes": [],
                                "id": 187,
                                "location_ids": [
                                    2
                                ],
                                "name": "mygroup",
                                "organization_ids": [
                                    3
                                ],
                                "parent_id": 186,
                                "puppetclass_ids": [],
                                "pxe_loader": "Grub2 UEFI",
                                "subnet_id": 20
                            }
                        ]
                    },
                    "before": {
                        "hostgroups": [
                            {
                                "config_group_ids": [],
                                "description": "mydesc",
                                "group_parameters_attributes": [],
                                "id": 187,
                                "location_ids": [
                                    2
                                ],
                                "name": "mygroup",
                                "organization_ids": [
                                    3
                                ],
                                "parent_id": 186,
                                "puppetclass_ids": [],
                                "pxe_loader": "Grub2 UEFI",
                                "subnet_id": 20
                            }
                        ]
                    }
                },
gardar commented 2 years ago

As a dirty workaround it's possible to check the registered output and only mark the task as changed when there's difference in the diff outputs. This does however not completely fix the issue, as the ansible roles of the hostgroup are not printed in the diff output.

- name: "Create hostgroups"
  theforeman.foreman.hostgroup:
    server_url: "{{ lookup('env', 'SATELLITE_SERVER_URL') }}"
    username: "{{ lookup('env', 'SATELLITE_USERNAME') }}"
    password: "{{ lookup('env', 'SATELLITE_PASSWORD') }}"
    validate_certs: "{{ lookup('env', 'SATELLITE_VALIDATE_CERTS') }}"
    name: "{{ item.name }}"
    ansible_roles: "{{ item.ansible_roles }}"
    description: "{{ item.description }}"
    subnet: "{{ item.subnet }}"
  delegate_to: localhost
  register: created
  loop: "{{ hostgroups }}"
  vars:
    hostgroups:
      - name: mygroup
        description: mydesc
        subnet: mysubnet
        ansible_roles:
          - role1
  changed_when:
    - (created.diff.before.hostgroups[0].keys() | symmetric_difference(created.diff.after.hostgroups[0].keys()))
    - (created.diff.before.hostgroups[0].values() | symmetric_difference(created.diff.after.hostgroups[0].values()))