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 164 forks source link

Parameters missing when creating a managed host if not present/inheritable from host group #743

Closed paulgration closed 4 years ago

paulgration commented 4 years ago
SUMMARY

Similar to #696 when creating a host in foreman (1.24.2) using the latest foreman-ansbile-modules (0.6.0) the host creation fails if all required parameters aren't present and some of these appear to be missing. With foreman-ansible-modules to add a managed host, the host needs to be part of a host group[1]. A host group created by the foreman web UI or foreman-ansible-modules can contain no settings other than a name, so when subsequently creating a host as part of this host group it doesn't inherit the following attributes and these aren't available in foreman_host of foreman-ansible-modules to specify them:

I believe it's reasonable to expect that multiple hosts in the same host group could have differing architectures/operating systems so it appears that both of these are missing from the foreman_host.

To make the host buildable, also requires:

So these are both missing from foreman_host too.

For info, adding the host to the same host group using the Foreman web UI also requires the Media, Partition Table and Root Password fields to be completed/valid (this is enforced by the web UI).

Generally there appears to be a bit of a mismatch between the required parameters for the foreman host creation API and the list of valid attributes in foreman-ansible-modules so the host creation attempt fails at the API with a 422 (Unprocessable Entity) HTTP response code when attributes are missing.

Foreman 1.24.2 API required attributes for host creation:

[1] This restriction doesn't seem to exist when using the Foreman web UI, it's possible to create a managed host without it being assigned to a host group (managed hosts shouldn't have to belong to a host group) and there are other, non-required, attributes missing in foreman-ansible-modules related to this.

ISSUE TYPE
ANSIBLE VERSION
ansible 2.9.6
KATELLO/FOREMAN VERSION
package tfm-rubygem-katello is not installed
foreman-1.24.2-1.el7.noarch
APYPIE VERSION
0.2.1
STEPS TO REPRODUCE
---
- name: "Foreman configuration"
  hosts: localhost
  connection: local
  become: false
  vars:
    username: API_USERNAME_HERE
    password: API_PASSWORD_HERE
    server_url: https://SERVER_ADDRESS
  module_defaults:
    theforeman.foreman.foreman_domain:
      username: '{{ username }}'
      password: '{{ password }}'
      server_url: '{{ server_url }}'
    theforeman.foreman.foreman_host:
      username: '{{ username }}'
      password: '{{ password }}'
      server_url: '{{ server_url }}'
    theforeman.foreman.foreman_hostgroup:
      username: '{{ username }}'
      password: '{{ password }}'
      server_url: '{{ server_url }}'
    theforeman.foreman.foreman_subnet:
      username: '{{ username }}'
      password: '{{ password }}'
      server_url: '{{ server_url }}'

  tasks:
    - name: Create domain
      theforeman.foreman.foreman_domain:
        name: example.com
        state: present

    - name: Create subnet
      theforeman.foreman.foreman_subnet:
        name: Example_subnet
        network: 192.168.1.0
        mask: 255.255.255.0
        domains:
        - example.com
        state: present

    - name: Create hostgroups
      theforeman.foreman.foreman_hostgroup:
        name: Example_hostgroup
        state: present

    - name: Create hosts
      theforeman.foreman.foreman_host:
        domain: example.com
        hostgroup: Example_hostgroup
        ip: 192.168.1.10
        location: Default Location
        mac: "005056a1b2c3"
        name: host.example.com
        organization: Default Organization
        state: present
        subnet: Example_subnet
EXPECTED RESULTS

Domain, subnet, host group and host expected to be created

ACTUAL RESULTS

PLAY [Foreman configuration] ************************************************************************

TASK [Gathering Facts] ******************************************************************************
ok: [localhost]

TASK [Create domain] ********************************************************************************
changed: [localhost]

TASK [Create subnet] ********************************************************************************
changed: [localhost]

TASK [Create hostgroups] ****************************************************************************
changed: [localhost]

TASK [Create hosts] *********************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "error": {"errors": {"architecture_id": ["can't be blank"], "operatingsystem_id": ["can't be blank"]}, "full_messages": ["Operatingsystem can't be blank", "Architecture can't be blank"], "id": null}, "msg": "Error while performing create on hosts: 422 Client Error: Unprocessable Entity for url: https://{SERVER_ADDRESS}/api/hosts"}

PLAY RECAP ******************************************************************************************
localhost                  : ok=4    changed=3    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

Domain, subnet, host group are created but the host isn't created, unless the Architecture and Operating system are updated using Foreman web UI (or API) and the playbook is run again - not possible via foreman-ansbile-modules alone. To make the host buildable, also requires setting the Media and Partition Table fields of the web UI.

paulgration commented 4 years ago

Related to #256 and #647

evgeni commented 4 years ago

Do the changes in https://github.com/theforeman/foreman-ansible-modules/pull/745 help you?

evgeni commented 4 years ago

we've merged that branch now, could you have a look if that's sufficient for your usecase or not?

paulgration commented 4 years ago

@evgeni this is great! I've tested the playbook in the original description above with the additional attributes (architecture, operatingsystem, media and ptable) and all seems to be working.

I haven't tried creating a host without assigning it to a host group as part of it's creation but it looks like everything is there now to support this?

evgeni commented 4 years ago

Yes, you should be able to create a host completely without a hostgroup now.

evgeni commented 4 years ago

Hey @pmgration wanted to check in if you had time to test without defining a hostgroup? I'd like to close this issue with "everything works for the user" ;)

paulgration commented 4 years ago

Hi @evgeni yeah I've been able to add hostgroups, and hosts assigned to those hostgroups - I haven't tried adding hosts that aren't assigned to a hostgroup yet but from our discussion and the changes in #745 that should all be possible too - I'll check that later today but happy to have the issue closed

paulgration commented 4 years ago

@evgeni I've tested creating a buildable host without it being assigned to a hostgroup so everything appears to be working as expected. Thanks!

evgeni commented 4 years ago

Perfect, closing as happy customer then ;)