| 1 |
- hosts: testhost |
| 2 |
gather_facts: no |
| 3 |
tasks: |
| 4 |
- name: verify omit is handled properly by the environment keyword |
| 5 |
shell: echo "IAMHERE = $IAMHERE; NOTHERE = $NOTHERE; ANOTHER = $ANOTHER" |
| 6 |
environment: |
| 7 |
- NOTHERE: '{{ omit }}' |
| 8 |
IAMHERE: hello |
| 9 |
- '{{ omit }}' |
| 10 |
- ANOTHER: stillhere |
| 11 |
register: result |
| 12 |
|
| 13 |
- assert: |
| 14 |
that: |
| 15 |
- result.stdout == "IAMHERE = hello; NOTHERE = ; ANOTHER = stillhere" |
| 16 |
|
| 17 |
- hosts: testhost |
| 18 |
gather_facts: no |
| 19 |
tasks: |
| 20 |
- name: get PATH from target |
| 21 |
command: echo $PATH |
| 22 |
register: target_path |
| 23 |
|
| 24 |
- hosts: testhost |
| 25 |
vars: |
| 26 |
test1: |
| 27 |
key1: val1 |
| 28 |
environment: |
| 29 |
PATH: '{{ansible_env.PATH + ":/lola"}}' |
| 30 |
lola: 'ido' |
| 31 |
tasks: |
| 32 |
- name: ensure special case with ansible_env is skipped but others still work |
| 33 |
assert: |
| 34 |
that: |
| 35 |
- target_path.stdout == ansible_env.PATH |
| 36 |
- "'/lola' not in ansible_env.PATH" |
| 37 |
- ansible_env.lola == 'ido' |
| 38 |
|
| 39 |
- name: check that envvar does not exist |
| 40 |
shell: echo $key1 |
| 41 |
register: test_env |
| 42 |
|
| 43 |
- name: assert no val in stdout |
| 44 |
assert: |
| 45 |
that: |
| 46 |
- '"val1" not in test_env.stdout_lines' |
| 47 |
|
| 48 |
- name: check that envvar does exist |
| 49 |
shell: echo $key1 |
| 50 |
environment: "{{test1}}" |
| 51 |
register: test_env2 |
| 52 |
|
| 53 |
- name: assert val1 in stdout |
| 54 |
assert: |
| 55 |
that: |
| 56 |
- '"val1" in test_env2.stdout_lines' |
| 57 |
|
| 58 |
- hosts: testhost |
| 59 |
vars: |
| 60 |
test1: |
| 61 |
key1: val1 |
| 62 |
test2: |
| 63 |
key1: not1 |
| 64 |
other1: val2 |
| 65 |
environment: "{{test1}}" |
| 66 |
tasks: |
| 67 |
- name: check that play envvar does exist |
| 68 |
shell: echo $key1 |
| 69 |
register: test_env3 |
| 70 |
|
| 71 |
- name: assert val1 in stdout |
| 72 |
assert: |
| 73 |
that: |
| 74 |
- '"val1" in test_env3.stdout_lines' |
| 75 |
|
| 76 |
- name: check that task envvar does exist |
| 77 |
shell: echo $key1; echo $other1 |
| 78 |
register: test_env4 |
| 79 |
environment: "{{test2}}" |
| 80 |
|
| 81 |
- name: assert all vars appear as expected |
| 82 |
assert: |
| 83 |
that: |
| 84 |
- '"val1" not in test_env4.stdout_lines' |
| 85 |
- '"not1" in test_env4.stdout_lines' |
| 86 |
- '"val2" in test_env4.stdout_lines' |
| 87 |
|
| 88 |
- block: |
| 89 |
- name: check that task envvar does exist in block |
| 90 |
shell: echo $key1; echo $other1 |
| 91 |
register: test_env5 |
| 92 |
|
| 93 |
- name: assert all vars appear as expected in block |
| 94 |
assert: |
| 95 |
that: |
| 96 |
- '"val1" not in test_env5.stdout_lines' |
| 97 |
- '"not1" in test_env5.stdout_lines' |
| 98 |
- '"val2" in test_env5.stdout_lines' |
| 99 |
environment: "{{test2}}" |
| 100 |
|
| 101 |
- name: test setting environment while using loops |
| 102 |
hosts: testhost |
| 103 |
environment: |
| 104 |
foo: outer |
| 105 |
tasks: |
| 106 |
- name: verify foo==outer |
| 107 |
command: /bin/echo $foo |
| 108 |
loop: |
| 109 |
- 1 |
| 110 |
register: test_foo |
| 111 |
|
| 112 |
- name: assert foo==outer |
| 113 |
assert: |
| 114 |
that: |
| 115 |
- test_foo.results[0].stdout == 'outer' |
| 116 |
|
| 117 |
- name: set environment on a task |
| 118 |
environment: |
| 119 |
foo: in_task |
| 120 |
command: /bin/echo $foo |
| 121 |
loop: |
| 122 |
- 1 |
| 123 |
register: test_foo |
| 124 |
|
| 125 |
- name: assert foo==in_task |
| 126 |
assert: |
| 127 |
that: |
| 128 |
- "test_foo.results[0].stdout == 'in_task'" |
| 129 |
|
| 130 |
- name: test that the outer env var is set appropriately still |
| 131 |
command: /bin/echo $foo |
| 132 |
loop: |
| 133 |
- 1 |
| 134 |
register: test_foo |
| 135 |
|
| 136 |
- name: assert foo==outer |
| 137 |
assert: |
| 138 |
that: |
| 139 |
- test_foo.results[0].stdout == 'outer' |
| 140 |
|
| 141 |
- name: set environment on a block |
| 142 |
environment: |
| 143 |
foo: in_block |
| 144 |
block: |
| 145 |
- name: test the environment is set in the block |
| 146 |
command: /bin/echo $foo |
| 147 |
loop: |
| 148 |
- 1 |
| 149 |
register: test_foo |
| 150 |
|
| 151 |
- name: assert foo==in_block |
| 152 |
assert: |
| 153 |
that: |
| 154 |
- "test_foo.results[0].stdout == 'in_block'" |
| 155 |
|
| 156 |
- name: test setting environment in a task inside a block |
| 157 |
environment: |
| 158 |
foo: in_block_in_task |
| 159 |
command: /bin/echo $foo |
| 160 |
loop: |
| 161 |
- 1 |
| 162 |
register: test_foo |
| 163 |
|
| 164 |
- name: assert foo==in_block_in_task |
| 165 |
assert: |
| 166 |
that: |
| 167 |
- "test_foo.results[0].stdout == 'in_block_in_task'" |
| 168 |
|
| 169 |
- name: test the environment var is set to the parent value |
| 170 |
command: /bin/echo $foo |
| 171 |
loop: |
| 172 |
- 1 |
| 173 |
register: test_foo |
| 174 |
|
| 175 |
- name: assert foo==in_block |
| 176 |
assert: |
| 177 |
that: |
| 178 |
- "test_foo.results[0].stdout == 'in_block'" |
| 179 |
|
| 180 |
- name: test the env var foo has the initial value |
| 181 |
command: /bin/echo $foo |
| 182 |
loop: |
| 183 |
- 1 |
| 184 |
register: test_foo |
| 185 |
|
| 186 |
- name: assert foo==outer |
| 187 |
assert: |
| 188 |
that: |
| 189 |
- test_foo.results[0].stdout == 'outer' |