Ansible V (Conditional loops & Ansible Vault)

Alex Izuka
4 min readOct 27, 2023

I began a series on what you need to know about Ansible, you can click here for the last series. In the concluding aspect of this series, we will look at using conditional loops in Ansible, Ansible Vault, and debugging techniques for common issues in Ansible.

Using conditional loops

Conditionals and loops in Ansible playbooks provide the flexibility to perform tasks based on specific conditions or iterate through a list of items. We will see its usage below.

Step 1: Create a List Variable

Create a variable in your playbook that holds a list of items. In this example, we’ll define a list variable named websites:

---
- name: Manage Web Servers
hosts: web_servers
vars:
websites:
- example.com
- test.com
- demo.com

Step 2: Use a Loop to Iterate Through the List

In the tasks section of your playbook, use the with_items parameter to iterate through the list of websites and perform a task for each item. For example, we'll create a directory for each website:

  tasks:
- name: Create website directories
file:
path: "/var/www/{{ item }}"
state: directory
with_items: "{{ websites }}"

In this task, we use the file module to create a directory with the name of each website from the websites list.

Step 3: Use a Conditional Statement

You can also use conditional statements to execute tasks based on specific conditions. In this example, we’ll use a conditional statement to install a web server (Nginx) only if a certain condition is met:

    - name: Install Nginx if condition is met
apt:
name: nginx
state: present
when: install_nginx | default(false)

In this task, the when parameter specifies that the task should only run if the install_nginx variable is set to true. You can set the install_nginx variable to true or false based on your requirements.

Run the playbook using the ansible-playbook command:

ansible-playbook my_playbook.yml

Ansible Vault

Ansible Vault is a feature of Ansible that provides secure credential management and data encryption. It allows you to encrypt sensitive data, such as passwords, API keys, and other secrets, so that they can be safely stored in version control systems or shared among team members without exposing the actual content.

Using Ansible Vault for Secure Credential Management

  1. Create an Encrypted File: To create an encrypted file, use the ansible-vault command with the create option:
ansible-vault create secrets.yml

This will open a text editor for you to enter your sensitive data. Once you save and exit, the data will be encrypted.

2. Edit an Encrypted File: You can also edit an existing encrypted file using the ansible-vault edit command:

ansible-vault edit secrets.yml

This command will prompt you for the password and open the file in a text editor for editing.

3. Encrypt an Existing File: To encrypt an existing file, use the ansible-vault encrypt command:

ansible-vault encrypt existing_secrets.yml

This command will encrypt the file and prompt you for a password.

Use Encrypted Variables

You can use Ansible Vault-encrypted variable files in your playbooks. For example, if you have a file named secrets.yml, you can include it in your playbook like this:

- name: Playbook Name
hosts: my_host
vars_files:
- secrets.yml
tasks:
# Your tasks here

Running Playbooks with Ansible Vault

When you run a playbook that uses encrypted files, Ansible will prompt you for the password to decrypt the data. You can provide the password interactively or use a password file.

ansible-playbook my_playbook.yml --ask-vault-pass

To avoid providing the password interactively, you can store it in a password file and use the --vault-password-file option:

ansible-playbook my_playbook.yml --vault-password-file my_password_file.txt

Using Ansible Vault allows you to securely manage sensitive data in your Ansible automation, ensuring that confidential information remains encrypted and protected from unauthorized access.

Debugging common issues in Ansible

Below are some common Ansible issues and debugging techniques.

1. Syntax Errors:

  • Issue: Syntax errors in a playbook.
  • Debugging: Use ansible-playbook --syntax-check to validate playbook syntax.
ansible-playbook --syntax-check my_playbook.yml

2. Host Unreachable:

  • Issue: Unable to connect to a target host.
  • Debugging: Use verbose mode -vvv for more information.
ansible-playbook -i inventory.ini my_playbook.yml -vvv

3. Undefined Variables:

  • Issue: A variable is undefined.
  • Debugging: Use debug module to print variable values.
- name: Debug Variable
debug:
var: my_variable

4. Playbook Conditionals:

  • Issue: Conditionals not evaluating as expected.
  • Debugging: Use the debug module to print variable values and test conditional expressions.
- name: Debug Conditional
debug:
msg: "The condition is {{ my_condition }}"
when: my_condition

5. Logging and Debug Output:

  • Issue: Lack of visibility into playbook execution.
  • Debugging: Use debug module to print variable values and task results.
- name: Debug Task
debug:
var: result_variable

Conclusion

We have looked at using conditional loops in Ansible, Ansible vault, and debugging techniques for common Ansible issues.

--

--