You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Variables and facts make your Ansible automation dynamic and reusable. Variables let you parameterise playbooks, while facts provide real-time information about your managed nodes.
---
- name: Deploy application
hosts: webservers
vars:
app_name: myapp
app_port: 8080
app_env: production
tasks:
- name: Print app info
debug:
msg: "Deploying {{ app_name }} on port {{ app_port }} ({{ app_env }})"
---
- name: Deploy application
hosts: webservers
vars_files:
- vars/common.yml
- vars/{{ env }}.yml
# Single variable
ansible-playbook site.yml -e "app_env=staging"
# Multiple variables
ansible-playbook site.yml -e "app_env=staging app_port=9090"
# Variables from a file
ansible-playbook site.yml -e "@vars/overrides.yml"
Ansible has a well-defined hierarchy for variable precedence. When the same variable is defined in multiple places, the highest precedence wins:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.