|
| 1 | +--- |
| 2 | + |
| 3 | +# Copyright 2021 Cloudera, Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# Find the current CDP username. |
| 18 | +- name: Find and set env_admin_username |
| 19 | + include_tasks: initialize.yml |
| 20 | + |
| 21 | +# If don't want to purge existing users from the FreeIPA group, |
| 22 | +# we'll query the group to get current users |
| 23 | +- name: Query sudoers group for list of users |
| 24 | + when: not sudoers__purge_users_in_group |
| 25 | + block: |
| 26 | + # Note - below creates the group if it doesn't already exist |
| 27 | + - name: Create or query the sudoers group |
| 28 | + community.general.ipa_group: |
| 29 | + ipa_user: "{{ sudoers__env_admin_username }}" |
| 30 | + ipa_pass: "{{ sudoers__env_admin_password }}" |
| 31 | + name: "{{ sudoers__sudo_group_name }}" |
| 32 | + state: present |
| 33 | + register: sudoers_group |
| 34 | + |
| 35 | + - name: Set facts for current user members of the group |
| 36 | + ansible.builtin.set_fact: |
| 37 | + __sudo_existing_group_users: "{{ sudoers_group.group.member_user | default([]) }}" |
| 38 | + |
| 39 | +# Final list of users to add to sudoers group - |
| 40 | +# either combined with existing group members or overrides |
| 41 | +- name: Create list of users to add to sudoers group |
| 42 | + ansible.builtin.set_fact: |
| 43 | + __sudo_group_users: "{{ sudoers__sudo_group_users | union(__sudo_existing_group_users) if not sudoers__purge_users_in_group else sudoers__sudo_group_users }}" |
| 44 | + |
| 45 | +# Create a FreeIPA group for sudo and add users |
| 46 | +- name: Add users to the sudoers group |
| 47 | + community.general.ipa_group: |
| 48 | + ipa_user: "{{ sudoers__env_admin_username }}" |
| 49 | + ipa_pass: "{{ sudoers__env_admin_password }}" |
| 50 | + name: "{{ sudoers__sudo_group_name }}" |
| 51 | + user: "{{ __sudo_group_users }}" |
| 52 | + state: present |
| 53 | + |
| 54 | +# Create FreeIPA sudo rule |
| 55 | +- name: Add sudo rule for passwordless sudo |
| 56 | + community.general.ipa_sudorule: |
| 57 | + ipa_user: "{{ sudoers__env_admin_username }}" |
| 58 | + ipa_pass: "{{ sudoers__env_admin_password }}" |
| 59 | + name: "{{ sudoers__sudorule_name }}" |
| 60 | + cmdcategory: all |
| 61 | + hostcategory: all |
| 62 | + sudoopt: "!authenticate" |
| 63 | + usergroup: |
| 64 | + - "{{ sudoers__sudo_group_name }}" |
| 65 | + state: present |
0 commit comments