Skip to content

Instantly share code, notes, and snippets.

@sayan3296
Created December 8, 2023 11:45
Show Gist options
  • Save sayan3296/6e6665a302a483b48d5367ec90c3f3cb to your computer and use it in GitHub Desktop.
Save sayan3296/6e6665a302a483b48d5367ec90c3f3cb to your computer and use it in GitHub Desktop.
register_to_satellite using Global Registration Method
---
# vim: ts=2 sw=2 ai expandtab
- name: Register to Satellite
  hosts: clients
  connection: smart
  gather_facts: true
  become: true
  vars_files:
    - satellite_credentials.yml
  vars:
    foreman_user: root
    satellite_org: 'RedHat'
    satellite_url: 'satellite.example.com'
    satellite_activation_key: 'RH8-AK'
  tasks:
    - assert:
        that:
          - ansible_distribution == "RedHat"
        fail_msg: "System can not be registered to Satellite"
        success_msg: "System will be registered"
    - name: test if system is registered
      command: bash -c "subscription-manager identity"
      register: registration
      failed_when: false
      changed_when:
        - registration.rc != 0
    - name: Find org and location id
      redhat.satellite.organization_info:
        username: "{{ satellite_username }}"
        password: "{{ satellite_password }}"
        server_url: "https://{{ satellite_url }}"
        name: "{{ satellite_org }}"
        validate_certs: false
      register: orgdata
      delegate_to: localhost
      when: registration is changed
      become: false
    - name: extract location id
      set_fact:
        locdata: "{{ orgdata.organization.locations | selectattr('name', 'eq', satellite_location) | last }}"
        orgdata: "{{ orgdata.organization }}"
      when: registration is changed
    - debug:
        var: tmp
      vars:
        tmp:
          locid: "{{ locdata.id }}"
          orgid: "{{ orgdata.id }}"
      when: registration is changed
    - assert:
        that:
          - locdata and orgdata
          - "'id' in locdata"
          - "'id' in orgdata"
          - locdata.id and orgdata.id
      when: registration is changed
    - name: extract registration script
      ansible.builtin.uri:
        url: "https://{{ satellite_url }}/api/registration_commands"
        return_content: true
        body_format: json
        user: "{{ satellite_username }}"
        password: "{{ satellite_password }}"
        method: POST
        force_basic_auth: true
        validate_certs: false
        body:
          location_id: "{{ locdata.id }}"
          organization_id: "{{ orgdata.id }}"
          registration_command:
            location_id: "{{ locdata.id }}"
            organization_id: "{{ orgdata.id }}"
            setup_insights: 0
            setup_remote_execution: 1
            jwt_expiration: 1
            insecure: 1
            activation_key: "{{ satellite_activation_key }}"
      register: regcmd
      delegate_to: localhost
      when: registration is changed
    - debug:
        var: regcmd.json.registration_command
      when: registration is changed
    - name: Write satellite registration
      ansible.builtin.copy:
        dest: /root/register.sh
        content: |
          #!/bin/bash
          {{ regcmd.json.registration_command }}
        mode: '0755'
        owner: root
        group: root
      when: registration is changed
    - name: run registration
      command: bash -c "/root/register.sh"
      when: registration is changed
    - name: add foreman sshkey to foreman_user
      ansible.posix.authorized_key:
        user: "{{ foreman_user }}"
        state: present
        key: "https://{{ satellite_url }}:9090/ssh/pubkey"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment