Este conteúdo não está disponível no idioma selecionado.
Chapter 21. Managing sudo access using an Ansible playbook
Define sudo rules using Ansible to control which Identity Management (IdM) users can run privileged commands on specific hosts in your domain.
21.1. Using an Ansible playbook to ensure sudo access for an IdM user on an IdM client Copiar o linkLink copiado para a área de transferência!
Use Ansible to ensure that sudo access to a specific command is granted to an Identity Management (IdM) user account on a specific IdM host.
Use the example below to create or update a sudo rule named idm_user_reboot. The rule grants idm_user the permission to run the /usr/sbin/reboot command on the idmclient machine.
Prerequisites
You have configured your Ansible control node to meet the following requirements:
- You are using Ansible version 2.15 or later.
-
You have installed the
ansible-freeipapackage. - The example assumes that in the ~/MyPlaybooks/ directory, you have created an Ansible inventory file with the fully-qualified domain name (FQDN) of the IdM server.
-
The example assumes that the secret.yml Ansible vault stores your
ipaadmin_passwordand that you have access to a file that stores the password protecting the secret.yml file.
-
The target node, that is the node on which the
freeipa.ansible_freeipamodule is executed, is part of the IdM domain as an IdM client, server or replica. - You have ensured the presence of a user account for idm_user in IdM and unlocked the account by creating a password for the user. For details on adding a new IdM user using the command line, see Adding users using the command line.
-
No local idm_user account exists on idmclient. The idm_user user is not listed in the
/etc/passwdfile on idmclient.
Procedure
Add one or more
sudocommands:Create an
ensure-reboot-sudocmd-is-present.ymlAnsible playbook that ensures the presence of the/usr/sbin/rebootcommand in the IdM database ofsudocommands. To simplify this step, you can copy and modify the example in the/usr/share/ansible/collections/ansible_collections/freeipa/ansible_freeipa/playbooks/sudocmd/ensure-sudocmd-is-present.ymlfile:--- - name: Playbook to manage sudo command hosts: ipaserver vars_files: - /home/user_name/MyPlaybooks/secret.yml tasks: # Ensure sudo command is present - freeipa.ansible_freeipa.ipasudocmd: ipaadmin_password: "{{ ipaadmin_password }}" name: /usr/sbin/reboot state: presentRun the playbook:
$ ansible-playbook --vault-password-file=password_file -v -i path_to_inventory_directory/inventory.file path_to_playbooks_directory/ensure-reboot-sudocmd-is-present.yml
Create a
sudorule that references the commands:Create an
ensure-sudorule-for-idmuser-on-idmclient-is-present.ymlAnsible playbook that uses thesudocommand entry to ensure the presence of a sudo rule. The sudo rule allows idm_user to reboot the idmclient machine. To simplify this step, you can copy and modify the example in the/usr/share/ansible/collections/ansible_collections/freeipa/ansible_freeipa/playbooks/sudorule/ensure-sudorule-is-present.ymlfile:--- - name: Tests hosts: ipaserver vars_files: - /home/user_name/MyPlaybooks/secret.yml tasks: # Ensure a sudorule is present granting idm_user the permission to run /usr/sbin/reboot on idmclient - freeipa.ansible_freeipa.ipasudorule: ipaadmin_password: "{{ ipaadmin_password }}" name: idm_user_reboot description: A test sudo rule. allow_sudocmd: /usr/sbin/reboot host: idmclient.idm.example.com user: idm_user state: presentRun the playbook:
$ ansible-playbook -v -i path_to_inventory_directory/inventory.file path_to_playbooks_directory/ensure-sudorule-for-idmuser-on-idmclient-is-present.yml
For details about all variables used in the playbook, see the
/usr/share/ansible/collections/ansible_collections/freeipa/ansible_freeipa/README-sudocmd.mdfile on the control node.
Verification
Test that the sudo rule whose presence you have ensured on the IdM server works on idmclient by verifying that idm_user can reboot idmclient using sudo. Note that it can take a few minutes for the changes made on the server to take effect on the client.
- Log in to idmclient as idm_user.
Reboot the machine using
sudo. Enter the password for idm_user when prompted:$ sudo /usr/sbin/reboot [sudo] password for idm_user:
If sudo is configured correctly, the machine reboots.
21.2. Managing multiple IdM sudo rules in a single Ansible task Copiar o linkLink copiado para a área de transferência!
Define multiple Identity Management (IdM) sudo rules in a single Ansible task using the sudorules batch option to simplify playbook structure and improve execution efficiency.
Using the services option available in the freeipa.ansible_freeipa.ipasudorule collection module, you can also specify multiple service variables that only apply to a particular service. Define this service by the name variable, which is the only mandatory variable for the services option.
Complete this procedure to ensure the presence of the HTTP/client01.idm.example.com@IDM.EXAMPLE.COM and the ftp/client02.idm.example.com@IDM.EXAMPLE.COM services in IdM with a single task.
Prerequisites
On the control node:
- You are using Ansible version 2.15 or later.
-
You have installed the
ansible-freeipapackage. - The example assumes that in the ~/MyPlaybooks/ directory, you have created an Ansible inventory file with the fully-qualified domain name (FQDN) of the IdM server.
-
The example assumes that the secret.yml Ansible vault stores your
ipaadmin_passwordand that you have access to a file that stores the password protecting the secret.yml file.
-
The target node, that is the node on which the
freeipa.ansible_freeipamodule is executed, is part of the IdM domain as an IdM client, server or replica.
The example further assumes the following:
- The user01 and user02 users exist in IdM.
- The usergroup01 user group exists in IdM.
- The hostgroup01 and hostgroup02 host groups exist in IdM.
Procedure
Navigate to the ~/MyPlaybooks/ directory:
$ cd ~/MyPlaybooks/Create an ensure-presence-of-multiple-sudorules-in-a-task.yml file with the following content:
--- - name: Playbook to handle sudorules hosts: ipaserver become: true tasks: # Ensure sudo command name: /usr/sbin/dmidecode is present - freeipa.ansible_freeipa.ipasudocmd: ipaadmin_password: "{{ ipaadmin_password }}" name: /usr/sbin/dmidecode # Ensure sudo command /usr/sbin/reboot is present - freeipa.ansible_freeipa.ipasudocmd: ipaadmin_password: "{{ ipaadmin_password }}" name: /usr/sbin/reboot # Ensure sudo command /usr/bin/yum is present - freeipa.ansible_freeipa.ipasudocmd: ipaadmin_password: "{{ ipaadmin_password }}" name: /usr/bin/yum # Ensure a sudo command group is present - freeipa.ansible_freeipa.ipasudocmdgroup: ipaadmin_password: "{{ ipaadmin_password }}" name: sudogroup01 sudocmd: - /usr/sbin/dmidecode - /usr/sbin/reboot - name: Ensure multiple sudo rules are present using batch mode freeipa.ansible_freeipa.ipasudorule: ipaadmin_password: "{{ ipaadmin_password }}" sudorules: - name: testrule01 user: - user01 - user02 group: - usergroup01 allow_sudocmd: - /usr/bin/yum allow_sudocmdgroup: - sudogroup01 - name: testrule02 hostgroup: - hostgroup01 - hostgroup02- NOTE
-
Using the
sudorulesoption, you can specify multiplesudorule variables that only apply to a particularsudorule. Thissudorule is defined by thenamevariable, which is the only mandatory variable for thesudorulesoption. In the example, theuser,group,allow_sudocmd, andallow_sudocmdgroupvariables are applied to the testrule01sudorule.
- Save the file.
Run the Ansible playbook. Specify the playbook file, the file storing the password protecting the secret.yml file, and the inventory file:
$ ansible-playbook --vault-password-file=password_file -v -i inventory ensure-presence-of-multiple-sudorules-in-a-task.yml