ansible-ipa-management/adhoc-ipagetkeytab.yml

139 lines
5.1 KiB
YAML
Raw Permalink Normal View History

2021-02-01 08:08:26 +00:00
---
# This playbook is meant to be used with callable variables, like adhoc or AWX.
# Special thanks to @remyabel for assisting in improving this playbook with
# extended security posture
# What: Pulls keytabs for a kerberos service
# What is expected:
2022-02-12 21:43:09 +00:00
# -> host: The host in the inventory
2021-02-01 08:08:26 +00:00
# -> ipa_service: using this format: SVC/hostname.rockylinux.org@ROCKYLINUX.ORG
# Note: This service MUST exist
# -> ipa_keytab_fullpath: The full path to the keytab. Example: /etc/gitlab/gitlab.keytab
# -> ipa_server: This needs to be one of the IPA servers
# -> ipa_owner: If applicable, the local account that can read this keytab (eg apache)
2024-02-13 05:35:41 +00:00
# -> ipaadmin_principal: The admin user that has kerberos management capabilities (default is admin)
2021-02-01 08:08:26 +00:00
# -> ipaadmin_password: This should be the password of the admin user
- name: Pull keytab from IPA
hosts: "{{ host|default('all') }}"
2021-02-01 08:08:26 +00:00
become: true
gather_facts: false
collections:
- freeipa.ansible_freeipa
2021-02-01 08:08:26 +00:00
tasks:
- name: "Checking for user variables"
2022-03-28 01:54:24 +00:00
ansible.builtin.assert:
2021-02-01 08:08:26 +00:00
that:
2024-02-13 05:35:41 +00:00
- ipaadmin_principal | mandatory
2021-02-01 08:08:26 +00:00
- ipaadmin_password | mandatory
- ipa_service | mandatory
- ipa_keytab_fullpath | mandatory
- ipa_server | mandatory
success_msg: "Required variables provided"
fail_msg: "We are missing required information"
- name: "Check that a keytab doesn't already exist"
2022-03-28 01:54:24 +00:00
ansible.builtin.stat:
2021-02-01 08:08:26 +00:00
path: "{{ ipa_keytab_fullpath }}"
register: keytab_status
check_mode: false
changed_when: "1 != 1"
- name: "Verify keytab existence"
2022-03-28 01:54:24 +00:00
ansible.builtin.assert:
2021-02-01 08:08:26 +00:00
that:
- "not keytab_status.stat.exists"
success_msg: "Keytab doesn't exist, moving on..."
fail_msg: "Keytab with that name already exists, skipping."
2024-02-13 05:35:41 +00:00
- name: "Grant {{ host }} and {{ ipaadmin_principal }} access to the service keytab"
2021-02-01 08:08:26 +00:00
delegate_to: "{{ ipa_server }}"
freeipa.ansible_freeipa.ipaservice:
2024-02-13 05:35:41 +00:00
ipaadmin_principal: "{{ ipaadmin_principal }}"
2021-02-01 08:08:26 +00:00
ipaadmin_password: "{{ ipaadmin_password }}"
name: "{{ ipa_service }}"
allow_retrieve_keytab_user:
2024-02-13 05:35:41 +00:00
- "{{ ipaadmin_principal }}"
2021-02-01 08:08:26 +00:00
allow_retrieve_keytab_host:
- "{{ host }}"
action: member
2024-02-13 05:35:41 +00:00
- name: "Grant {{ host }} and {{ ipaadmin_principal }} access to the host keytab"
2021-02-01 08:08:26 +00:00
delegate_to: "{{ ipa_server }}"
freeipa.ansible_freeipa.ipahost:
2024-02-13 05:35:41 +00:00
ipaadmin_principal: "{{ ipaadmin_principal }}"
2021-02-01 08:08:26 +00:00
ipaadmin_password: "{{ ipaadmin_password }}"
name: "{{ host }}"
state: present
allow_retrieve_keytab_user:
2024-02-13 05:35:41 +00:00
- "{{ ipaadmin_principal }}"
2021-02-01 08:08:26 +00:00
managedby_host: "{{ host }}"
action: member
- name: "Get kerberos ticket"
delegate_to: "{{ ipa_server }}"
2024-02-13 05:35:41 +00:00
ansible.builtin.shell: "set -o pipefail && echo \"{{ ipaadmin_password }}\" | kinit {{ ipaadmin_principal }}"
2021-02-01 08:08:26 +00:00
check_mode: false
changed_when: "1 != 1"
when: not keytab_status.stat.exists
- name: "Attempt to retrieve keytab"
delegate_to: "{{ ipa_server }}"
2022-03-28 01:54:24 +00:00
ansible.builtin.command: "ipa-getkeytab -r -s {{ ipa_server }} -p {{ ipa_service }} -k /tmp/{{ host }}.kt"
2021-02-01 08:08:26 +00:00
register: ret_result
check_mode: false
changed_when: "1 != 1"
failed_when: "not ('Keytab successfully retrieved' in ret_result.stderr or 'krbPrincipalKey not found' in ret_result.stderr)"
- name: "Create keytab if it didn't exist, based on the last task"
delegate_to: "{{ ipa_server }}"
2022-03-28 01:54:24 +00:00
ansible.builtin.command: "ipa-getkeytab -s {{ ipa_server }} -p {{ ipa_service }} -k /tmp/{{ host }}.kt"
2021-02-01 08:08:26 +00:00
when: "'krbPrincipalKey not found' in ret_result.stderr"
- name: "Destroy admin ticket"
delegate_to: "{{ ipa_server }}"
2022-03-28 01:54:24 +00:00
ansible.builtin.command: "kdestroy -A"
2021-02-01 08:08:26 +00:00
register: kdestroy_result
changed_when: "kdestroy_result.rc == 0"
- name: "Put the keytab into a register"
delegate_to: "{{ ipa_server }}"
2022-03-28 01:54:24 +00:00
ansible.builtin.command: "base64 /tmp/{{ host }}.kt"
2021-02-01 08:08:26 +00:00
register: keytab
check_mode: false
changed_when: "keytab.rc == 0"
- name: "Destroy local keytab"
delegate_to: "{{ ipa_server }}"
2022-03-28 01:54:24 +00:00
ansible.builtin.file:
2021-02-01 08:08:26 +00:00
path: "/tmp/{{ host }}.kt"
state: absent
- name: "Deploy keytab to {{ host }} from register"
2022-03-28 01:54:24 +00:00
ansible.builtin.copy:
2021-02-01 08:08:26 +00:00
dest: "{{ ipa_keytab_fullpath }}.b64"
content: "{{ keytab.stdout }}"
owner: "{{ ipa_owner|default('root') }}"
group: "{{ ipa_owner|default('root') }}"
mode: '0600'
- name: "Decode keytab"
2022-03-28 01:54:24 +00:00
ansible.builtin.shell: "umask 077 && base64 -d {{ ipa_keytab_fullpath }}.b64 > {{ ipa_keytab_fullpath }}"
2021-02-01 08:08:26 +00:00
changed_when: "1 != 1"
- name: "Destroy encoded keytab"
2022-03-28 01:54:24 +00:00
ansible.builtin.file:
2021-02-01 08:08:26 +00:00
path: "{{ ipa_keytab_fullpath }}.b64"
state: absent
- name: "Set ownership if applicable, otherwise it's root owned"
2022-03-28 01:54:24 +00:00
ansible.builtin.file:
2021-02-01 08:08:26 +00:00
path: "{{ ipa_keytab_fullpath }}"
owner: "{{ ipa_owner|default('root') }}"
group: "{{ ipa_owner|default('root') }}"
mode: '0600'
state: file
tags:
- keytab
2022-02-12 21:43:09 +00:00
...