Skip to content

Commit c9ea72b

Browse files
committed
Ansible environment variables
1 parent 0ff701b commit c9ea72b

File tree

7 files changed

+85
-1
lines changed

7 files changed

+85
-1
lines changed
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Run playbook
2+
3+
```bash
4+
ansible-playbook --inventory inventory/ansible-env-variable-playbook/hosts ansible-env-variable-playbook.yml
5+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
- name: Example Ansible playbook for Handlers
3+
hosts: all
4+
become: yes
5+
remote_user: ubuntu
6+
roles:
7+
- custom-role
8+
9+
environment:
10+
EXAMPLE: "Foo bar"
11+
MY_VAR1: "variable value1"
12+
13+
tasks:
14+
# Environment variable at Playbook Level
15+
- name: Environment var at Playbook Level
16+
ansible.builtin.command: "echo $EXAMPLE"
17+
18+
# Environment variable only for tasks
19+
- name: Environment var for only task Level
20+
environment:
21+
MY_TASK_LEVEL_VARIABLE: "Hello World!"
22+
ansible.builtin.command: "echo $MY_TASK_LEVEL_VARIABLE"
23+
24+
#Testing Environment variable
25+
- name: Testing both playbook level as well as environment level vars.
26+
ansible.builtin.command: "echo $EXAMPLE $MY_TASK_LEVEL_VARIABLE "
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[default]
2+
ec2-18-185-110-39.eu-central-1.compute.amazonaws.com ansible_ssh_private_key_file=/Users/rahulwagh/.ssh/aws_ec2_terraform

ansible-environment-variable/roles/custom-role/tasks/main.yml

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
var_file_variable: "Hello, world in main var.yml!"

ansible-variables/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ ansible-playbook --inventory inventory/ansible-variable-playbook/hosts ansible-v
1414

1515
```bash
1616
ansible-playbook --inventory inventory/ansible-variable-playbook/hosts ansible-variables-playbook.yml --extra-vars "@my-vars.yml"
17+
```
18+
19+
```bash
20+
ansible-playbook playbook.yml
21+
--extra-vars '{"version":"1.0"}'
1722
```

ansible-variables/ansible-variables-playbook.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
vars:
1010
# 1. String Variable
11-
main_playbook_variable: "Hello, world in main playbook!"
11+
main_playbook_variable: "foo"
1212

1313
# 2. Boolean Variable
1414
is_enabled: false

0 commit comments

Comments
 (0)