- Published on
Environment Variables with Ansible
- Authors
- Name
- Ruan Bekker
- @ruanbekker
This is a quick post on how to use environment variables in ansible
Inventory
Our inventory.ini
file looks like this:
[localhost]
localhost
Across Tasks
You can set environment variables across tasks, and let your tasks inherit the variables:
- hosts: localhost
vars:
var_mysecret: secret123
tasks:
- name: echo my env var
environment:
MYNAME: "{{var_mysecret}}"
shell: "echo hello $MYNAME > /tmp/bla.txt"
args:
creates: /tmp/bla.txt
When we run the task:
$ ansible-playbook -i inventory.ini -u ruan task.yml
Check the output:
$ cat /tmp/bla.txt
hello secret123
Environment Variables Per Task
You can set environment variables per task:
- hosts: dev
tasks:
- name: echo my env var
environment:
MYNAME: "RUAN"
shell: "echo hello $MYNAME > /tmp/bla2.txt"
args:
creates: /tmp/bla2.txt
Running the task:
$ ansible-playbook -i inventory.ini -u ruan task.yml
Checking the output:
$ cat /tmp/bla2.txt
hello RUAN
Read More
Read more on environment variables in ansible in their documentation
Thank You
Thanks for reading, if you like my content, feel free to check out my website, and subscribe to my newsletter or follow me at @ruanbekker on Twitter.
- Linktree: https://go.ruan.dev/links
- Patreon: https://go.ruan.dev/patreon