Add host via ansible

CMK: 2.2.0p22.cre
Alma Linux 8 and 9

Just starting with ansible and have what I believe is a good playbook to add a new host, install and setup the client and plugins, inventory that new host and activate the changes. Anyone see if I’ve gone wrong anywhere here?

(The two ansible.builtin.shell tasks run off some scripts that I use to get the agent installed and registered.)

---

- hosts: localhost
  gather_facts: false
  tasks:
    - name: Get and install agent rpm
      ansible.builtin.shell: /nfs_share/get_agent.sh
      delegate_to: client-hostname

    - name: Add client host 
      checkmk.general.host:
        server_url: "http://server-hostname"
        site: "sitename"
        automation_user: "ansible"
        automation_secret: "SECRET_STUFF"
        name: "client-hostname"
        folder: "/Linux/Server"
        state: "present"

    - name: "Register client for TLS"
      ansible.builtin.shell: echo "y" | /nfs_share/register.sh
      delegate_to: client-hostname

    - name: Inventory host
      checkmk.general.discovery:
        host_name: "client-hosname"
        server_url: "http://server-hostname/"
        site: "sitename"
        automation_user: "ansible"
        automation_secret: "SECRET_STUFF"
        state: tabula_rasa

    - name: Activate all changes HERE
      checkmk.general.activation:
        server_url: "http://server-hostname/"
        site: "sitename"
        automation_user: "ansible"
        automation_secret: "SECRET_STUFF"
        force_foreign_changes: 'true'
        sites:
          - "sitename"
      run_once: 'true'

Hi Steve,

the checkmk Agent role also contains playbooks for the installation and the tls registration of the agent.
These are very well made and maintained and are build for the most operating Systems, so I would tend to use that instead of your custom scripts.

Otherwise - If it works, it works :slight_smile:

1 Like

Checkmk maintains a collection on Ansible Galaxy that has everything already what you need: Ansible Galaxy

1 Like

I am working on figuring out the galaxy collection for this task but I still have some local scripts I need to get on a client so some of my first shot at doing this manually will still be useful.

Anybody got a playbook they can share? I’m apparently just not understanding how the checkmk ansible stuff is used. And I have zero experience using any collections in ansible, so far just been using the builtin stuff.

I’ve tried a few things but none have worked. I think I just need a couple of real world type examples to get over the initial hump.

I want to playbook adding a host to a folder on a site, install the agent on that host along with a few plugins and a couple of local plugins. Register the host for TLS, run discovery and activate all that. I’ll be probably bewriting out a couple of other files and making a couple config changes but I got those steps figured out in a playbook already.

Think I just figured out why nothing seems to be working for me. I recently updated check_mk to 2.3.0p3.cre and the ansible collection seems to only work with 2.2x versions.

Hey Steve! Good to read you again.

I think the best way would be to use the checkmk.general.agent role for initial installation. With that you get an installed agent including TLS registration. After that you can have at it with your preexisting scripts.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed. Contact an admin if you think this should be re-opened.