diff --git a/exercises/ansible_rhel/1.4-variables/README.md b/exercises/ansible_rhel/1.4-variables/README.md index c6f85ea42..a2a5a4881 100644 --- a/exercises/ansible_rhel/1.4-variables/README.md +++ b/exercises/ansible_rhel/1.4-variables/README.md @@ -23,6 +23,7 @@ Variables in Ansible are powerful tools for making your playbooks flexible and r ### Step 1 - Understanding Variables A variable in Ansible is a named representation of some data. Variables can contain simple values like strings and numbers, or more complex data like lists and dictionaries. + ### Step 2 - Variable Syntax and Creation The creation and usage of variables involve a specific syntax: @@ -31,9 +32,12 @@ The creation and usage of variables involve a specific syntax: * Starting with a letter or underscore. * Containing only letters, numbers, and underscores. 3. Using Variables: Variables are referenced in tasks using the double curly braces in quotes `"{{ variable_name }}"`. This syntax tells Ansible to replace it with the variable's value at runtime. + + Update the `system_setup.yml` playbook to include and use a variable: + ```yaml --- - name: Basic System Setup @@ -54,6 +58,7 @@ Update the `system_setup.yml` playbook to include and use a variable: state: present create_home: true ``` + Run this playbook with `ansible-navigator`. @@ -97,6 +102,8 @@ The register keyword in Ansible is used to capture the output of a task and save Update the `system_checks.yml` playbook: + + ```yaml --- - name: System Configuration Checks @@ -115,6 +122,7 @@ Update the `system_checks.yml` playbook: msg: "User {{ user_name }} exists." when: user_check.rc == 0 ``` + Playbook Details: diff --git a/exercises/ansible_rhel/1.5-handlers/README.md b/exercises/ansible_rhel/1.5-handlers/README.md index 19a474eb5..6d3744af3 100644 --- a/exercises/ansible_rhel/1.5-handlers/README.md +++ b/exercises/ansible_rhel/1.5-handlers/README.md @@ -35,6 +35,8 @@ Let's add to the system_setup.yml playbook the ability to install the Apache HTT > NOTE: Previous examples had hosts set to node1 but now it is set to all. This means when you run this updated Ansible playbook you will notice updates for the new systems being automated against, the user Roger created on all new systems and the Apache web server package httpd installed on all the hosts within the web group. + + ```yaml --- - name: Basic System Setup @@ -64,6 +66,8 @@ Let's add to the system_setup.yml playbook the ability to install the Apache HTT when: inventory_hostname in groups['web'] ``` + + In this example, `inventory_hostname in groups['web']` is the conditional statement. `inventory_hostname` refers to the name of the current host that Ansible is working on in the playbook. The condition checks if this host is part of the `web` group defined in your inventory file. If true, the task will execute and install Apache on that host. ### Step 3 - Handlers @@ -175,7 +179,7 @@ node3 : ok=8 changed=4 unreachable=0 failed=0 s Loops in Ansible allow you to perform a task multiple times with different values. This feature is particularly useful for tasks like creating multiple user accounts in our given example. In the original system_setup.yml playbook from Exercise 1.4, we had a task for creating a single user: - + ```yaml - name: Create a new user ansible.builtin.user: @@ -184,9 +188,11 @@ In the original system_setup.yml playbook from Exercise 1.4, we had a task for c create_home: true ``` + Now, let's modify this task to create multiple users using a loop: + ```yaml - name: Create a new user ansible.builtin.user: @@ -198,12 +204,15 @@ Now, let's modify this task to create multiple users using a loop: - bob - carol ``` + + What Changed? 1. Loop Directive: The loop keyword is used to iterate over a list of items. In this case, the list contains the names of users we want to create: alice, bob, and carol. 2. User Creation with Loop: Instead of creating a single user, the modified task now iterates over each item in the loop list. The `{{ item }}` placeholder is dynamically replaced with each username in the list, so the ansible.builtin.user module creates each user in turn. + When you run the updated playbook, this task is executed three times, once for each user specified in the loop. It's an efficient way to handle repetitive tasks with varying input data. diff --git a/exercises/ansible_rhel/1.6-templates/README.md b/exercises/ansible_rhel/1.6-templates/README.md index d66747077..afe88d540 100644 --- a/exercises/ansible_rhel/1.6-templates/README.md +++ b/exercises/ansible_rhel/1.6-templates/README.md @@ -25,7 +25,9 @@ Ansible leverages Jinja2, a widely-used templating language for Python, allowing ### Step 2 - Crafting Your First Template + Templates end with a `.j2` extension and mix static content with dynamic placeholders enclosed in `{{ }}`. + In the following example, let's create a template for the Message of the Day (MOTD) that includes dynamic host information. @@ -41,12 +43,16 @@ mkdir -p ~/lab_inventory/templates Create a file named `motd.j2` in the templates directory with the following content: + + ```jinja Welcome to {{ ansible_hostname }}. OS: {{ ansible_distribution }} {{ ansible_distribution_version }} Architecture: {{ ansible_architecture }} ``` + + This template dynamically displays the hostname, OS distribution, version, and architecture of each managed host. ### Step 3 - Deploying the Template with a Playbook diff --git a/exercises/ansible_rhel/1.7-role/README.md b/exercises/ansible_rhel/1.7-role/README.md index f83daa5f1..852594120 100644 --- a/exercises/ansible_rhel/1.7-role/README.md +++ b/exercises/ansible_rhel/1.7-role/README.md @@ -30,6 +30,8 @@ Building on our previous work with Apache configuration, let's craft an Ansible Run the following Ansible playbook to clean the environment: + + ```yaml --- - name: Cleanup Environment @@ -66,6 +68,8 @@ Run the following Ansible playbook to clean the environment: content: '' ``` + + ### Step 3 - Building the Apache Role We'll develop a role named `apache` to install, configure, and manage Apache. @@ -94,6 +98,8 @@ apache_service_name: httpd Adjust `/home/student/lab_inventory/roles/apache/tasks/main.yml` to include tasks for Apache installation and service management: + + ```yaml --- # tasks file for ansible-files/roles/apache @@ -128,6 +134,8 @@ Adjust `/home/student/lab_inventory/roles/apache/tasks/main.yml` to include task notify: Reload Firewall ``` + + 4. Implement Handlers: In `/home/student/lab_inventory/roles/apache/handlers/main.yml`, create a handler to restart firewalld if its configuration changes: @@ -145,6 +153,8 @@ In `/home/student/lab_inventory/roles/apache/handlers/main.yml`, create a handle Use a Jinja2 template for a custom `index.html`. Store the template in `templates/index.html.j2`: + + ```html @@ -156,6 +166,8 @@ Use a Jinja2 template for a custom `index.html`. Store the template in `template ``` + + 6. Update `tasks/main.yml` to deploy this `index.html` template: ```yaml diff --git a/exercises/ansible_rhel/1.8-troubleshoot/README.md b/exercises/ansible_rhel/1.8-troubleshoot/README.md index c00ccbeb5..5f6e855ec 100644 --- a/exercises/ansible_rhel/1.8-troubleshoot/README.md +++ b/exercises/ansible_rhel/1.8-troubleshoot/README.md @@ -34,6 +34,8 @@ In this example, add debug tasks to your Apache role in the `tasks/main.yml` to Insert debug tasks to display the values of variables or custom messages for troubleshooting: + + ```yaml - name: Display Variable Value ansible.builtin.debug: @@ -44,6 +46,8 @@ Insert debug tasks to display the values of variables or custom messages for tro msg: "Apache service name is {{ apache_service_name }}" ``` + + ### Step 3 - Error Handling with Blocks Ansible allows grouping tasks using `block` and handling errors with `rescue` sections, similar to try-catch in traditional programming. @@ -54,6 +58,8 @@ In this example, add a block to handle potential errors during the Apache config Wrap tasks that could potentially fail in a block and define a rescue section to handle errors: + + ```yaml - name: Apache Configuration with Potential Failure Point block: @@ -67,6 +73,8 @@ Wrap tasks that could potentially fail in a block and define a rescue section to msg: "Missing Apache configuration file '{{ apache_conf_src }}'. Using default settings." ``` + + 2. Add an `apache_conf_src` variable within `vars/main.yml` of the apache role. ```yaml diff --git a/exercises/ansible_rhel/README.md b/exercises/ansible_rhel/README.md index 9cb967944..5a1d10b45 100644 --- a/exercises/ansible_rhel/README.md +++ b/exercises/ansible_rhel/README.md @@ -55,8 +55,8 @@ Having said that, the exercises themselves should take roughly 4-5 hours. The fi ## Section 2 - Ansible Automation Platform Exercises -* [Exercise 2.1 - Introduction to automation controller](2.1-intro) -* [Exercise 2.2 - Inventories, credentials and ad hoc commands](2.2-cred) +* [Exercise 2.1 - Introduction to Ansible automation controller](2.1-intro) +* [Exercise 2.2 - Inventories and Credentials in Ansible Automation Controller](2.2-cred) * [Exercise 2.3 - Projects & job templates](2.3-projects) * [Exercise 2.4 - Surveys](2.4-surveys) * [Exercise 2.5 - Role based access control](2.5-rbac)