Objective: Create an Ansible playbook that configures user accounts on target servers based on variables, and includes conditionals and loops.
---
- name: Configure Users
hosts: target_servers
become: true
vars:
users:
- name: alice
state: present
groups: developers
- name: bob
state: present
groups: administrators
tasks:
- name: Create or remove users
user:
name: "{{ item.name }}"
state: "{{ item.state }}"
groups: "{{ item.groups }}"
with_items: "{{ users }}"
Instructions for Participants:
- Review the provided
broken_deploy_users.yml
playbook. - Identify and fix the intentional errors in the playbook.
- Run the corrected playbook against your target servers.
ansible-playbook -i inventory.ini fixed_deploy_users.yml
- Verify that user accounts are configured as expected on the target servers.
Discussion Points:
- How are variables used to define user configurations in Ansible?
- What is the purpose of the
with_items
directive, and how does it relate to loops? - How are conditionals used in Ansible playbooks, and where do you find them in this exercise?
Common Errors:
- Incorrect indentation
- Typos in variable names
- Missing quotes around variable values
- Incorrect use of
with_items