Configuring Ansible within a Devbox Environment¶
Assuming you have set up a Devbox environment (as described in our Devbox Setup Guide), this guide focuses on configuring Ansible within that environment, including project structure, using Ansible Vault for secrets management, and writing a simple playbook.
Setting Up Your Project Structure¶
With your Devbox environment ready (including Ansible installed via requirements.txt), let's set up a standard Ansible project structure:
# Ensure you are inside your devbox shell
mkdir -p playbooks roles inventory group_vars host_vars
touch ansible.cfg
Create a basic ansible.cfg file to configure Ansible's behavior:
[defaults]
inventory = ./inventory
roles_path = ./roles
host_key_checking = False
vault_password_file = ./.vault_pass.txt
[privilege_escalation]
become = True
become_method = sudo
become_ask_pass = False
Working with Ansible Vault¶
Ansible Vault is a feature that allows you to keep sensitive data such as passwords or keys encrypted rather than in plaintext. Let's set it up:
- Create a vault password file (make sure to add this to your
.gitignore): To generate a random password, use pwgen. In the command above, "y" generates a secure password, "s" is responsible for symbols in the password, "14" is the length of the password, and "1" at the end helps generate only one password. - Creating encrypted variables: This will open an editor where you can add sensitive variables:
- Referencing vault variables in your playbooks:
Create
group_vars/all/vars.ymlfor non-sensitive variables that reference vault values: - Editing encrypted files:
- Encrypting strings for use in playbooks: This outputs an encrypted variable you can paste directly into YAML files.
Security Best Practice
Never commit .vault_pass.txt to your repository. Add it to your .gitignore file and share the password through a secure channel like a password manager.
Advanced: Using Multiple Vault Passwords¶
For complex projects, you might need different vault passwords for different environments:
-
Create an identity file instead of a single password:
-
Update
ansible.cfgto use multiple vault IDs: - Creating environment-specific encrypted files:
Creating a Simple Playbook¶
Let's create a simple playbook to test our Ansible setup within the Devbox environment:
Edit playbooks/test.yml:
---
- name: Test Playbook
hosts: localhost
connection: local
gather_facts: yes
vars:
message: "Hello from Ansible in Devbox!"
tasks:
- name: Print message
debug:
msg: "{{ message }}"
- name: Show Python version used by Ansible
command: python --version
register: python_version
- name: Display Python version
debug:
msg: "Ansible is using Python: {{ python_version.stdout }}"
Run the playbook from within your devbox shell:
You should see output confirming the message and the Python version (matching the one specified in your devbox.json).
Troubleshooting Common Issues¶
SSH Key Issues¶
If Ansible fails to connect to remote hosts via SSH:
- Ensure your SSH key is added to the SSH agent
- Verify SSH connectivity outside of Ansible
- Check Ansible inventory for correct hostnames/IPs and SSH users/ports
- Test Ansible ping module
Vault Decryption Problems¶
If you're seeing "Decryption failed" errors when running playbooks:
# Verify vault password file permissions are correct (readable only by user)
chmod 600 .vault_pass.txt
# Manually test decryption using the vault password file
ansible-vault view group_vars/all/vault.yml
# If using multiple vault IDs, ensure the correct ID is specified or available
ansible-vault view --vault-id dev@.vault_pass_dev.txt group_vars/dev/vault.yml
vault_password_file path in ansible.cfg is correct relative to where you run ansible-playbook.