Ansible Components
Ansible components run Ansible playbooks for configuration management and infrastructure automation. They allow you to manage playbook execution with the same stack-based configuration approach used for Terraform, Helmfile, and Packer.
Available Configuration Sections
Ansible components support the common configuration sections:
vars- Variables passed to ansible via
--extra-vars. env- Environment variables during execution.
settings- Integrations, metadata, and Ansible-specific settings like playbook and inventory.
metadata- Component behavior and inheritance.
command- Override ansible binary.
hooks- Lifecycle event handlers.
Component Structure
A typical Ansible component configuration:
components:
ansible:
webserver:
vars:
app_name: myapp
app_port: 8080
app_version: "1.0.0"
env:
ANSIBLE_HOST_KEY_CHECKING: "false"
settings:
ansible:
playbook: site.yml
inventory: inventory/production
Ansible Directory Structure
Ansible components are located in the path configured in atmos.yaml:
# atmos.yaml
components:
ansible:
base_path: components/ansible
Example structure:
components/ansible/
├── hello-world/
│ ├── site.yml
│ └── inventory.ini
├── webserver/
│ ├── site.yml
│ ├── roles/
│ │ └── nginx/
│ └── inventory/
│ ├── production
│ └── staging
└── database/
├── deploy.yml
└── inventory.ini
Playbook Configuration
Each Ansible component contains playbooks that use variables passed from Atmos:
components/ansible/hello-world/site.yml
Playbook and Inventory Resolution
The playbook and inventory can be specified in two ways, with command line flags taking precedence:
- Stack manifest settings — Define
settings.ansible.playbookandsettings.ansible.inventory - Command line flags — Use
--playbook/-pand--inventory/-i
components:
ansible:
webserver:
settings:
ansible:
playbook: site.yml # Default playbook
inventory: inventory/production # Default inventory
Component-Type Defaults
Define defaults for all Ansible components:
# Apply to all Ansible components
ansible:
vars:
managed_by: Atmos
env:
ANSIBLE_HOST_KEY_CHECKING: "false"
ANSIBLE_FORCE_COLOR: "true"
# Individual components
components:
ansible:
webserver:
vars:
app_port: 8080
Complete Example
stacks/orgs/acme/plat/prod/us-east-1.yaml
Variable Handling
Atmos automatically generates a YAML variables file containing all variables from the component's
vars section. This file is passed to Ansible using --extra-vars @<filename>.
The generated file follows the naming convention: <context>-<component>.ansible.vars.yaml
Here, <context> is the full Atmos context prefix (e.g., tenant-account-stage-region), not just the stack name.
For example, for a component webserver with context prefix acme-plat-prod-us-east-1, the file would be:
acme-plat-prod-us-east-1-webserver.ansible.vars.yaml
The file is automatically cleaned up after playbook execution completes.
Running Ansible Commands
Atmos provides commands that wrap Ansible operations:
# Run a playbook
atmos ansible playbook webserver -s prod
# Run with explicit playbook and inventory
atmos ansible playbook webserver -s prod -p site.yml -i inventory/production
# Dry run (shows command without executing)
atmos ansible playbook webserver -s prod --dry-run
# Pass options directly to ansible-playbook
atmos ansible playbook webserver -s prod -- --check -vvv
# Check Ansible version
atmos ansible version
Environment Variables
Common environment variables for Ansible components:
ANSIBLE_HOST_KEY_CHECKING- Disable SSH host key checking (set to
false). ANSIBLE_FORCE_COLOR- Force colored output (set to
true). ANSIBLE_CONFIG- Path to Ansible configuration file.
ANSIBLE_VAULT_PASSWORD_FILE- Path to Ansible Vault password file.
ANSIBLE_ROLES_PATH- Additional paths to search for roles.
ANSIBLE_COLLECTIONS_PATH- Paths to search for collections.
Try It
See the demo-ansible example for a minimal hello-world setup demonstrating variable passing:
cd examples/demo-ansible
atmos ansible playbook hello-world -s dev
Best Practices
-
Use Settings for Playbook Config: Define
settings.ansible.playbookandsettings.ansible.inventoryin stack manifests rather than passing flags every time. -
Centralize Defaults: Define common settings in catalog defaults and override only when necessary.
-
Use Dependencies: Define
depends_onwhen playbooks need to run after infrastructure is provisioned. -
Keep Playbooks Small: Create focused playbooks for specific tasks rather than monolithic automation.
-
Use Environment Variables: Configure Ansible behavior through
envrather thanansible.cfgfor consistency across environments. -
Leverage Inheritance: Use abstract components and inheritance for shared playbook configurations across environments.