This commit is contained in:
2020-08-17 12:04:20 -04:00
parent 3323922fd6
commit 9fa09f26bd
13 changed files with 1082 additions and 2 deletions

View File

@@ -0,0 +1,540 @@
---
- name: "Clear facts"
set_fact:
host_id: ""
location_id: ""
host_grp_id: ""
compute_resource_id: ""
compute_profile_id: ""
host_fqdn: ""
host_exists: ""
partition_table_id: ""
subnet_id: ""
domain_id: ""
failed_host: []
created_host: []
updated_host: []
- name: "Obtain Satellite6 Host Group ID"
block:
- name: "Obtain Satellite6 Host Group ID"
uri:
body: "{\"search\": 'title=\"{{ host.host_group }}\"'}"
vars:
sat6_url_path: "api/v2/hostgroups"
register: "host_grp_qry"
- debug:
var: "host_grp_qry.json.results[0]"
verbosity: "1"
- name: "Set Satellite6 Host Group ID"
set_fact:
host_grp_id: "{{ host_grp_qry.json.results[0].id }}"
when: "host_grp_qry.json.results[0].title == host.host_group"
- debug:
var: "host_grp_id"
verbosity: "1"
tags:
- "host_group_id"
- name: "Check if Satellite6 host exists"
block:
- name: "Set Satellite6 host fqdn | Domain Default from Host Group"
set_fact:
host_fqdn: "{{ host.name }}.{{ host_grp_qry.json.results[0].domain_name }}"
when:
- "not host.domain | default('')"
- "host_grp_qry.json.results[0].domain_name"
- name: "Set Satellite6 host fqdn | Domain Specified"
set_fact:
host_fqdn: "{{ host.name }}.{{ host.domain }}"
when: "host.domain | default('')"
- debug:
var: "host_fqdn"
verbosity: "1"
- name: "Check if Satellite6 host {{ host_fqdn }} exists"
uri:
body: "{\"search\": 'name=\"{{ host_fqdn }}\"'}"
vars:
sat6_url_path: "api/v2/hosts"
register: "host_qry"
- debug:
var: "host_qry.json.results[0]"
verbosity: "1"
- name: "Set Satellite6 Host {{ host_fqdn }} Exists | True"
set_fact:
host_exists: True
when:
- "host_qry.json.results | length > 0"
- "host_qry.json.results[0].name == host_fqdn"
- name: "Set Satellite6 Host {{ host_fqdn }} Exists | False"
set_fact:
host_exists: False
when:
- "host_qry.json.results | length < 1"
- name: "Set Satellite6 Host id"
set_fact:
host_id: "{{ host_qry.json.results[0].id }}"
when:
- "not sat6_fail_on_existing"
- "host.force_host_update | default(False)"
- "host.register_only | default(False)"
- "host_exists"
- debug:
var: "host_exists"
verbosity: "1"
- debug:
var: "host_id"
verbosity: "1"
when:
- "not sat6_fail_on_existing"
- "host.force_host_update | default(False)"
- "host.register_only | default(False)"
- "host_exists"
tags:
- "host_exists_check"
- name: "Obtain Satellite6 Domain ID"
block:
- name: "Obtain Satellite6 Domain ID"
uri:
body: "{\"search\": 'name=\"{{ host.domain }}\"'}"
vars:
sat6_url_path: "api/v2/domains"
register: "domain_qry"
- debug:
var: "domain_qry.json.results[0]"
verbosity: "1"
- name: "Set Satellite6 Domain ID"
set_fact:
domain_id: "{{ domain_qry.json.results[0].id }}"
when: "domain_qry.json.results[0].name == host.domain"
- debug:
var: "domain_id"
verbosity: "1"
when: (not host_exists and host.domain is defined) or
(host_exists and
host.force_host_update | default(False) and
host.domain is defined and
host.register_only | default(False) and
not sat6_fail_on_existing)
- name: "Obtain Satellite6 Location ID"
block:
- name: "Obtain Satellite6 Location ID"
uri:
body: "{\"search\": 'name=\"{{ host.location }}\"'}"
vars:
sat6_url_path: "api/v2/locations"
register: "loc_qry"
- debug:
var: "loc_qry.json.results[0]"
verbosity: "1"
- name: "Set Satellite6 Location ID"
set_fact:
location_id: "{{ loc_qry.json.results[0].id }}"
when: "loc_qry.json.results[0].name == host.location"
- debug:
var: "location_id"
verbosity: "1"
when: (not host_exists) or
(host_exists and host.force_host_update | default(False) and host.register_only | default(False) and not sat6_fail_on_existing)
tags:
- "location_id"
- name: "Obtain Satellite6 Compute Resource ID | Specified"
block:
- name: "Obtain Satellite6 Compute Resource ID"
uri:
body: "{\"search\": 'name=\"{{ host.compute_resource }}\"'}"
vars:
sat6_url_path: "api/v2/compute_resources"
register: "compute_resource_qry"
- debug:
var: "compute_resource_qry.json.results[0]"
verbosity: "1"
- name: Set Satellite6 Compute Resource ID
set_fact:
compute_resource_id: "{{ compute_resource_qry.json.results[0].id }}"
when: "compute_resource_qry.json.results[0].name == host.compute_resource"
- debug:
var: "compute_resource_id"
verbosity: "1"
when: (not host_exists and host.compute_resource is defined and host.mac is undefined) or
(host_exists and
host.force_host_update | default(False) and
host.compute_resource is defined and
host.mac is undefined and
host.register_only | default(False) and
not sat6_fail_on_existing)
tags:
- "compute_resource_id"
- name: "Obtain Satellite6 Compute Profile ID | Specified"
block:
- name: "Obtain Satellite6 Compute Profile ID"
uri:
body: "{\"search\": 'name=\"{{ host.compute_profile | truncate(20, True, '') }}\"'}"
vars:
sat6_url_path: "api/v2/compute_profiles"
register: "compute_profile_qry"
- debug:
var: "compute_profile_qry.json.results[0]"
verbosity: "1"
- name: "Set Satellite6 Compute Profile ID | Specified"
set_fact:
compute_profile_id: "{{ compute_profile_qry.json.results[0].id }}"
when: "compute_profile_qry.json.results[0].name == host.compute_profile"
- debug:
var: "compute_profile_id"
verbosity: "1"
when: (not host_exists and host.compute_profile is defined) or
(host_exists and host.force_host_update | default(False) and host.register_only | default(False) and host.compute_profile is defined)
tags:
- "compute_profile_id"
- name: "Obtain Satellite6 Compute Profile ID | Default from Host Group"
block:
- name: "Obtain Compute Profile"
uri:
body: "{\"search\": 'name=\"{{ host_grp_qry.json.results[0].compute_profile_name }}\"', \"per_page\": \"1000\"}"
vars:
sat6_url_path: "api/v2/compute_profiles"
register: "inherit_compute_profile_qry"
- debug:
var: "inherit_compute_profile_qry.json.results[0]"
verbosity: "1"
- name: "Set Compute Profile ID"
set_fact:
compute_profile_id: "{{ inherit_compute_profile_qry.json.results[0].id }}"
when:
- "not host_exists"
- "host.compute_profile is undefined"
- "host.mac is undefined"
- "not host.register_only | default(False)"
tags:
- "compute_profile_id"
- name: "Obtain Satellite6 Subnet ID"
block:
- name: "Obtain Satellite6 Subnet ID"
uri:
body: "{\"search\": 'name=\"{{ host.subnet }}\"'}"
vars:
sat6_url_path: "api/v2/subnets"
register: "subnet_qry"
- debug:
var: "subnet_qry.json.results[0]"
verbosity: "1"
- name: "Set Satellite6 Subnet ID"
set_fact:
subnet_id: "{{ subnet_qry.json.results[0].id }}"
when: "subnet_qry.json.results[0].name == host.subnet"
- debug:
var: "subnet_id"
verbosity: "1"
when: (not host_exists and host.subnet is defined) or
(host_exists and
host.force_host_update | default(False) and
host.register_only | default(False) and
host.subnet is defined and
not sat6_fail_on_existing)
tags:
- "subnet_id"
- name: "Obtain Satellite6 partition table ID"
block:
- name: "Obtain Satellite6 partition table ID"
uri:
body: "{\"search\": 'name=\"{{ host.partition_table }}\"'}"
vars:
sat6_url_path: "api/v2/ptables"
register: "partition_table_qry"
- debug:
var: "partition_table_qry.json.results[0]"
verbosity: "1"
- name: Set Satellite6 partition table ID
set_fact:
partition_table_id: "{{ partition_table_qry.json.results[0].id }}"
when: "partition_table_qry.json.results[0].name == host.partition_table"
- debug:
var: "partition_table_id"
verbosity: "1"
when: (not host_exists and host.partition_table is defined) or
(host_exists and
host.force_host_update | default(False) and
host.register_only | default(False) and
host.partition_table is defined and
not sat6_fail_on_existing)
tags:
- "partition_table_id"
# Virtual Host Creation and Updating
- name: "Create new Satellite 6 host"
block:
- name: "Create new Satellite 6 host {{ host_fqdn }} | Virtual"
block:
- name: "Create new Satellite 6 host {{ host_fqdn }} | Virtual"
uri:
method: "POST"
body: "{{ lookup('template', 'new_host_virtual.json.j2') }}"
status_code: "201"
timeout: 180
vars:
sat6_url_path: "api/v2/hosts"
register: "create_host_results"
- debug:
var: "create_host_results"
verbosity: "1"
- name: "Wait for host creation to complete"
pause:
seconds: "{{ sat6_power_on_delay | default('30') }}"
# - name: "Power on Satellite6 host {{ host_fqdn }}"
# uri:
# body: "{\"power_action\": \"start\"}"
# method: "PUT"
# vars:
# sat6_url_path: "api/v2/hosts/{{ create_host_results.json.id }}/power"
# register: "power_result"
# failed_when:
# - "'[Cannot run VM because the VM is in Up status.]' not in power_result.json.error.message"
# - debug:
# var: "power_result"
# verbosity: "1"
- set_fact:
created_host: ["{{ host_fqdn }}"]
# when:
# - "create_host_results.status == 201"
# - "power_result.status == 200"
when:
- "not host_exists"
- "compute_profile_id is defined"
- "compute_resource_id is defined"
- "domain_id is defined"
- "host.mac is undefined"
- "not host.register_only | default(False)"
- name: "Create new Satellite 6 host {{ host_fqdn }} | Virtual (register_only)"
block:
- name: "Create new Satellite 6 host {{ host_fqdn }} | Virtual (register_only)"
uri:
method: "POST"
body: "{{ lookup('template', 'new_host_virtual_register_only.json.j2') }}"
status_code: "201"
vars:
sat6_url_path: "api/v2/hosts"
register: "create_host_results"
- debug:
var: "create_host_results"
verbosity: "1"
- set_fact:
created_host: ["{{ host_fqdn }}"]
when:
- "create_host_results.status == 201"
when:
- "not host_exists"
- "host.mac is undefined"
- "domain_id is defined"
- "host.register_only | default(False)"
- name: "Update existing Satellite 6 host {{ host_fqdn }} | Virtual"
block:
- name: "Update existing Satellite 6 host {{ host_fqdn }} | Virtual"
uri:
method: "PUT"
body: "{{ lookup('template', 'new_host_virtual_register_only.json.j2') }}"
vars:
sat6_url_path: "api/v2/hosts/{{ host_id }}"
register: "update_host_results"
- debug:
var: "update_host_results"
verbosity: "1"
- set_fact:
updated_host: ["{{ host_fqdn }}"]
when:
- "update_host_results.status == 200"
when:
- "host_exists"
- "host.force_host_update | default(False)"
- "host.register_only | default(False)"
- "host.mac is undefined"
- "domain_id is defined"
- "not sat6_fail_on_existing"
# Bare-metal Host Creation and Updating
- name: "Create new Satellite 6 host {{ host_fqdn }} | Physical"
block:
- name: "Create new Satellite 6 host {{ host_fqdn }} | Physical"
uri:
method: "POST"
body: "{{ lookup('template', 'new_host_physical.json.j2') }}"
status_code: "201"
vars:
sat6_url_path: "api/v2/hosts"
register: "create_host_results"
- debug:
var: "create_host_results"
verbosity: "1"
- set_fact:
created_host: ["{{ host_fqdn }}"]
when: "create_host_results.status == 201"
when:
- "not host_exists"
- "host.mac is defined"
- "domain_id is defined"
- "host.compute_profile is undefined"
- "host.compute_resource is undefined"
- "not host.register_only | default(False)"
- name: "Create new Satellite 6 host {{ host_fqdn }} | Physical (register only)"
block:
- name: "Create new Satellite 6 host {{ host_fqdn }} | Physical (register only)"
uri:
method: "POST"
body: "{{ lookup('template', 'new_host_physical_register_only.json.j2') }}"
status_code: "201"
vars:
sat6_url_path: "api/v2/hosts"
register: "create_host_results"
- debug:
var: "create_host_results"
verbosity: "1"
- set_fact:
created_host: ["{{ host_fqdn }}"]
when: "create_host_results.status == 201"
when:
- "not host_exists"
- "host.mac is defined"
- "domain_id is defined"
- "host.compute_profile is undefined"
- "host.compute_resource is undefined"
- "host.register_only | default(False)"
- name: "Update existing Satellite 6 host {{ host_fqdn }} | Physical"
block:
- name: "Update existing Satellite 6 host {{ host_fqdn }} | Physical"
uri:
method: "PUT"
body: "{{ lookup('template', 'new_host_physical_register_only.json.j2') }}"
vars:
sat6_url_path: "api/v2/hosts/{{ host_id }}"
register: "update_host_results"
- debug:
var: "update_host_results"
verbosity: "1"
- set_fact:
updated_host: ["{{ host_fqdn }}"]
when: "update_host_results.status == 200"
when:
- "host_exists"
- "host.force_host_update | default(False)"
- "host.register_only | default(False)"
- "host.mac is defined"
- "domain_id is defined"
- "host.compute_profile is undefined"
- "host.compute_resource is undefined"
- "not sat6_fail_on_existing"
- name: "Build list of Created Hosts"
set_fact:
created_hosts: "{{ created_hosts | union(created_host) }}"
when: "created_host | length > 0"
- name: "Build list of Updated Hosts"
set_fact:
updated_hosts: "{{ updated_hosts | union(updated_host) }}"
when: "updated_host | length > 0"
when: (not host_exists) or
(host_exists and host.force_host_update | default(False) and host.register_only | default(False) and not sat6_fail_on_existing)
- name: "Handle existing Satellite6 hosts | Fail on existing"
block:
- debug:
msg: "{{ host_fqdn }} already exists. Not attempting to create."
- name: "Failed host"
set_fact:
failed_host: ["{{ host_fqdn }}"]
- name: "Build list of failed hosts | fail"
set_fact:
failed_hosts: "{{ failed_hosts | union(failed_host) }}"
fail_playbook: True
when: "sat6_fail_on_existing"
when:
- "host_exists"
- "sat6_fail_on_existing"
- "not host.force_host_update | default(False)"
- name: "Handle existing Satellite6 hosts | No fail"
block:
- debug:
msg: "{{ host_fqdn }} already exists. Not attempting to create."
when: "not host.force_host_update"
- name: "Failed host"
set_fact:
failed_host: ["{{ host_fqdn }}"]
when: "not host.force_host_update"
- name: "Build list of failed hosts | no fail"
set_fact:
failed_hosts: "{{ failed_hosts | union(failed_host) }}"
when: "not host.force_host_update"
- debug:
msg:
- "{{ host_fqdn }} already exists. Not attempting to create."
- "Updated {{ host_fqdn }}"
when:
- "host.force_host_update | default(False)"
- "host.register_only | default(False)"
when:
- "host_exists"
- "not sat6_fail_on_existing"

View File

@@ -0,0 +1,114 @@
---
- name: "Satellite6 Create Hosts"
block:
- name: "Ensure we have the Satellite Server's CA for validating authentication"
stat:
path: "/etc/pki/ca-trust/source/anchors/katello-server-ca.crt"
register: "ca_cert"
- name: "Obtain Satellite Server's CA for validating authentication"
block:
- name: "Download CA Certificate"
get_url:
url: "http://{{ sat6_fqdn }}/pub/katello-server-ca.crt"
dest: "/etc/pki/ca-trust/source/anchors/katello-server-ca.crt"
- name: "Update CA trust"
command: "update-ca-trust"
when: "not ca_cert.stat.exists"
become: True
- name: "Obtain Satellite6 Organization ID"
block:
- name: "Obtain Satellite6 Organization ID"
uri:
body: "{\"search\": 'name=\"{{ sat6_organization }}\"'}"
vars:
sat6_url_path: "api/v2/organizations"
register: "org_qry"
- debug:
var: "org_qry.json.results[0]"
verbosity: "1"
- name: "Set Satellite6 Organization ID"
set_fact:
organization_id: "{{ org_qry.json.results[0].id }}"
when: "org_qry.json.results[0].name == sat6_organization"
- debug:
var: "organization_id"
verbosity: "1"
tags:
- "organization_id"
- name: "Initialize lists"
set_fact:
failed_hosts: []
created_hosts: []
updated_hosts: []
- name: "Create new Satellite 6 Hosts"
include_tasks: "create_host.yml"
with_items: "{{ sat6_hosts }}"
loop_control:
loop_var: "host"
label: "{{ host.name }}"
when: "sat6_hosts | length > 0"
# Output summary/failure messages based on several conditions
- debug:
msg: "No hosts were created."
when: "created_hosts | length < 1"
- name: "Playbook fail"
fail:
msg:
- "List of hosts to provision missing."
- "Variable sat6_hosts list empty."
when: "sat6_hosts | length < 1"
- debug:
msg:
- "Host(s) created successfully:"
- "{{ created_hosts }}"
when: "created_hosts | length > 0"
# when we want to fail the playbook on existing sat6 hosts
- name: "Playbook fail when host exists"
fail:
msg:
- "Host(s) already exist in Satellite6:"
- "{{ failed_hosts }}"
when:
- "fail_playbook | default(False)"
- "sat6_fail_on_existing"
# when we don't want to fail the playbook on existing sat6 hosts
## Handles where we are using host.force_host_update: False
- debug:
msg:
- "Host(s) already exist in Satellite6:"
- "{{ failed_hosts }}"
when:
- "not sat6_fail_on_existing"
- "failed_hosts | length > 0"
## Handles where we are using host.force_host_update: True
- debug:
msg:
- "Host(s) already existed and were updated in Satellite6:"
- "{{ updated_hosts }}"
when:
- "not sat6_fail_on_existing"
- "updated_hosts | length > 0"
# set uri module defaults to avoid repetitive params
module_defaults:
uri:
user: "{{ sat6_user }}"
password: "{{ sat6_pass }}"
force_basic_auth: True
url: "https://{{ sat6_fqdn }}/{{ sat6_url_path | default('') }}"
method: "GET"
status_code: "200"
body_format: "json"