DO374
DO374
Travis Michette
Version 1.1
Table of Contents
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Repositories for this Course. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Demo Setup/Preparing to Teach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1. Developing Playbooks with Ansible Automation Platform 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1. Introducing Red Hat Ansible Automation Platform 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.1. Orientation to Red Hat Ansible Automation Platform 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.2. Red Hat Ansible Automation Platform 2 Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.2.1. Ansible Core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.2.2. Ansible Content Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.2.3. Ansible Content Navigator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.2.4. Ansible Execution Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.2.5. Automation Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1.2.6. Ansible Automation Hub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1.2.7. Hosted Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1.3. Red Hat Ansible Automation Platform 2 Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1.3.1. Developing Playbooks with Ansible Automation Platform 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2. Running Playbooks with Automation Content Navigator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2.1. Introducing Automation Content Navigator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2.1.1. Improving Portability with Automation Execution Environments. . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2.2. Installing Automation Content Navigator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2.3. Configuring Authentication to Managed Hosts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2.3.1. Preparing SSH Key-Based Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2.3.2. Providing Private Keys to the Automation Execution Environment . . . . . . . . . . . . . . . . . . . . . . 8
1.2.4. Running Automation Content Navigator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2.4.1. Ansible Ad-Hoc Commands Using Content Navigator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.2.4.2. Running Playbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2.4.3. Reviewing Previous Playbook Runs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2.4.4. Reading Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2.4.5. Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3. Demo - Ansible Ad-Hoc Commands Using Content Navigator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4. Demo - Ansible Content Navigator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.5. Managing Ansible Project Materials Using Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.5.1. Defining Infrastructure as Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.5.2. Introducing Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.5.3. Describing Initial Git Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.5.4. Starting the Git Workflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.5.4.1. Examining the Git Log. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.5.5. Working with Branches and References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.5.5.1. Creating Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.5.5.2. Merging Branches. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.5.5.3. Creating Branches from Old Commits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.5.5.4. Pushing Branches to Remote Repositories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.5.6. Structuring Ansible Projects in Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.5.6.1. Roles and Ansible Content Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.5.6.2. Configuring Git to Ignore Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.6. Demo - Using Git. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.7. Demo -Github Tools (gh) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.8. Implementing Recommended Ansible Practices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
1.8.1. The Effectiveness of Ansible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
1.8.2. Keeping Things Simple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
1.8.2.1. Keeping Your Playbooks Readable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
1.8.2.2. Use Existing Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
1.8.2.3. Adhering to a Standard Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
1.8.3. Staying Organized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
1.8.3.1. Following Conventions for Naming Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1.8.3.2. Standardizing the Project Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1.8.3.3. Using Dynamic Inventories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1.8.3.4. Taking Advantage of Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.8.3.5. Using Roles and Ansible Content Collections for Reusable Content . . . . . . . . . . . . . . . . . . . . 33
1.8.3.6. Running Playbooks Centrally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.8.3.7. Building Automation Execution Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.8.4. Testing Often . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.8.4.1. Testing the Results of Tasks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.8.4.2. Using Block/Rescue to Recover or Rollback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
1.8.4.3. Developing Playbooks with the Latest Ansible Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
1.8.4.4. Using Test Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2. Managing Content Collections and Execution Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.1. Reusing Content from Ansible Content Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.1.1. Defining Ansible Content Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.1.1.1. Organizing Ansible Content Collections in Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.1.2. Using Ansible Content Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.1.2.1. Accessing Ansible Content Collection Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.1.2.2. Using Ansible Content Collections in Playbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.1.2.3. Finding Ansible Content Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.1.2.4. Using the Built-in Ansible Content Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.2. Demo - Using Ansible Content Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.3. Finding and Installing Ansible Content Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.3.1. Sources for Ansible Content Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.3.1.1. Finding Collections on Ansible Automation Hub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.3.2. Installing Ansible Content Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.3.2.1. Installing Collections from the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.3.2.2. Installing Collections with a Requirements File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.3.2.3. Listing Installed Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.3.3. Configuring Collection Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.3.3.1. Installing Collections from Ansible Automation Hub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.3.3.2. Installing Collections from Private Automation Hub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.4. Selecting an Execution Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.4.1. Describing Automation Execution Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.4.2. Selecting a Supported Automation Execution Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.4.3. Inspecting Automation Execution Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.4.4. Using Automation Execution Environments with Ansible Content Navigator . . . . . . . . . . . . . . . . . 49
3. Running Playbooks with Automation Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.1. Explaining the Automation Controller Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.1.1. Introduction to Automation Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.1.2. Describing the Architecture of Automation Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.1.3. Automation Controller Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.2. Demo - Setting Up Automation Controller. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.3. Running Playbooks in Automation Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.3.1. Exploring Resources in Automation Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.3.2. Creating Credential Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.3.2.1. Listing Credentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.3.2.2. Creating a Machine Credential . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.3.2.3. Creating a Source Control Credential . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.3.3. Creating Project Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.3.4. Creating Inventory Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.3.4.1. Manually Creating Groups and Hosts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.3.4.2. Populating Groups and Hosts Using a Project Inventory File. . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.3.5. Creating Job Template Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.3.6. Launching and Reviewing Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.4. Demo - Running a Playbook from Automation Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4. Working with Ansible Configuration Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.1. Examining Ansible Configuration with Automation Content Navigator . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.1.1. Inspecting Configuration in Interactive Mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.1.1.1. Searching for Specific Configuration Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.1.1.2. Accessing Parameter Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.1.1.3. Inspecting Local Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.1.2. Inspecting Ansible Configuration in Standard Output Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.2. Configuring Automation Content Navigator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4.2.1. Format of the Settings File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4.2.2. Locating the Settings File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4.2.2.1. Selecting a Settings File to Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4.2.3. Editing the Settings File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4.2.3.1. Setting a Default Automation Execution Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4.2.3.2. Default to Running in Standard Output Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4.2.3.3. Disabling Playbook Artifacts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4.2.3.4. Overview of an Example Settings File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
5. Managing Inventories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.1. Managing Dynamic Inventories. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.1.1. Generating Inventories Dynamically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.1.2. Discussing Inventory Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.1.2.1. Using Inventory Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.1.3. Developing Inventory Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.1.3.1. Using Inventory Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.1.4. Managing Multiple Inventories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.2. Writing YAML Inventory Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.2.1. Discussing Inventory Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.2.2. Writing YAML Static Inventory Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.2.2.1. Setting Inventory Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.2.3. Converting a Static Inventory File in INI Format to YAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.2.4. Troubleshooting YAML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.2.4.1. Protecting a Colon Followed by a Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.2.4.2. Protecting a Variable that Starts a Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.2.4.3. Knowing the Difference Between a String and a Boolean or Float. . . . . . . . . . . . . . . . . . . . . . 57
5.3. Managing Inventory Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.3.1. Describing the Basic Principles of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.3.2. Variable Merging and Precedence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.3.2.1. Determining Command-line Option Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.3.2.2. Determining Role Default Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.3.2.3. Determining Host and Group Variable Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.3.2.4. Determining Play Variable Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.3.2.5. Determining the Precedence of Extra Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.3.3. Separating Variables from Inventory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.3.4. Using Special Inventory Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.3.4.1. Configuring Human Readable Inventory Host Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
5.3.5. Identifying the Current Host Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
6. Managing Task Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6.1. Controlling Privilege Escalation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6.1.1. Privilege Escalation Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6.1.1.1. Privilege Escalation by Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6.1.1.2. Defining Privilege Escalation in Plays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6.1.1.3. Privilege Escalation in Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6.1.1.4. Grouping Privilege Escalation Tasks with Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6.1.1.5. Applying Privilege Escalation in Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6.1.1.6. Listing Privilege Escalation with Connection Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6.2. Choosing Privilege Escalation Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6.3. Controlling Privilege Escalation (DEMO) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
6.4. Controlling Task Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
6.4.1. Controlling the Order of Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
6.4.1.1. Importing or Including Roles as a Task. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
6.4.1.2. Defining Pre- and Post-tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
6.4.1.3. Reviewing the Order of Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
6.4.2. Listening to Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
6.4.2.1. Notifying Handlers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
6.4.3. Controlling the Order of Host Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
6.5. Running Selected Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
6.5.1. Tagging Ansible Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
6.5.2. Managing Tagged Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
6.5.2.1. Running Tasks with Specific Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
6.5.2.2. Combining Tags to Run Multiple Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
6.5.2.3. Skipping Tasks with Specific Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
6.5.2.4. Listing Tags in a Playbook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
6.5.3. Assigning Special Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
6.6. Controlling Tasks with Tags (DEMO) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
6.7. Optimizing Execution for Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.7.1. Optimizing Playbook Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.7.1.1. Optimizing the Infrastructure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.7.1.2. Disabling Fact Gathering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.7.1.3. Reusing Gathered Facts with Fact Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.7.1.4. Limiting Fact Gathering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.7.1.5. Increasing Parallelism. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.7.1.6. Avoiding Loops with the Package Manager Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.7.1.7. Efficiently Copying Files to Managed Hosts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.7.1.8. Using Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.7.1.9. Enabling Pipelining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.7.2. Profiling Playbook Execution with Callback Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.7.2.1. Timing Tasks and Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
7. Transforming Data with Filters and Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1. Processing Variables Using Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.1. Ansible Filters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.2. Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.3. Manipulating Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.3.1. Extracting list elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.3.2. Modifying the Order of List Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.3.3. Merging Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.3.4. Operating on Lists as Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.4. Manipulating Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.4.1. Joining dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.4.2. Converting Dictionaries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.5. Hashing, Encoding, and Manipulating Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.5.1. Hashing strings and passwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.5.2. Encoding strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.5.3. Formatting Text. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.5.4. Replacing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.6. Manipulating JSON Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.6.1. JSON Queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.6.2. Parsing and Encoding Data Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.2. Demo - JSON Queries on Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
7.3. Templating External Data using Lookups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
7.3.1. Lookup Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
7.3.2. Calling Lookup Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
7.3.3. Selecting Lookup Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
7.3.3.1. Reading the Contents of Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
7.3.3.2. Applying Data with a Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
7.3.3.3. Reading Command Output in the Execution Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
7.3.3.4. Getting Content from a URL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
7.3.3.5. Getting Information from the Kubernetes API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
7.3.3.6. Using Custom Lookup Plug-ins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
7.3.4. Handling Lookup Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
7.4. Implementing Advanced Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
7.4.1. Comparing Loops and Lookup Plug-ins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
7.4.2. Example Iteration Scenarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
7.4.2.1. Iterating over a List of Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
7.4.2.2. Iterating Over Nested Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
7.4.2.3. Iterating Over a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
7.4.2.4. Iterating Over a File Globbing Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
7.4.2.5. Retrying a Task. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
7.5. Using Filters to Work with Network Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
7.5.1. Gathering and Processing Networking Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
7.5.2. Network Information Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
7.5.2.1. Testing IP Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
7.5.2.2. Filtering Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
7.5.2.3. Manipulating IP Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
7.5.2.4. Reformatting or Calculating Network Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
8. Coordinating Rolling Updates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
8.1. Delegating Tasks and Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
8.1.1. Delegating Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
8.1.1.1. Delegating to localhost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
8.1.2. Delegating Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
8.2. Configuring Parallelism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
8.2.1. Configure Parallelism in Ansible Using Forks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
8.2.2. Running Batches of Hosts Through the Entire Play. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
8.3. Managing Rolling Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
8.3.1. Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
8.3.2. Controlling Batch Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
8.3.2.1. Setting a Fixed Batch Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
8.3.2.2. Setting Batch Size as a Percentage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
8.3.2.3. Setting Batch Sizes to Change During the Play . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
8.3.3. Aborting the Play . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
8.3.3.1. Specifying Failure Tolerance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
8.3.4. Running a Task Once . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
9. Creating Content Collections and Execution Environments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
9.1. Writing Ansible Content Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
9.1.1. Developing Ansible Content Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
9.1.1.1. Selecting a Namespace for Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
9.1.1.2. Creating Collection Skeletons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
9.1.1.3. Adding Content to Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
9.1.1.4. Updating Collection Metadata. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
9.1.1.5. Declaring Collection Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
9.1.1.6. Building Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
9.1.1.7. Validating and Testing Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
9.1.2. Publishing Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
9.2. Building a Custom Execution Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
9.2.1. Deciding When to Create a Custom Automation Execution Environment . . . . . . . . . . . . . . . . . . . . 79
9.2.2. Preparing for a New Automation Execution Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
9.2.2.1. Declaring the Ansible Content Collections to Install . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
9.2.2.2. Declaring Python Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
9.2.2.3. Declaring RPM Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
9.2.3. Building a New Automation Execution Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
9.2.3.1. Interacting with the Build Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
9.3. Validating a Custom Execution Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
9.3.1. Testing Automation Execution Environments Locally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
9.3.1.1. Running a Test Playbook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
9.3.1.2. Providing Authentication Credentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
9.3.2. Sharing an Automation Execution Environment from Private Automation Hub . . . . . . . . . . . . . . . . 80
9.4. Using Custom Content Collections and Execution Environments in Automation Controller. . . . . . . . . . 81
9.4.1. Using Custom Collections with Existing Execution Environments . . . . . . . . . . . . . . . . . . . . . . . . . . 81
9.4.1.1. Preparing Ansible Projects for Automation Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
9.4.1.2. Storing Authentication Credentials for Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
9.4.2. Using Custom Automation Execution Environments with Automation Controller . . . . . . . . . . . . . . 81
9.4.2.1. Storing Container Registry Credentials. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
9.4.2.2. Configuring Automation Execution Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
9.4.2.3. Configuring the Default Automation Execution Environment for a Project . . . . . . . . . . . . . . . . 81
9.4.2.4. Specifying an Automation Execution Environment in a Template . . . . . . . . . . . . . . . . . . . . . . 81
Appendix A: Exam Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
A.1. Understand and use Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
A.2. Manage inventory variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
A.3. Manage task execution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
A.4. Transform data with filters and plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
A.5. Delegate tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
A.6. Manage content collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
A.7. Manage execution environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
A.8. Manage inventories and credentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
A.9. Manage automation controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Appendix B: References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
B.1. Ansible Roles, Collections, and Content. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
B.2. Ansible Automation Platform (AAP 2). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
B.3. Execution Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
B.4. Callback Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
B.5. Ansible Facts and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
B.6. Ansible Galaxy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
B.7. Ansible Navigator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
B.8. Ansible Automation Hub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
B.9. Ansible Builder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
B.10. Pre-Commit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
B.11. Ansible Real-World Examples and Articles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
B.12. Other Ansbile Examples and Repositories (from other Instructors) . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
B.13. Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
B.14. VSCode Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
B.15. DISA STIG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Section Repositories for this Course
Introduction
Repositories for this Course
There are two repositories used for this course. One is for the book, content, and demos. There is a Jenkins
job that will build from the private Gitlab repository and create, modify, and upload content into the Github
repository.
Main Repositories
• DO374 - Book: https://gitlab.michettetech.com/travis/do374
• DO374 - Public Demo Repository for Students: https://github.com/tmichett/do374/
Demo Repositories
These repositories contain demo playbooks that are used as projects in Ansible Controller. They also contain
inventory files as well as configuration files needed to run the playbooks locally from workstation for testing.
2. Clone Repository
The Ansible Core package is provided by ansible-core and is version Ansible Core 2.11 in AAP2.0. This
package provides the ansible command as well as the built-in modules allowing administrators to run
playbooks with the ansible-playbook command. The ansible-core package only contains a minimal set of
modules (ansible.builtin) collection and all other modules have been moved to Ansible collections.
It is still possible to install the package called ansible. This will install Ansible 2.9 which is
AAP1.2. This version of Ansible will support collections, but is not the full AAP2.0 version of
Ansible.
Ansible content and modules have now been re-organized into what is referred to as Ansible Content
Collections (Content Collections) in order to support the growth and rapid development of modules and
packages. This separation allows modules, roles, plug-in to be separated from the Ansible Core for a simpler
management style.
• Developers can easily upgrade and deploy new version of their modules without depending on Ansible
• Only needed modules can be present on the Ansible system or in the execution environment
• New modules and content doesn’t need to wait for a new version of Ansible to be deployed
ansible.builtin
The ansible.builtin collection is a special collection that will always be part of Ansible Core.
However, this has a limited number of modules. Things like the Firealld module have now
been moved as part of the POSIX Ansible Collection.
Collection Mapping
Ansible mapping of content collections: https://github.com/ansible/ansible/blob/devel/lib/
ansible/config/ansible_builtin_runtime.yml
AAP provides ansible-navigator which is the new preffered tool to run and interact with Ansible on the CLI. It
extends and includes the functionality of the ansible-playbook, ansible-inventory, and ansible-config
commands.
While Ansible Navigator still leverages ansible.cfg, it has its own configuration file that must point to both the
ansible.cfg being used as well as using its own ansible-navigator.yml configuration file which has even more
options to extend and control the behavior of Ansible Navigator.
Why ansible-navigator?
The purpose of ansible-navigator is to separate the control node from the execution
environment. This makes it easier for playbooks to be run in a production environment from
Ansible Controller Nodes (formerly known as Ansible Tower).
Ansible Execution Environments (EEs) as container images which contain the following items:
Ansible EEs
• Ansible Core
• Ansible Content Collections
◦ Ansible Modules
◦ Ansible Roles
• Python Libraries
• Other dependencies
The default AAP2 environment provides Ansible Core 2.11 and Red Hat Certified Content Collections to give a
similar experience to AAP1.2 which is what provides Ansible 2.9.
Ansible 2.9 is part of AAP1.2, but it supports things like Ansible Collections. You must have
AAP2 to support things link Ansible Navigator, and other components of the AAP2 platform.
The ansible-builder package can be used to create and develop your own custom execution environments.
Automation Controller provides a central web-based UI and REST API which can be used to automate Ansible
jobs. Previous iterations of Ansible leveraged Ansible Tower which was the control node and execution
environment. With the deployment of AAP2, Ansible Tower was re-named to Ansible Automation Controller
and serves as the control node only, as with Ansible Automation Controller, the execution environment can be
separated from the controller node as it now runs in a container.
By separating the control node functionality and execution environments, it is much easier to leverage the
system when playbooks could require different python environments or other requirements to run.
Automation Controller
AAP2 Automation Controllers has the ability to use multiple execution environments on
playbook and project levels as the execution plan is 100% separate from the control plane.
Ansible Automation Hub allows easy management and distribution of Ansible automation content. Red Hat
maintains supported and certified content collections and Ansible Galaxy maintains the community-based
content. The addition of Automation Hub also provides the ability to host a private automation hub which is
basically a self-hosted version of Ansible Galaxy or Red Hat’s console.redhat.com version of Automation
Hub.
The private automation hub provides a container registry for distribution of custom execution environments as
well as a repository for Ansible Collections and namespaces.
Ansible Execution Engines (EEs) can be built and customized to contain everything needed to execute
playbooks developed by your organization. These playbooks can be leveraged seamlessly between content
navigator and automation controller providing access is available to the EEs being used (which is where
automation hub comes into play).
Ansible ad-hoc commands are not supported with Ansible Navigator and not reccomended
as a best practice. However, ad-hoc commands can still be run by installing the Ansible
package and leveraging the ansible command.
In order to run a playbook using Ansible Navigator, you must use the ansible-navigator run command. It is
possible to use Ansible Navigator to provide the same output as the ansible-playbook command by providing
the argument with the run command and using -m stdout.
ansible-navigator Use
If the -m stdout is not provided, ansible-navigator runs the playbook in interactive mode.
This mode allows analyzing plays, tasks, and the runtime in a more detailed fashion.
Typically, you use number for what should be displayed, but if the number is >9 it is
necessary to use : followed by the number. The interactive mode interface can be exited by
hitting the escape key (multiple times, depending on the level being analyzed).
Execution environments were introduced as part of AAP2. The introduction of EEs meant that Ansible could be
run from a container image that included Ansible Engine runtimes, content collections, software dependencies,
and python components needed to run playbooks and interact with Ansible. EEs allow ansible-navigator and
Ansible Automation Controller to leverage automation execution environments simplifying development,
testing, and deployment of Ansible playbooks in a consistent and predictable fashion. Red Hat provides
several supported EEs from Red Hat’s Ansible Automation Hub.
EEs allow ansible-navigator and Ansible Controller to easily leverage custom execution environments by
specifying an Execution Environment Image (--eei) to be used for running playbooks. By specifying EEIs, it is
no longer necessary to have multiple configurations on control nodes to run Ansible playbooks.
SSH access can be prepared by creating users on the systems and setting up SSH key-pairs between the
systems. The SSH key pair is created with ssh-keygen and usually resides in ~/.ssh directory. The public
key is installed on the remove system in the ~/.ssh/authorized_keys file usually with the ssh-copy-id
command.
SUDO access is generally granted without password access by creating a sudoers file for the user in the
/etc/sudoers.d directory.
① Allows the devops user SUDO access for all commands without requiring a password.
There are some tricks to running and leveraging ansible-navigator as the SSH private key must somehow
become available to the EE. When running in a GUI environment, ssh-agent is already running and will add
private keys to the agent. This same behavior doesn’t happen when logged into the systems via SSH.
① Starting ssh-agent
② Adding Identities to SSH-Agent Keyring
Ansible navigator goes beyond the traditional Ansible commands and provides additional functionality.
Navigator and its sub-commands can be run from the command line (cli) or within the interactive content
navigator session.
Subcommand Description
collections Get information about installed collections.
When runnign ansible-navigator in Interactive Mode, it is possible to use the subcommands by placing a :
and the subcommand. For example, you can do :run to run a playbook.
The ansible-navigator command can be used to run playbooks leveraging an Execution Environment Image
(EEI) which creates a container known as an Execution Environment (EE). The EEI is already set to use the
ansible-playbook command, however, it is possible to still execute Ansible ad-hoc commands leveraging
ansible-navigator and the configured EE
The exec — ansible portion of the command will replace the ansible-playbook command in the container
and instead use the ansible command and everything that comes after it as an ad-hoc command.
Ansible ad-hoc commands can be extremely useful for testing the Ansible configuration, specifically,
ansible.cfg, ansible-navigator.yml, and inventory. It is also extremely useful for testing things like the
Ansible user, SSH keys, and suoders capabilities.
It is possible to run an Ansible playbook using the ansible-navigator run command both interactively or with
stdout like the ansible-playbook command. If you are in interactive mode, the playbook output can be
examined interactively.
anr playbook.yml
ansible-navigator provides a replay feature of playbook runs, providing artifacts are enabled, an artifact will
be generated with a PlaybookName-artifact-date.json format. The ansible-navigator replay command can
be used from both the command line and interactive.
ansible-navigator can prompt for passwords and input only if artifacts are disabled. It is
possible to control and configure Ansible Navigator with the ansible-navigator.yml file
which is discussed later in the course.
Documentation can be read using the ansible-navigator doc <module_name>. Unlike the ansible-doc
command, the --list and -l option cannot list items and instead, must specify the plug-in or module name.
The ansible-navigator --help command can be used to view help view STDOUT.
ansible-navigator --help
The ansible-navigator --help doesn’t always display all options. It may be necessary to
perform additional options to output the help correctly.
The exec — ansible portion of the command will replace the ansible-playbook command in the container
and instead use the ansible command and everything that comes after it as an ad-hoc command.
It is also important to remember that ansible-navigator is a command that leverages an underlying container
technology (Podman on Red Hat Systems) and that the EEI is a container image. Utilities like Podman can
assist in understanding how and what is happening in an EE.
Listing 12. Ad-Hoc Using Navigator (Testing Config for SSH w/ Creds)
Listing 13. Ad-Hoc Using Navigator (Testing Config for SSH w/o Creds)
Listing 14. Ad-Hoc Using Navigator (Testing Config for SSH & Sudoers w/ Creds)
Listing 15. Ad-Hoc Using Navigator (Testing Config for Sudoers w/o Creds)
2. Install Navigator
Installed:
ansible-navigator-1.0.0-2.el8ap.noarch
Complete!
① The $EE environment variable provides the EE for the ansible-navigator command
① Starting ssh-agent
② Adding key to keyring for SSH-Agent
ansible-playbook Equivalence
The ansible-navigator run playbook.yml -m stdout will provide the same STDOUT as the
ansible-playbook command. There are some other features about ansible-navigator but
those will be covered in a later chapter and section.
6. Run the ansible-navigator run command interactively (Leave out the -m stdout)
b. Get detailed output of Task 13 (Hit : and then hit 13 and enter to navigate)
7. Exit Ansible Navigator by hitting the ESC key multiple times to exit each layer.
[student@workstation navigator]$
The git config command controls all settings and user settings will be saved in ~/.gitconfig file. The settings
in the .gitconfig file are global and are set using the --global directive paired with the git config command.
# .bashrc
source /usr/share/git-core/contrib/completion/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true
export PS1='[\u@\h \W$(declare -F __git_ps1 &>/dev/null && __git_ps1 " (%s)")]\$ '
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
git-prompt.sh Key
• (branch *) - means that you have modified a tracked file.
• (branch +) - means that you have modified and staged with git add a tracked file.
• (branch %) - means that you have untracked files in your tree.
• Combinations of markers are possible, such as (branch *+) meaning there are multiple
files to be tracked, staged, etc.
GIT repositories can be created from scratch and initialized or they can be cloned. The following diagram
shows some of the ways of interacting with and creating a GIT repo.
GIT Commands
The git commit -a file can stage and commit modified files in one step (meaning that it does
the git add), however, it doesn’t stage any new untracked files. A git add command must be
used to stage new files for the first time.
The git log command can display commit log messages as well as hashes for each commit.
Roles and collections can be difficult to plan and manage. There are advantages to possibly keeping a static
role or collection as part of the Ansible project, but general best-practice is to utilize the most current version of
a role or content collection.
Working with AAP 2.x (especially when using ansible-navigator) it is important to think about development,
testing, and management of the project. In addition to collections and roles, it is necessary to think about all the
artifacts that could be generated by the ansible-navigator command as well as any logs. Therefore, in the
main portion of the project, there should also be a .gitignore that will ignore assets/artifacts created by the
ansible-navigator command.
roles/**
!roles/requirements.yml
collections/**
!collections/requirements.yml
ansible-navigator.log
*-artifact-*
.ssh ①
① The .ssh directory can be created to have the SSH config file and SSH Keys and identities. If this is
located in the project directory, then ansible-navigator can provide this information to the Ansible
Execution Environment (EE) and it eliminates the need to use ssh-agent.
① Prompt changed to + indicating new files being tracked, but not committed
① Normal Prompt
1. Change to the
cd /home/student/Github/do374/Demos/CH1/git
cp vars/git_creds.yml_demo vars/git_creds.yml
ansible-playbook Git_Tools_Setup.yml -K
gh auth login
✔ ~/Github/do374/Demos/CH1/git [main|✔]
22:54 $ gh issue list
Use YAML formatting in the default style/syntax and not the folded form to enable better readability.
Additionally, use Jinja2 filters and templates to process data in variables.
It is also good practice to make use of vertical white space allowing better readability for the end user.
When writing playbooks, start with a basic playbook and use a static inventory file. Use debug modules as
stubs to assist in designing playbooks and verifying output.
Even though modules have a default state, it is best practices to specifically define the state within the module.
This makes the playbook easier to read and protects against changes that might occur to the module in the
future.
AAP2.x Modules
With the shift in Ansible Automation Platform, many of the modules that used to be built-in to
Ansible have shifted and now live in collections. It is important to understand and know that
these modules do still exist, but now they are part of a collection. Avoid, when possible, the
use of the command, shell, and raw arguments as these aren’t Idempotent modules and
should only be used when a module isn’t available.
YAML is a formatted style of writing, therefore, white spaces for indention are very important. It is a good idea
to decide how many spaces are used to indent (most people choose 2 spaces) which deals with horizontal
alignment and white spaces. It should also be determined how vertical white space will be managed for
readability of the playbooks and tasks.
In addition to using space effectively, naming conventions of variables and labeling of plays/tasks should be
considered in addition to how/where to leave comments within the playbook.
Variable naming conventions should be decided and followed throughout playbook creation.
Naming Conventions
Use a consistent structure, especially if planning on submitting roles to Ansible Galaxy and Github.
├── collections/
│ └── requirements.yml
│ └── example_collection/
├── dbservers.yml
├── inventories/
│ ├── prod/
│ │ ├── group_vars/
│ │ ├── host_vars/
│ │ └── inventory/
│ └── stage/
│ ├── group_vars/
│ ├── host_vars/
│ └── inventory/
├── roles/
│ └── std_server/
├── site.yml
├── storage.yml
└── webservers.yml
The example structure above shows that there are two inventory files and variables which allow separation of
variables based on the specific inventory files. The shared playbooks are at the root level of the directory,
where the roles being used are under the roles directory.
The benefit of this structure allows large playbooks to be split into smaller files making playbooks more
readable and understandable.
Dynamic inventories should be used when possible, especially when systems are VMs existing in a
virtualization or cloud environment. Dynamic inventories allow for central management of hosts and groups
from a single location ensuring that inventory is automatically updated.
• Geographic location: Where systems are located (regions, countries, data centers)
• Environment: Stage of SDLC (dev, test, qa, prod)
• Sites/Services: Grouping of hosts in similar subset of functions (webserver, database server, proxy, etc.)
Hosts inherit variables from all groups they are members. If the same variable exists with
different settings across the groups in which a host is a member, the last variable loaded is
the one that will be used.
1.8.3.5. Using Roles and Ansible Content Collections for Reusable Content
Roles keep playbooks simple. The ansible-galaxy command can initialize the role’s directory hierarchy and
make provide the initial template files that need to be used. The ansible-galaxy command can also be used to
get roles from separate Git repositories not stored on Ansible Galaxy. Ansible Galaxy is also used to manage
Ansible content collections. In the case of both roles and collections a requirements.yml file can be created to
specify the installation source of the role of collection.
roles and collections respectively. It is also necessary to configure the ansible.cfg file to
have the collections path so it searches the ./collections path. It is also recommended to
use a requirements.yml file to install both roles and collections using the ansible-galaxy
command.
Ansible playbooks should be run from a designated control node. Each system administrator should have their
own usernames/passwords and SSH keys to access the environment and managed in the authorized_keys
file. Ansible Controller greatly assists in management of users and credentials.
Custom Ansible execution environments should be created with collections and all Python dependencies if
these collections and Python requirements will be frequently used. The custom EE can then easily be used by
developers and administrators alike leveraging Ansible Content Navigator or Ansible Controller.
The results of the tasks should always be tested rather than relying on return codes from a specific Ansible
module.
The block directive can be used for grouping tasks and used in conjunction with rescue in order to recover from
errors or failures.
- block:
- name: Check web site from web server
uri:
url: http://{{ ansible_fqdn }}
return_content: yes
register: example_webpage
failed_when: example_webpage.status != 200
rescue:
- name: Restart web server
service:
name: httpd
status: restarted
Playbooks should be tested with the latest version of Ansible routinely to avoid issues as Ansible modules and
features evolve. In particular, watch for warnings or deprecation messages when playbooks are run.
Deprecated features generally remain for four (4) minor releases of Ansible before they are completely
removed or changed.
• ansible-playbook --syntax-check: Performs basic syntax checking of playbook without actually running
the playbook.
• ansible-playbook --check: Allows the playbook to be run against managed hosts without changing things.
It should be noted this test may fail if tasks require a physical change within the play to move on.
There are a few other Ansible tools out there to assist with Ansible playbook development
that are available upstream but not included in RHEL 8.
• ansible-lint: Parses playbook and looks for issues within the playbook.
• yamllint : Parses YAML file and attempts to identify syntax errors (not Ansible specific)
One of the biggest areas of concern is with the Ansible.Posix collection as this collection
now contains FirewallD and other modules that are commonly utilized to maintain RHEL
Systems.
firewalld:
redirect: ansible.posix.firewalld
Ansible Content Collections are organized into namespaces. The namespace must be unique and is generally
assigned to a vendor or individual. Namespaces are the first part of a Fully-Qualified Collection Name (FQCN).
Collections maintained by the Ansible community are located on Ansible Galaxy under the community
namespace.
The ansible-navigator collections command can list collections available in Ansible EEs. It is possible to list
modules within collections by selecting the collection you want to see by hitting :XX where XX is the number of
the collection you wish to reference. From there it is possible to get further information on modules within the
collections.
In order to retrieve documentation from ansible-navigator, it is necessary to use the ansible-navigator doc
command with the collection name and appending --mode stdout in order for it to display on the command
line.
EXAMPLES: ①
In order to properly use collections in playbooks a Fully Qualified Collection Name (FQCN) should be used. An
example of a FQCN would be ansible.posix.firewalld. It is common for many people to ignore the FQCN
when it is an Ansible built-in collection, but to be sure on which collection and module is used, it is considered
best practices to use FQCN for all Ansible tasks like using ansible.builtin.yum instead of just yum.
Ansible includes a small subset of built-in modules. It is often accepted to use these modules using the short
name, but Red Hat reccomends using the FQCN even for the ansible.builtin.<module> modules.
^f/PgUp page up ^b/PgDn page down ↑↓ scroll esc back [0-9] goto :help
help
^f/PgUp page up ^b/PgDn page down ↑↓ scroll esc back [0-9] goto :help
help
24│doc: ④
25│ author:
26│ - Adam Miller (@maxamillion)
27│ description:
28│ - This module allows for addition or deletion of services and ports
(either TCP
29│ or UDP) in either running or permanent firewalld rules.
30│ module: firewalld
47│ options:
48│ icmp_block:
49│ description:
50│ - The ICMP block you would like to add/remove to/from a zone in
firewalld.
51│ type: str
52│ icmp_block_inversion:
53│ description:
54│ - Enable/Disable inversion of ICMP blocks for a zone in firewalld.
55│ type: str
56│ immediate:
5. Exit ansible-navigator by pressing the ESC key several times to get back to the command prompt.
EXAMPLES: ②
① Playbook to setup and configure webserver using Ansible Automation Platform 2.x with Collections
② The ansible.builtin collection used by FQCN
③ The ansible.posix collection used by FQCN to get the firewalld Module
① Playbook written using Ansible Automation Platform ⇐ AAP 1.2 or Ansible ⇐ 2.9
② Using standard Ansible modules and not leveraging collections
3. Run the playbook Website_Past.yml using the EE 2.9 image that was downloaded.
PLAY RECAP *
servera : ok=6 changed=4 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
① Ensuring that the SSH Agent Service is running so keys can be added to the keyring for containers
② Loading the SSH Key to the Keyring for ansible-navigator
③ Running the EE 2.9 image with output to the screen to run the playbook.
execution-environment:
image: ee-supported-rhel8:2.0
PLAY RECAP *
serverb : ok=6 changed=4 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
cd Github/do374/Demos/CH3/Controller/
./Demo_Prep.sh
5. Managing Inventories
5.1. Managing Dynamic Inventories
Section Info Here
1. Ensure ansible-navigator.yml config is updated, the ansible.cfg has a valid (non-root) user, and playbooks are
available.
---
ansible-navigator:
ansible:
config: ./ansible.cfg
execution-environment:
image: ee-supported-rhel8:2.0
pull-policy: missing
mode: stdout ①
playbook-artifact:
enable: false
① Set Mode to Standard Out to see output from the terminal interface.
[defaults]
inventory=inventory.yml
remote_user=devops
order = reverse_sorted
---
- name: Playbook to Show Users
hosts: all
become: true
tasks:
- name: show ansible_user_id with BECOME=true
debug:
var: ansible_user_id ①
tasks:
- name: show ansible_user_id with BECOME=false
debug:
var: ansible_user_id
① The ansible_user_id is a special variable from Ansible gathered facts. This variable captures the user
executing the commands for a given task. If Fact Gathering is set to false, this value is not available.
✔ ~/Github/do374/Demos/CH6/Priv_Escallation [main|✔]
11:51 $ ansible-navigator run Priv_Demo_Book.yml
"ansible_user_id": "root"
}
PLAY RECAP
***************************************************************************
servera.lab.example.com : ok=4 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
serverb.lab.example.com : ok=4 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
serverc.lab.example.com : ok=4 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
cd /home/student/Github/do374/Demos/CH6/tags
① The community.general collection has the modules and filters necessary to perform JSON Queries and
manipulate data.
② The jmespath Python installation is needed for the modules and filters in the community.general
collection and needs to be installed for the Python 3.6 version (if that is what is used)
③ The jmespath Python installation is needed for the modules and filters in the community.general
collection and needs to be installed for the Python 3.9 version (if that is what is used)
Essentially, in addition to the community.general collection, the jmespath Python Package is needed to
function and it is version specific to the version of Python being used by Ansible.
anr json_query_demo.yml
TASK [Get the 'name' elements from the list of dictionaries in 'hosts']
**************
ok: [localhost] => {
"changed": false,
"msg": "All assertions passed"
}
TASK [Get the 'name' elements from the list of dictionaries in 'hosts']
**************
fatal: [localhost]: FAILED! => {
"assertion": "['bastion', 'classroom', 'nada'] is eq( ['bastion',
'classroom'] )", ①
"changed": false,
"evaluated_to": false,
"msg": "Assertion failed"
}
① Can see the assertion and why/how it failed because nada isn’t in the check.
8.3.1. Overview
yum install jq
Listing 46. Git Commands to Setup Git and the Repository behavior
The git config XXX commands are tab complete aware so it is possible to get the syntax
and items using tab completion.
# git add .
# git push
• Structure host and group variables using multiple files per host or group
◦ GE: Managing Inventory Variables
• Use special variables to override the host, port, or remote user for a specific host
◦ Section: Using Special Inventory Variables
◦ GE: Managing Inventory Variables
• Set up directories containing multiple host variable files for managed hosts
◦ Section: Separating Variables from Inventory
• Override names used in inventory files with a different name or IP address
◦ Section: Using Special Inventory Variables
FORKS
The Forks setting controls parallel runs and is in the defaults section of the ansible.cfg file
as forks=#. It can also be overriden on the command line using -f #.
Hashing and Encrypting Filters (Section: Hashing, Encoding, and Manipulating Strings)
• hash
• password_hash
• Populate variables with data from external sources using lookup plugins
◦ GE: Templating External Data Using Lookups
• Use lookup and query functions to incorporate data from external sources into playbooks and deployed
template files
◦ GE:
• Implement loops using structures other than simple lists using lookup plugins and filters
◦ GE: Implementing Advanced Loops
• Inspect, validate, and manipulate variables containing networking information with filters
◦ GE: Using Filters to work with Network Addresses
Chapter 9
Commands Used
• ansible-galaxy collection init <namespace>.<collectionname>
• ansible-galaxy collection build
• ansible-galaxy collection publish
Getting Help
There is very little information in the man pages. However, the ansible-galaxy collection
--help and ansible-galaxy collection init --help commands can provide assistance and
context.
# mkdir meta
Listing 52. Creating meta/runtime.yml File
# vim meta/runtime.yml
---
requires_ansible: '>=2.9.10'
The above commands require being in the top-level of the collection directory.
The plugins sub-directory is created and part of the skeleton. In order to leverage any
custom modules, it is necessary to create a plugins/modules subdirectory and copy the
modules to this location.
Chapter 9
Appendix B: References
B.1. Ansible Roles, Collections, and Content
• Ansible Collections Overview
◦ https://github.com/ansible-collections/overview/blob/main/README.rst
• Introduction to RHEL System Roles
◦ https://www.redhat.com/en/blog/introduction-rhel-system-roles
• Ansible Galaxy: Collections and Roles
◦ https://docs.rockylinux.org/books/learning_ansible/04-ansible-galaxy/
• Ansible Collection Index
◦ https://docs.ansible.com/ansible/latest/collections/index.html#list-of-collections
• Ansible Module Mapping - Where have all the modules gone??
◦ https://github.com/ansible/ansible/blob/devel/lib/ansible/config/ansible_builtin_runtime.yml
• Ansible Posix Collection
◦ https://github.com/ansible-collections/ansible.posix
• Ansible Automation Platform Certified Content
◦ https://access.redhat.com/articles/3642632
• Migrating to Ansible Collections
◦ <a href="https://www.ansible.com/hubfs/Webinar%20PDF%20slides/2020-Dec-
08—​Webinar%20Migrating%20to%20Ansible%20Collections.pdf"
class="bare">https://www.ansible.com/hubfs/Webinar%20PDF%20slides/2020-Dec-08—&#
8203;Webinar%20Migrating%20to%20Ansible%20Collections.pdf</a>
• Ansible Netcommon
◦ https://github.com/ansible-collections/ansible.netcommon/blob/main/meta/runtime.yml
• Ansible Collections on Galaxy
◦ https://galaxy.ansible.com/ui/namespaces/ansible/
◦ https://galaxy.ansible.com/ui/namespaces/community
◦ https://galaxy.ansible.com/ui/namespaces/tmichett
◦ https://galaxy.ansible.com/ui/namespaces/redhat_cop
• Creating collections
◦ https://docs.ansible.com/ansible/devel/dev_guide/developing_collections_creating.html#creating-
collections-skeleton
• Using collections
◦ https://docs.ansible.com/ansible/latest/user_guide/collections_using.html
• Ansible Posix Collection
◦ https://galaxy.ansible.com/ansible/posix
https://github.com/ansible/ansible-navigator/issues/611
• Linux: https://github.com/cli/cli/blob/trunk/docs/install_linux.md
• General Instructions: https://cli.github.com/manual/installation
GH Token Creation
To get started with GitHub CLI, please run: gh auth login Alternatively, populate the
GH_TOKEN environment variable with a GitHub API authentication token.
B.10. Pre-Commit
The Jenkins jobs use pre-commit rules to check items before running. Failure to pass these checks will result
in Jenkins build failures. Instructions for using pre-commit are in the text, but it might be recommended and
easier to enable pre-commit automatically.
B.13. Python
• How to install Python 3 on Red Hat Enterprise Linux
◦ https://developers.redhat.com/blog/2018/08/13/install-python3-rhel?source=sso#tl_dr