Provisioning discovered hosts: hostgroup settings not applied

Just chiming in as I’m dealing with the same issue of things not appearing, but, not using Ansible, but am using nested host groups. I guess I can try without them.

I will add that I can’t use the automated discovery because I need to manually set the hostname before kickstarting (we have things in our install process that register with AD using hostname during build) so we can’t change it afterwards. But, having a simple way to set hostname/group without doing the whole customization thing would be great.

You can still use automated discovery in that case, let me share the playbook I use to create a new machine:

If you want, you can replace most of the variables with static values if you don’t have an extensive Ansible setup. Or script these actions in a different way (you could also accomplish this using hammer on the commandline)

---
# This is an example playbook/workflow on how to add new VMs to the virtual infrastructure
#
# 0. Ensure we can talk to Proxmox
# 1. Create a new KVM on Proxmox and grab MAC address
# 2. Create a new Discovery Rule in Foreman
# 3. Boot the VM
# 4. Get coffee
# 5. Playbook will clean up Discovery Rule for you
#
# NOTE: Due to how Proxmox works, the hostname on the hypervisor is not FQDN, the one
# in Satellite is. It's recommended to use the Foreman inventory module instead of
# Proxmox in environments where Foreman is present
#
- name: 'Deploy new machine'
  hosts: 'config.rh.lab'
  gather_facts: false
  vars_prompt:
    - name: 'input_hostname'
      prompt: 'What is the hostname of the new machine?'
      default: 'servera.example.com'
      private: false
    - name: 'input_memory'
      prompt: 'How much RAM in GB?'
      default: 2
      private: false
    - name: 'input_hostgroup'
      prompt: 'Which hostgroup will the machine be in?'
      default: 'RedHat8-Base-Infra'
      private: false
  tasks:
    - name: 'Ensure Proxmoxer Python module'
      ansible.builtin.pip:
        name: 'proxmoxer'
        state: 'present'

    - name: 'Create new VM on Proxmox'
      community.general.proxmox_kvm:
        api_user: "{{ proxmox_user }}"
        api_password: "{{ proxmox_password }}"
        api_host: "{{ proxmox_host }}"
        validate_certs: "{{ proxmox_validate_certs }}"
        name: "{{ input_hostname }}"
        node: "{{ proxmox_node }}"
        boot: 'ndc'  # network, dvd, disk
        bootdisk: 'virtio0'
        bios: 'ovmf'
        efidisk0:
          storage: "{{ proxmox_storage }}"
          efitype: '4m'
          format: 'raw'
          pre_enrolled_keys: false
        cores: 2
        memory: "{{ input_memory * 1024 }}"
        net:
          net0: 'virtio,bridge=vmbr0'
        scsihw: 'virtio-scsi-pci'
        virtio:
          virtio0: "{{ proxmox_storage }}:40"
        ide:
          ide0: 'none,media=cdrom'
        state: 'present'
      register: 'vm_created'

    - name: 'Ensure Discovery Rule on Satellite'
      theforeman.foreman.discovery_rule:
        username: "{{ hostvars[rhsm_foreman_server]['foreman_admin_user'] }}"
        password: "{{ hostvars[rhsm_foreman_server]['foreman_admin_password'] }}"
        server_url: "{{ hostvars[rhsm_foreman_server]['foreman_url'] }}"
        validate_certs: "{{ hostvars[rhsm_foreman_server]['foreman_validate_certs'] }}"
        name: "ansible-autoinstall-{{ input_hostname }}"
        search: "mac = {{ vm_created['mac']['net0'] | lower }}"
        hostgroup: "{{ input_hostgroup }}"
        hostname: "{{ input_hostname }}"
        organizations:
            - "{{ hostvars[rhsm_foreman_server]['foreman_organization'] }}"
        locations: "{{ hostvars[rhsm_foreman_server]['foreman_locations'] }}"

    - name: 'Wait for VM settle'
      ansible.builtin.pause:
        seconds: 10

    - name: 'Start VM on Proxmox'
      community.general.proxmox_kvm:
        api_user: "{{ proxmox_user }}"
        api_password: "{{ proxmox_password }}"
        api_host: "{{ proxmox_host }}"
        validate_certs: "{{ proxmox_validate_certs }}"
        name: "{{ input_hostname }}"
        state: 'started'

    - name: 'Wait for VM installer to start'
      ansible.builtin.pause:
        minutes: 5

    - name: 'Remove Discovery Rule from Satellite'
      theforeman.foreman.discovery_rule:
        username: "{{ hostvars[rhsm_foreman_server]['foreman_admin_user'] }}"
        password: "{{ hostvars[rhsm_foreman_server]['foreman_admin_password'] }}"
        server_url: "{{ hostvars[rhsm_foreman_server]['foreman_url'] }}"
        validate_certs: "{{ hostvars[rhsm_foreman_server]['foreman_validate_certs'] }}"
        name: "ansible-autoinstall-{{ input_hostname }}"
        state: 'absent'

We don’t use Ansible at all, we’re all puppet based, and I can’t do it in puppet either as the software in question that needs the hostname to be set is required by our puppet configs.

Right, but the point I wanted to make was, you can still use discovery rules to create a rule that matches a VMs MAC address and assigns it the hostname you need. You could probably also whip up something with Puppet to do either the required hammer calls or via the REST API.

Or call Ansible from Puppet, the possibilities are endless :grin: