From d5408fe4d1b04644e39f97213c3eae391a3b35bc Mon Sep 17 00:00:00 2001 From: Webster Mudge Date: Thu, 31 Jul 2025 17:47:03 -0400 Subject: [PATCH] Update freeipa_client role Signed-off-by: Webster Mudge --- roles/freeipa_client/README.md | 63 +++- roles/freeipa_client/defaults/main.yml | 25 +- .../files/freeipa_dbus_session.conf | 22 ++ roles/freeipa_client/handlers/main.yml | 26 +- roles/freeipa_client/meta/argument_specs.yml | 66 ++++ roles/freeipa_client/meta/main.yml | 43 --- .../molecule/default/converge.yml | 36 ++ .../molecule/default/create.yml | 335 ++++++++++++++++++ .../molecule/default/destroy.yml | 156 ++++++++ .../molecule/default/molecule.yml | 61 ++++ .../molecule/default/prepare.yml | 89 +++++ .../molecule/default/requirements.yml | 21 ++ .../molecule/default/verify.yml | 49 +++ roles/freeipa_client/tasks/main.yml | 141 +++----- roles/freeipa_client/vars/main.yml | 16 + 15 files changed, 975 insertions(+), 174 deletions(-) create mode 100644 roles/freeipa_client/files/freeipa_dbus_session.conf create mode 100644 roles/freeipa_client/meta/argument_specs.yml delete mode 100644 roles/freeipa_client/meta/main.yml create mode 100644 roles/freeipa_client/molecule/default/converge.yml create mode 100644 roles/freeipa_client/molecule/default/create.yml create mode 100644 roles/freeipa_client/molecule/default/destroy.yml create mode 100644 roles/freeipa_client/molecule/default/molecule.yml create mode 100644 roles/freeipa_client/molecule/default/prepare.yml create mode 100644 roles/freeipa_client/molecule/default/requirements.yml create mode 100644 roles/freeipa_client/molecule/default/verify.yml create mode 100644 roles/freeipa_client/vars/main.yml diff --git a/roles/freeipa_client/README.md b/roles/freeipa_client/README.md index cc7df80e..3f54084e 100644 --- a/roles/freeipa_client/README.md +++ b/roles/freeipa_client/README.md @@ -1,17 +1,54 @@ - +## Requirements -# freeipa_client +None. + +## Dependencies + +- `freeipa.ansible_freeipa.ipaclient` + +## Examples + +```yaml +- name: Install FreeIPA client + ansible.builtin.import_role: + name: freeipa_client + vars: + ipaclient_domain: example.internal + ipaclient_realm: EXAMPLE.INTERNAL + ipaclient_servers: [ "freeipa.example.internal" ] + ipaclient_dns_servers: [ "10.0.0.4" ] + ipaadmin_password: "SomEpassWord" +``` + +## License + +``` +Copyright 2024 Cloudera, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +``` diff --git a/roles/freeipa_client/defaults/main.yml b/roles/freeipa_client/defaults/main.yml index 8d25c3c1..961b4797 100644 --- a/roles/freeipa_client/defaults/main.yml +++ b/roles/freeipa_client/defaults/main.yml @@ -1,12 +1,10 @@ ---- - -# Copyright 2023 Cloudera, Inc. All Rights Reserved. +# Copyright 2025 Cloudera, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, @@ -14,12 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -# ipaserver_domain: -# ipaserver_realm: -# ipa_hosts: # List of FQDN of IPA hosts -# ipa_server_ips: # List of IP of IPA hosts -# ipaadmin_principal: -# ipaadmin_password: +# ipaclient_packages: [] + +ipaclient_domain: "{{ undef(hint='Please define the DNS domain') }}" +ipaclient_realm: "{{ undef(hint='Please define the Kerberos realm') }}" + +ipaclient_servers: "{{ undef(hint='Please define the hostnames of the FreeIPA servers') }}" +ipaclient_dns_servers: "{{ undef(hint='Please define the IP addresses of the DNS servers') }}" + +ipaclient_sssd_enumeration: false -enable_dns: false -fallback_nameservers: ["8.8.8.8"] +ipaadmin_principal: admin +ipaadmin_password: "{{ undef(hint='Please define the FreeIPA adminstrator principal password') }}" diff --git a/roles/freeipa_client/files/freeipa_dbus_session.conf b/roles/freeipa_client/files/freeipa_dbus_session.conf new file mode 100644 index 00000000..87677bc0 --- /dev/null +++ b/roles/freeipa_client/files/freeipa_dbus_session.conf @@ -0,0 +1,22 @@ + + + + + 360000 + 360000 + diff --git a/roles/freeipa_client/handlers/main.yml b/roles/freeipa_client/handlers/main.yml index 2b0545c2..3e3b8fdc 100644 --- a/roles/freeipa_client/handlers/main.yml +++ b/roles/freeipa_client/handlers/main.yml @@ -1,11 +1,10 @@ ---- -# Copyright 2023 Cloudera, Inc. All Rights Reserved. +# Copyright 2025 Cloudera, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, @@ -13,7 +12,24 @@ # See the License for the specific language governing permissions and # limitations under the License. -- name: Restart network +- name: Restart host + ansible.builtin.reboot: + +- name: Restart SSSD + ansible.builtin.service: + name: "{{ sssd_service }}" + state: restarted + +# RHEL only +- name: Restart DBUS + ansible.builtin.systemd: + name: dbus + enabled: true + masked: false + state: restarted + +# RHEL only +- name: Restart logind ansible.builtin.service: - name: NetworkManager + name: systemd-logind state: restarted diff --git a/roles/freeipa_client/meta/argument_specs.yml b/roles/freeipa_client/meta/argument_specs.yml new file mode 100644 index 00000000..96dbaac1 --- /dev/null +++ b/roles/freeipa_client/meta/argument_specs.yml @@ -0,0 +1,66 @@ +# Copyright 2025 Cloudera, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +argument_specs: + main: + short_description: Set up FreeIPA client + description: + - Set up FreeIPA client, registering the host with a FreeIPA server, configured for DNS updates. + - Set up DNS search domain and DNS forwarders for name resolution. + - Install either defined or default FreeIPA client packages. + - Optionally, configure SSSD to enumerate users and groups. + author: Cloudera Labs + version_added: "5.0.0" + options: + ipaclient_packages: + description: + - List of FreeIPA packages to install. + - If not defined, the role will default to the packages defined in the + L(freeipa.ansible_freeipa.ipaclient,https://github.com/freeipa/ansible-freeipa/tree/master/roles/ipaclient) role. + type: list + elements: str + ipaclient_domain: + description: + - Domain name for DNS search and FreeIPA registration. + required: true + ipaclient_realm: + description: + - Realm for the Kerberos service. + required: true + ipaclient_servers: + description: + - List of hostnames of the FreeIPA servers. + type: list + elements: str + required: true + ipaclient_dns_servers: + description: + - List of IP address of the DNS servers to use for resolution. + - Typically, the IP addresses of the FreeIPA DNS servers. + type: list + elements: str + required: true + ipaclient_sssd_enumeration: + description: + - Flag to enable the C(enumeration) setting in the SSSD configuration file. + type: bool + default: false + ipaadmin_principal: + description: + - Principal of the FreeIPA user with elevated priviledges allowing host registration. + default: admin + ipaadmin_password: + description: + - Password of the FreeIPA user with elevated priviledges allowing host registration. + required: true diff --git a/roles/freeipa_client/meta/main.yml b/roles/freeipa_client/meta/main.yml deleted file mode 100644 index 3bbdfe57..00000000 --- a/roles/freeipa_client/meta/main.yml +++ /dev/null @@ -1,43 +0,0 @@ ---- - -# Copyright 2023 Cloudera, Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -galaxy_info: - role_name: freeipa_client - namespace: cloudera - author: Webster Mudge Jim Enright Chuck Levesque - description: > - Deployment of FreeIPA clients for Cloudera Data Platform (CDP) Base and ECS - company: Cloudera - license: Apache-2.0 - - min_ansible_version: 2.10 - - platforms: - - name: Debian - versions: all - - name: Fedora - versions: all - - name: GenericLinux - versions: all - - name: MacOSX - versions: all - - name: Ubuntu - versions: all - - galaxy_tags: - - cloudera - - cdp - - freeipa diff --git a/roles/freeipa_client/molecule/default/converge.yml b/roles/freeipa_client/molecule/default/converge.yml new file mode 100644 index 00000000..d2afd469 --- /dev/null +++ b/roles/freeipa_client/molecule/default/converge.yml @@ -0,0 +1,36 @@ +# Copyright 2024 Cloudera, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +- name: Converge + hosts: clients + gather_facts: true + become: true + tasks: + - name: Gather network details from the FreeIPA servers + ansible.builtin.setup: + gather_subset: default_ipv4 + delegate_to: "{{ __ipa_server }}" + delegate_facts: true + loop: "{{ groups['server'] }}" + loop_control: + loop_var: __ipa_server + + - name: Install FreeIPA client + ansible.builtin.import_role: + name: freeipa_client + vars: + ipaclient_domain: "{{ ipaserver_domain }}" + ipaclient_realm: "{{ ipaserver_realm }}" + ipaclient_servers: "{{ groups['server'] }}" # hostnames of FreeIPA servers + ipaclient_dns_servers: "{{ groups['server'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | list }}" diff --git a/roles/freeipa_client/molecule/default/create.yml b/roles/freeipa_client/molecule/default/create.yml new file mode 100644 index 00000000..132438bf --- /dev/null +++ b/roles/freeipa_client/molecule/default/create.yml @@ -0,0 +1,335 @@ +# Copyright 2024 Cloudera, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +- name: Create + hosts: localhost + connection: local + gather_facts: false + no_log: "{{ molecule_no_log }}" + vars: + # Run config handling + default_run_id: "{{ lookup('password', '/dev/null chars=ascii_lowercase length=5') }}" + default_run_config: + run_id: "{{ default_run_id }}" + + run_config_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/run-config.yml" + run_config_from_file: "{{ (lookup('file', run_config_path, errors='ignore') or '{}') | from_yaml }}" + run_config: '{{ default_run_config | combine(run_config_from_file) }}' + + # Platform settings handling + default_assign_public_ip: true + default_aws_profile: "{{ lookup('env', 'AWS_PROFILE') }}" + default_boot_wait_seconds: 120 + default_instance_type: t3a.medium + default_key_inject_method: cloud-init # valid values: [cloud-init, ec2] + default_key_name: "molecule-{{ run_config.run_id }}" + default_private_key_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/id_rsa" + default_public_key_path: "{{ default_private_key_path }}.pub" + default_ssh_user: ansible + default_ssh_port: 22 + default_user_data: '' + + default_security_group_name: "molecule-{{ run_config.run_id }}" + default_security_group_description: Ephemeral security group for Molecule instances + default_security_group_rules: + - proto: tcp + from_port: "{{ default_ssh_port }}" + to_port: "{{ default_ssh_port }}" + cidr_ip: "0.0.0.0/0" + - proto: icmp + from_port: 8 + to_port: -1 + cidr_ip: "0.0.0.0/0" + default_security_group_rules_egress: + - proto: -1 + from_port: 0 + to_port: 0 + cidr_ip: "0.0.0.0/0" + + platform_defaults: + assign_public_ip: "{{ default_assign_public_ip }}" + aws_profile: "{{ default_aws_profile }}" + boot_wait_seconds: "{{ default_boot_wait_seconds }}" + instance_type: "{{ default_instance_type }}" + key_inject_method: "{{ default_key_inject_method }}" + key_name: "{{ default_key_name }}" + private_key_path: "{{ default_private_key_path }}" + public_key_path: "{{ default_public_key_path }}" + security_group_name: "{{ default_security_group_name }}" + security_group_description: "{{ default_security_group_description }}" + security_group_rules: "{{ default_security_group_rules }}" + security_group_rules_egress: "{{ default_security_group_rules_egress }}" + ssh_user: "{{ default_ssh_user }}" + ssh_port: "{{ default_ssh_port }}" + cloud_config: {} + image: "" + image_name: "" + image_owner: [self] + name: "" + region: "" + security_groups: [] + tags: {} + volumes: [] + vpc_id: "" + vpc_subnet_id: "" + + # Merging defaults into a list of dicts is, it turns out, not straightforward + platforms: >- + {{ [platform_defaults | dict2items] + | product(molecule_yml.platforms | map('dict2items') | list) + | map('flatten', levels=1) + | list + | map('items2dict') + | list }} + pre_tasks: + - name: Validate platform configurations + ansible.builtin.assert: + that: + - platforms | length > 0 + - platform.name is string and platform.name | length > 0 + - platform.assign_public_ip is boolean + - platform.aws_profile is string + - platform.boot_wait_seconds is integer and platform.boot_wait_seconds >= 0 + - platform.cloud_config is mapping + - platform.image is string + - platform.image_name is string + - platform.image_owner is sequence or (platform.image_owner is string and platform.image_owner | length > 0) + - platform.instance_type is string and platform.instance_type | length > 0 + - platform.key_inject_method is in ["cloud-init", "ec2"] + - platform.key_name is string and platform.key_name | length > 0 + - platform.private_key_path is string and platform.private_key_path | length > 0 + - platform.public_key_path is string and platform.public_key_path | length > 0 + - platform.region is string + - platform.security_group_name is string and platform.security_group_name | length > 0 + - platform.security_group_description is string and platform.security_group_description | length > 0 + - platform.security_group_rules is sequence + - platform.security_group_rules_egress is sequence + - platform.security_groups is sequence + - platform.ssh_user is string and platform.ssh_user | length > 0 + - platform.ssh_port is integer and platform.ssh_port in range(1, 65536) + - platform.tags is mapping + - platform.volumes is sequence + - platform.vpc_id is string + - platform.vpc_subnet_id is string and platform.vpc_subnet_id | length > 0 + quiet: true + loop: '{{ platforms }}' + loop_control: + loop_var: platform + label: "{{ platform.name }}" + tasks: + - name: Write run config to file + ansible.builtin.copy: + dest: "{{ run_config_path }}" + content: "{{ run_config | to_yaml }}" + mode: "0600" + + - name: Generate local key pairs + community.crypto.openssh_keypair: + path: "{{ item.private_key_path }}" + type: rsa + size: 2048 + regenerate: never + loop: "{{ platforms }}" + loop_control: + label: "{{ item.name }}" + register: local_keypairs + + - name: Look up EC2 AMI(s) by owner and name (if image not set) + amazon.aws.ec2_ami_info: + owners: "{{ item.image_owner }}" + filters: "{{ item.image_filters | default({}) | combine(image_name_map) }}" + vars: + image_name_map: "{% if item.image_name is defined and item.image_name | length > 0 %}{{ {'name': item.image_name} }}{% else %}{}{% endif %}" + loop: "{{ platforms }}" + loop_control: + label: "{{ item.name }}" + when: not item.image + register: ami_info + + - name: Look up subnets to determine VPCs (if needed) + amazon.aws.ec2_vpc_subnet_info: + subnet_ids: "{{ item.vpc_subnet_id }}" + loop: "{{ platforms }}" + loop_control: + label: "{{ item.name }}" + when: not item.vpc_id + register: subnet_info + + - name: Validate discovered information + ansible.builtin.assert: + that: + - platform.image or (ami_info.results[index].images | length > 0) + - platform.vpc_id or (subnet_info.results[index].subnets | length > 0) + quiet: true + loop: "{{ platforms }}" + loop_control: + loop_var: platform + index_var: index + label: "{{ platform.name }}" + + - name: Create ephemeral EC2 keys (if needed) + amazon.aws.ec2_key: + profile: "{{ item.aws_profile | default(omit) }}" + region: "{{ item.region | default(omit) }}" + name: "{{ item.key_name }}" + key_material: "{{ local_keypair.public_key }}" + vars: + local_keypair: "{{ local_keypairs.results[index] }}" + loop: "{{ platforms }}" + loop_control: + index_var: index + label: "{{ item.name }}" + when: item.key_inject_method == "ec2" + register: ec2_keys + + - name: Create ephemeral security groups (if needed) + amazon.aws.ec2_security_group: + profile: "{{ item.aws_profile | default(omit) }}" + iam_instance_profile: "{{ item.iam_instance_profile | default(omit) }}" + region: "{{ item.region | default(omit) }}" + vpc_id: "{{ item.vpc_id or vpc_subnet.vpc_id }}" + name: "{{ item.security_group_name }}" + description: "{{ item.security_group_description }}" + rules: "{{ item.security_group_rules }}" + rules_egress: "{{ item.security_group_rules_egress }}" + vars: + vpc_subnet: "{{ subnet_info.results[index].subnets[0] }}" + loop: "{{ platforms }}" + loop_control: + index_var: index + label: "{{ item.name }}" + when: item.security_groups | length == 0 + + - name: Create ephemeral EC2 instance(s) + amazon.aws.ec2_instance: + profile: "{{ item.aws_profile | default(omit) }}" + region: "{{ item.region | default(omit) }}" + filters: "{{ platform_filters }}" + instance_type: "{{ item.instance_type }}" + image_id: "{{ platform_image_id }}" + vpc_subnet_id: "{{ item.vpc_subnet_id }}" + security_groups: "{{ platform_security_groups }}" + network: + assign_public_ip: "{{ item.assign_public_ip }}" + volumes: "{{ item.volumes }}" + key_name: "{{ (item.key_inject_method == 'ec2') | ternary(item.key_name, omit) }}" + tags: "{{ platform_tags }}" + user_data: "{{ platform_user_data }}" + state: "running" + wait: true + vars: + platform_security_groups: "{{ item.security_groups or [item.security_group_name] }}" + platform_generated_image_id: "{{ (ami_info.results[index].images | sort(attribute='creation_date', reverse=True))[0].image_id }}" + platform_image_id: "{{ item.image or platform_generated_image_id }}" + + platform_generated_cloud_config: + users: + - name: "{{ item.ssh_user }}" + ssh_authorized_keys: + - "{{ local_keypairs.results[index].public_key }}" + sudo: "ALL=(ALL) NOPASSWD:ALL" + platform_cloud_config: >- + {{ (item.key_inject_method == 'cloud-init') + | ternary((item.cloud_config | combine(platform_generated_cloud_config)), item.cloud_config) }} + platform_user_data: |- + #cloud-config + {{ platform_cloud_config | to_yaml }} + + platform_generated_tags: + instance: "{{ item.name }}" + "molecule-run-id": "{{ run_config.run_id }}" + platform_tags: "{{ (item.tags or {}) | combine(platform_generated_tags) }}" + platform_filter_keys: "{{ platform_generated_tags.keys() | map('regex_replace', '^(.+)$', 'tag:\\1') }}" + platform_filters: "{{ dict(platform_filter_keys | zip(platform_generated_tags.values())) }}" + loop: "{{ platforms }}" + loop_control: + index_var: index + label: "{{ item.name }}" + register: ec2_instances_async + async: 7200 + poll: 0 + + - name: Instance boot block + when: ec2_instances_async is changed + block: + - name: Wait for instance creation to complete + ansible.builtin.async_status: + jid: "{{ item.ansible_job_id }}" + loop: "{{ ec2_instances_async.results }}" + loop_control: + index_var: index + label: "{{ platforms[index].name }}" + register: ec2_instances + until: ec2_instances is finished + retries: 300 + + - name: Collect instance configs + ansible.builtin.set_fact: + instance_config: + instance: "{{ item.name }}" + address: "{{ item.assign_public_ip | ternary(instance.public_ip_address, instance.private_ip_address) }}" + user: "{{ item.ssh_user }}" + port: "{{ item.ssh_port }}" + identity_file: "{{ item.private_key_path }}" + instance_ids: + - "{{ instance.instance_id }}" + vars: + instance: "{{ ec2_instances.results[index].instances[0] }}" + loop: "{{ platforms }}" + loop_control: + index_var: index + label: "{{ item.name }}" + register: instance_configs + + - name: Write Molecule instance configs + ansible.builtin.copy: + dest: "{{ molecule_instance_config }}" + content: >- + {{ instance_configs.results + | map(attribute='ansible_facts.instance_config') + | list + | to_json + | from_json + | to_yaml }} + mode: "0600" + + - name: Start SSH pollers + ansible.builtin.wait_for: + host: "{{ item.address }}" + port: "{{ item.port }}" + search_regex: SSH + delay: 10 + timeout: 320 + loop: "{{ instance_configs.results | map(attribute='ansible_facts.instance_config') | list }}" + loop_control: + label: "{{ item.instance }}" + register: ssh_wait_async + async: 300 + poll: 0 + + - name: Wait for SSH + ansible.builtin.async_status: + jid: "{{ item.ansible_job_id }}" + loop: "{{ ssh_wait_async.results }}" + loop_control: + index_var: index + label: "{{ platforms[index].name }}" + register: ssh_wait + until: ssh_wait is finished + retries: 300 + delay: 1 + + - name: Wait for boot process to finish + ansible.builtin.pause: + seconds: "{{ platforms | map(attribute='boot_wait_seconds') | max }}" diff --git a/roles/freeipa_client/molecule/default/destroy.yml b/roles/freeipa_client/molecule/default/destroy.yml new file mode 100644 index 00000000..fb95a201 --- /dev/null +++ b/roles/freeipa_client/molecule/default/destroy.yml @@ -0,0 +1,156 @@ +# Copyright 2024 Cloudera, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +- name: Destroy + hosts: localhost + connection: local + gather_facts: false + no_log: "{{ molecule_no_log }}" + vars: + # Run config handling + default_run_id: "{{ lookup('password', '/dev/null chars=ascii_lowercase length=5') }}" + default_run_config: + run_id: "{{ default_run_id }}" + + run_config_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/run-config.yml" + run_config_from_file: "{{ (lookup('file', run_config_path, errors='ignore') or '{}') | from_yaml }}" + run_config: '{{ default_run_config | combine(run_config_from_file) }}' + + # Platform settings handling + default_aws_profile: "{{ lookup('env', 'AWS_PROFILE') }}" + default_key_inject_method: cloud-init # valid values: [cloud-init, ec2] + default_key_name: "molecule-{{ run_config.run_id }}" + default_security_group_name: "molecule-{{ run_config.run_id }}" + + platform_defaults: + aws_profile: "{{ default_aws_profile }}" + key_inject_method: "{{ default_key_inject_method }}" + key_name: "{{ default_key_name }}" + region: "" + security_group_name: "{{ default_security_group_name }}" + security_groups: [] + vpc_id: "" + vpc_subnet_id: "" + + # Merging defaults into a list of dicts is, it turns out, not straightforward + platforms: >- + {{ [platform_defaults | dict2items] + | product(molecule_yml.platforms | map('dict2items') | list) + | map('flatten', levels=1) + | list + | map('items2dict') + | list }} + + # Stored instance config + instance_config: "{{ (lookup('file', molecule_instance_config, errors='ignore') or '{}') | from_yaml }}" + pre_tasks: + - name: Validate platform configurations + ansible.builtin.assert: + that: + - platforms | length > 0 + - platform.name is string and platform.name | length > 0 + - platform.aws_profile is string + - platform.key_inject_method is in ["cloud-init", "ec2"] + - platform.key_name is string and platform.key_name | length > 0 + - platform.region is string + - platform.security_group_name is string and platform.security_group_name | length > 0 + - platform.security_groups is sequence + - platform.vpc_id is string + - platform.vpc_subnet_id is string and platform.vpc_subnet_id | length > 0 + quiet: true + loop: '{{ platforms }}' + loop_control: + loop_var: platform + label: "{{ platform.name }}" + tasks: + - name: Look up subnets to determine VPCs (if needed) + amazon.aws.ec2_vpc_subnet_info: + profile: "{{ item.aws_profile | default(omit) }}" + region: "{{ item.region | default(omit) }}" + subnet_ids: "{{ item.vpc_subnet_id }}" + loop: "{{ platforms }}" + loop_control: + label: "{{ item.name }}" + when: not item.vpc_id + register: subnet_info + + - name: Validate discovered information + ansible.builtin.assert: + that: platform.vpc_id or (subnet_info.results[index].subnets | length > 0) + quiet: true + loop: "{{ platforms }}" + loop_control: + loop_var: platform + index_var: index + label: "{{ platform.name }}" + + - name: Destroy resources + when: instance_config | length != 0 + block: + - name: Destroy ephemeral EC2 instances + amazon.aws.ec2_instance: + profile: "{{ item.aws_profile | default(omit) }}" + region: "{{ item.region | default(omit) }}" + instance_ids: "{{ instance_config | map(attribute='instance_ids') | flatten }}" + vpc_subnet_id: "{{ item.vpc_subnet_id }}" + state: absent + loop: "{{ platforms }}" + loop_control: + label: "{{ item.name }}" + register: ec2_instances_async + async: 7200 + poll: 0 + + - name: Wait for instance destruction to complete + ansible.builtin.async_status: + jid: "{{ item.ansible_job_id }}" + loop: "{{ ec2_instances_async.results }}" + loop_control: + index_var: index + label: "{{ platforms[index].name }}" + register: ec2_instances + until: ec2_instances is finished + retries: 300 + + - name: Destroy ephemeral security groups (if needed) + amazon.aws.ec2_security_group: + profile: "{{ item.aws_profile | default(omit) }}" + region: "{{ item.region | default(omit) }}" + vpc_id: "{{ item.vpc_id or vpc_subnet.vpc_id }}" + name: "{{ item.security_group_name }}" + state: absent + vars: + vpc_subnet: "{{ subnet_info.results[index].subnets[0] }}" + loop: "{{ platforms }}" + loop_control: + index_var: index + label: "{{ item.name }}" + when: item.security_groups | length == 0 + + - name: Destroy ephemeral keys (if needed) + amazon.aws.ec2_key: + profile: "{{ item.aws_profile | default(omit) }}" + region: "{{ item.region | default(omit) }}" + name: "{{ item.key_name }}" + state: absent + loop: "{{ platforms }}" + loop_control: + index_var: index + label: "{{ item.name }}" + when: item.key_inject_method == "ec2" + + - name: Write Molecule instance configs + ansible.builtin.copy: + dest: "{{ molecule_instance_config }}" + content: "{{ {} | to_yaml }}" diff --git a/roles/freeipa_client/molecule/default/molecule.yml b/roles/freeipa_client/molecule/default/molecule.yml new file mode 100644 index 00000000..873ce8da --- /dev/null +++ b/roles/freeipa_client/molecule/default/molecule.yml @@ -0,0 +1,61 @@ +# Copyright 2024 Cloudera, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Requires a preexisting AWS VPC and subnet, the latter referenced by the +# environment variable, TEST_VPC_SUBNET_ID + +# Requires AWS credentials, including region + +# Requires the following Python libraries on localhost +# - netattr +# - boto3 + +driver: + name: ec2 +platforms: + - name: freeipa.molecule.internal + image_owner: "309956199498" + image_name: RHEL-9.4.0_HVM-* + instance_type: t3.medium + boot_wait_seconds: 15 + vpc_subnet_id: ${TEST_VPC_SUBNET_ID} + tags: + Name: server.freeipa_client.molecule + Project: Molecule testing for freeipa_client + groups: + - server + security_groups: ${TEST_VPC_SECURITY_GROUP} + - name: rhel9-4.molecule.internal + image_owner: "309956199498" + image_name: RHEL-9.4.0_HVM-* + instance_type: t3.medium + boot_wait_seconds: 15 + vpc_subnet_id: ${TEST_VPC_SUBNET_ID} + tags: + Name: rhel9-4.freeipa_client.molecule + Project: Molecule testing for freeipa_client + groups: + - clients + security_groups: ${TEST_VPC_SECURITY_GROUP} +provisioner: + name: ansible + inventory: + group_vars: + all: + # ipaserver_forwarders and ipaserver_recursion_acl_cidr are set in prepare.yml + # due to runtime requirements + ipaserver_domain: molecule.internal + ipaserver_realm: MOLECULE.INTERNAL + ipadm_password: "freebird" + ipaadmin_password: "freebird" diff --git a/roles/freeipa_client/molecule/default/prepare.yml b/roles/freeipa_client/molecule/default/prepare.yml new file mode 100644 index 00000000..a9e66659 --- /dev/null +++ b/roles/freeipa_client/molecule/default/prepare.yml @@ -0,0 +1,89 @@ +# Copyright 2024 Cloudera, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +- name: Prepare server + hosts: server + gather_facts: yes + become: yes + tasks: + - name: Extract the VPC subnet ID from the Molecule platform configuration + ansible.builtin.set_fact: + test_subnet_id: "{{ molecule_yml.platforms | selectattr('name', 'eq', inventory_hostname) | map(attribute='vpc_subnet_id') | first }}" + + - name: Retrieve the VPC subnet details + amazon.aws.ec2_vpc_subnet_info: + subnet_id: "{{ test_subnet_id }}" + register: __subnet + become: no + delegate_to: localhost + + - name: Retrieve the VPC details + amazon.aws.ec2_vpc_net_info: + vpc_ids: "{{ __subnet.subnets | map(attribute='vpc_id') | first }}" + register: __vpc + become: no + delegate_to: localhost + + - name: Update the core networking + ansible.builtin.import_role: + name: prereq_network_dns + vars: + network_ip_address: "{{ ansible_default_ipv4.address }}" + network_dns_domain: "{{ ipaserver_domain }}" + # See https://docs.aws.amazon.com/vpc/latest/userguide/vpc-dns.html + vpc_cidr: "{{ __vpc.vpcs | map(attribute='cidr_block') | list }}" + network_dns_forwarders: "{{ vpc_cidr | map('ansible.utils.ipmath', '2') | list }}" + + - name: Install FreeIPA server + ansible.builtin.import_role: + name: freeipa_server + vars: + # See https://docs.aws.amazon.com/vpc/latest/userguide/vpc-dns.html + vpc_cidr: "{{ __vpc.vpcs | map(attribute='cidr_block') | list }}" + ipaserver_cidr: "{{ vpc_cidr }}" + ipaserver_forwarders: "{{ vpc_cidr | map('ansible.utils.ipmath', '2') | list }}" + ipaserver_recursion_acl_cidr: "{{ vpc_cidr }}" + +- name: Prepare clients + hosts: clients + gather_facts: yes + become: yes + tasks: + - name: Extract the VPC subnet ID from the Molecule platform configuration + ansible.builtin.set_fact: + test_subnet_id: "{{ molecule_yml.platforms | selectattr('name', 'eq', inventory_hostname) | map(attribute='vpc_subnet_id') | first }}" + + - name: Retrieve the VPC subnet details + amazon.aws.ec2_vpc_subnet_info: + subnet_id: "{{ test_subnet_id }}" + register: __subnet + become: no + delegate_to: localhost + + - name: Retrieve the VPC details + amazon.aws.ec2_vpc_net_info: + vpc_ids: "{{ __subnet.subnets | map(attribute='vpc_id') | first }}" + register: __vpc + become: no + delegate_to: localhost + + - name: Update the core networking + ansible.builtin.import_role: + name: prereq_network_dns + vars: + network_ip_address: "{{ ansible_default_ipv4.address }}" + network_dns_domain: "{{ ipaserver_domain }}" + # See https://docs.aws.amazon.com/vpc/latest/userguide/vpc-dns.html + vpc_cidr: "{{ __vpc.vpcs | map(attribute='cidr_block') | list }}" + network_dns_forwarders: "{{ groups['server'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | community.general.lists_union(vpc_cidr | map('ansible.utils.ipmath', '2') | list) }}" diff --git a/roles/freeipa_client/molecule/default/requirements.yml b/roles/freeipa_client/molecule/default/requirements.yml new file mode 100644 index 00000000..6464be94 --- /dev/null +++ b/roles/freeipa_client/molecule/default/requirements.yml @@ -0,0 +1,21 @@ +# Copyright 2024 Cloudera, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +collections: + - community.crypto + - amazon.aws + - ansible.posix + - ansible.utils + - community.general + - freeipa.ansible_freeipa diff --git a/roles/freeipa_client/molecule/default/verify.yml b/roles/freeipa_client/molecule/default/verify.yml new file mode 100644 index 00000000..fca60e78 --- /dev/null +++ b/roles/freeipa_client/molecule/default/verify.yml @@ -0,0 +1,49 @@ +# Copyright 2024 Cloudera, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +- name: Verify + hosts: all + gather_facts: true + become: true + tasks: + - name: Install supporting DNS packages + ansible.builtin.package: + name: "{{ item }}" + state: present + loop: + - bind-utils + + - name: Gather details for forward DNS + ansible.builtin.command: host {{ inventory_hostname }} + register: __forward + failed_when: __forward.stdout | string is not regex(ansible_default_ipv4.address) + changed_when: false + + - name: Gather details for reverse DNS + ansible.builtin.command: host {{ ansible_default_ipv4.address }} + register: __reverse + failed_when: __reverse.stdout | string is not regex(inventory_hostname) + changed_when: false + + - name: Execute kinit for admin + ansible.builtin.shell: "set -o pipefail; echo '{{ ipaadmin_password }}' | kinit admin" + register: __init + failed_when: __init.stdout | string is not regex("Password for admin@MOLECULE.INTERNAL") + changed_when: false + + - name: List Kerberos tickets + ansible.builtin.command: klist + register: __krb + failed_when: __krb.stdout | string is not regex("krbtgt/MOLECULE.INTERNAL@MOLECULE.INTERNAL") + changed_when: false diff --git a/roles/freeipa_client/tasks/main.yml b/roles/freeipa_client/tasks/main.yml index 5e64b3d4..ed238c12 100644 --- a/roles/freeipa_client/tasks/main.yml +++ b/roles/freeipa_client/tasks/main.yml @@ -1,11 +1,10 @@ ---- -# Copyright 2023 Cloudera, Inc. All Rights Reserved. +# Copyright 2024 Cloudera, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, @@ -13,113 +12,53 @@ # See the License for the specific language governing permissions and # limitations under the License. -- name: Permissive SELinux - ansible.posix.selinux: - policy: targeted - state: permissive +# - name: Configure host and DNS networking +# ansible.builtin.import_role: +# name: prereq_network_dns +# vars: +# network_ip_address: "{{ ipaclient_ip_address }}" +# network_dns_domain: "{{ ipaclient_domain }}" +# network_dns_forwarders: "{{ ipaclient_dns_servers }}" -- name: Set up DNS and networking - when: enable_dns - block: - - name: Update RHEL networking - when: ansible_facts['os_family'] == 'RedHat' - block: - - name: Check for existence of /etc/cloud/cloud.cfg - ansible.builtin.stat: - path: /etc/cloud/cloud.cfg - register: cloud_cfg +# TODO Should be a full reboot, not a dbus and logind restart +# - name: Update DBUS configuration (RHEL) +# when: ansible_os_family == 'RedHat' +# ansible.builtin.copy: +# src: freeipa_dbus_session.conf +# dest: /etc/dbus-1/session.d/freeipa.conf +# notify: +# - Restart DBUS +# # - Restart logind - - name: Set cloud-init to preserve hostname (RHEL) - ansible.builtin.lineinfile: - path: /etc/cloud/cloud.cfg - regex: "^(#)?preserve_hostname" - line: "preserve_hostname: 1" - state: present - when: cloud_cfg.stat.exists - notify: restart network - - - name: Set interface config to preserve resolv.conf changes (RHEL)' - ansible.builtin.lineinfile: - path: "/etc/sysconfig/network-scripts/ifcfg-{{ ansible_default_ipv4.interface }}" - regex: "^(#)?PEERDNS" - line: "PEERDNS=no" - state: present - notify: restart network - - - name: Set /etc/NetworkManager/conf.d/disable-resolve.conf-managing.conf (RHEL) - ansible.builtin.copy: - dest: /etc/NetworkManager/conf.d/disable-resolve.conf-managing.conf - content: | - # Generated by Ansible - [main] - dns=none - notify: restart network - - - name: Set /etc/resolv.conf directly - ansible.builtin.copy: - dest: /etc/resolv.conf - content: | - # Generated by Ansible - search {{ ipaserver_domain }} - {{ ['nameserver '] | product(ipa_server_ips | sort) | map('join') | join('\n') }} - notify: restart network - - - name: Disable nm-cloud-setup if present - ignore_errors: true - ansible.builtin.command: "{{ __nm_cloud_setup_disable_item }}" - loop_control: - loop_var: __nm_cloud_setup_disable_item - loop: - - systemctl disable nm-cloud-setup.service nm-cloud-setup.timer - - systemctl stop nm-cloud-setup.service nm-cloud-setup.timer - - rm -rf /etc/systemd/system/nm-cloud-setup.service.d - - - name: Set /etc/hostname to the FQDN - ansible.builtin.copy: - content: "{{ inventory_hostname }}" - dest: /etc/hostname - notify: restart network - - - name: Set /etc/hosts - ansible.builtin.copy: - dest: /etc/hosts - content: | - # Set by Ansible - {{ ansible_default_ipv4.address }} {{ inventory_hostname }} {{ inventory_hostname_short }} - notify: restart network - - - name: Check for existence of /etc/dhcp/dhclient.conf - ansible.builtin.stat: - path: /etc/dhcp/dhclient.conf - register: dhclient_conf - - - name: Set /etc/dhcp/dhclient.conf for domain search and name servers - ansible.builtin.lineinfile: - path: /etc/dhcp/dhclient.conf - regex: "^(#)?{{ dhclient_entry.value }}" - line: "{{ dhclient_entry.value }}" - state: present - loop: "{{ entries | dict2items }}" - loop_control: - loop_var: dhclient_entry - label: "{{ dhclient_entry.key }}" - vars: - entries: - domain_search: supersede domain-search "{{ ipaserver_domain }}"; - domain_name_servers: supersede domain-name-servers {{ ipa_server_ips | sort | union(fallback_nameservers) | join(', ') }}; - when: dhclient_conf.stat.exists - notify: restart network +- name: Set SSSD to enumerate users and groups + when: ipaclient_sssd_enumeration | bool + ansible.builtin.lineinfile: + path: "{{ sssd_config_path }}" + insertafter: "^\\[domain/.+\\]" + regexp: "^enumerate" + line: "enumerate = True" + notify: + - Restart SSSD - name: Flush handlers ansible.builtin.meta: flush_handlers -- name: Set up the FreeIPA Client +- name: Install defined FreeIPA client packages + when: ipaclient_packages is defined + ansible.builtin.package: + name: "{{ __ipa_package }}" + state: present + loop: "{{ ipaclient_packages }}" + loop_control: + loop_var: __ipa_package + +- name: Set up the FreeIPA Client via DNS discovery ansible.builtin.include_role: name: freeipa.ansible_freeipa.ipaclient vars: state: present ipaclient_hostname: "{{ inventory_hostname }}" - ipaclient_servers: "{{ ipa_hosts }}" - ipaserver_setup_dns: "{{ enable_dns }}" - ipasssd_enable_dns_updates: "{{ enable_dns }}" ipaclient_mkhomedir: true + ipaclient_install_packages: "{{ (ipaclient_packages is undefined) | ternary('true', 'false') }}" + ipaserver_setup_dns: true + ipasssd_enable_dns_updates: true diff --git a/roles/freeipa_client/vars/main.yml b/roles/freeipa_client/vars/main.yml new file mode 100644 index 00000000..a5d3cf3b --- /dev/null +++ b/roles/freeipa_client/vars/main.yml @@ -0,0 +1,16 @@ +# Copyright 2024 Cloudera, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +sssd_service: sssd +sssd_config_path: /etc/sssd/sssd.conf