2014-02-21 16:34:06 +00:00
|
|
|
# test code for lookup plugins
|
2018-12-11 09:52:12 +00:00
|
|
|
# Copyright: (c) 2014, James Tanner <tanner.jc@gmail.com>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2014-02-21 16:34:06 +00:00
|
|
|
|
|
|
|
# FILE LOOKUP
|
|
|
|
|
|
|
|
- name: make a new file to read
|
|
|
|
copy: dest={{output_dir}}/foo.txt mode=0644 content="bar"
|
|
|
|
|
|
|
|
- name: load the file as a fact
|
2016-04-18 14:47:38 +00:00
|
|
|
set_fact:
|
2014-02-21 16:34:06 +00:00
|
|
|
foo: "{{ lookup('file', output_dir + '/foo.txt' ) }}"
|
|
|
|
|
|
|
|
- debug: var=foo
|
|
|
|
|
2016-04-18 14:47:38 +00:00
|
|
|
- name: verify file lookup
|
|
|
|
assert:
|
2014-02-21 16:34:06 +00:00
|
|
|
that:
|
|
|
|
- "foo == 'bar'"
|
|
|
|
|
|
|
|
|
|
|
|
# PASSWORD LOOKUP
|
|
|
|
|
2016-06-27 17:44:25 +00:00
|
|
|
- name: remove previous password files and directory
|
|
|
|
file: dest={{item}} state=absent
|
2014-08-19 16:30:04 +00:00
|
|
|
with_items:
|
|
|
|
- "{{output_dir}}/lookup/password"
|
2016-06-27 17:44:25 +00:00
|
|
|
- "{{output_dir}}/lookup/password_with_salt"
|
2014-08-19 16:30:04 +00:00
|
|
|
- "{{output_dir}}/lookup"
|
2014-02-21 16:34:06 +00:00
|
|
|
|
|
|
|
- name: create a password file
|
2016-04-18 14:47:38 +00:00
|
|
|
set_fact:
|
2014-08-19 16:30:04 +00:00
|
|
|
newpass: "{{ lookup('password', output_dir + '/lookup/password length=8') }}"
|
|
|
|
|
|
|
|
- name: stat the password file directory
|
|
|
|
stat: path="{{output_dir}}/lookup"
|
|
|
|
register: result
|
|
|
|
|
|
|
|
- name: assert the directory's permissions
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- result.stat.mode == '0700'
|
|
|
|
|
|
|
|
- name: stat the password file
|
|
|
|
stat: path="{{output_dir}}/lookup/password"
|
|
|
|
register: result
|
|
|
|
|
|
|
|
- name: assert the directory's permissions
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- result.stat.mode == '0600'
|
2014-02-21 16:34:06 +00:00
|
|
|
|
|
|
|
- name: get password length
|
2014-08-19 16:30:04 +00:00
|
|
|
shell: wc -c {{output_dir}}/lookup/password | awk '{print $1}'
|
2014-02-21 16:34:06 +00:00
|
|
|
register: wc_result
|
|
|
|
|
|
|
|
- debug: var=wc_result.stdout
|
|
|
|
|
|
|
|
- name: read password
|
2014-08-19 16:30:04 +00:00
|
|
|
shell: cat {{output_dir}}/lookup/password
|
2014-02-21 16:34:06 +00:00
|
|
|
register: cat_result
|
|
|
|
|
|
|
|
- debug: var=cat_result.stdout
|
|
|
|
|
|
|
|
- name: verify password
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "wc_result.stdout == '9'"
|
|
|
|
- "cat_result.stdout == newpass"
|
2016-06-27 17:44:25 +00:00
|
|
|
- "' salt=' not in cat_result.stdout"
|
|
|
|
|
|
|
|
- name: fetch password from an existing file
|
|
|
|
set_fact:
|
|
|
|
pass2: "{{ lookup('password', output_dir + '/lookup/password length=8') }}"
|
|
|
|
|
|
|
|
- name: read password (again)
|
|
|
|
shell: cat {{output_dir}}/lookup/password
|
|
|
|
register: cat_result2
|
|
|
|
|
|
|
|
- debug: var=cat_result2.stdout
|
|
|
|
|
|
|
|
- name: verify password (again)
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "cat_result2.stdout == newpass"
|
|
|
|
- "' salt=' not in cat_result2.stdout"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- name: create a password (with salt) file
|
|
|
|
debug: msg={{ lookup('password', output_dir + '/lookup/password_with_salt encrypt=sha256_crypt') }}
|
|
|
|
|
|
|
|
- name: read password and salt
|
|
|
|
shell: cat {{output_dir}}/lookup/password_with_salt
|
|
|
|
register: cat_pass_salt
|
|
|
|
|
|
|
|
- debug: var=cat_pass_salt.stdout
|
|
|
|
|
|
|
|
- name: fetch unencrypted password
|
|
|
|
set_fact:
|
|
|
|
newpass: "{{ lookup('password', output_dir + '/lookup/password_with_salt') }}"
|
|
|
|
|
|
|
|
- debug: var=newpass
|
|
|
|
|
|
|
|
- name: verify password and salt
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "cat_pass_salt.stdout != newpass"
|
|
|
|
- "cat_pass_salt.stdout.startswith(newpass)"
|
|
|
|
- "' salt=' in cat_pass_salt.stdout"
|
|
|
|
- "' salt=' not in newpass"
|
|
|
|
|
|
|
|
|
|
|
|
- name: fetch unencrypted password (using empty encrypt parameter)
|
|
|
|
set_fact:
|
|
|
|
newpass2: "{{ lookup('password', output_dir + '/lookup/password_with_salt encrypt=') }}"
|
|
|
|
|
|
|
|
- name: verify lookup password behavior
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "newpass == newpass2"
|
|
|
|
|
2016-09-27 21:31:21 +00:00
|
|
|
- name: verify that we can generate a 1st password without writing it
|
|
|
|
set_fact:
|
|
|
|
newpass: "{{ lookup('password', '/dev/null') }}"
|
|
|
|
|
|
|
|
- name: verify that we can generate a 2nd password without writing it
|
|
|
|
set_fact:
|
|
|
|
newpass2: "{{ lookup('password', '/dev/null') }}"
|
|
|
|
|
|
|
|
- name: verify lookup password behavior with /dev/null
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "newpass != newpass2"
|
2014-02-21 16:34:06 +00:00
|
|
|
|
2014-12-09 16:31:21 +00:00
|
|
|
# ENV LOOKUP
|
2014-02-21 16:34:06 +00:00
|
|
|
|
2017-08-05 04:12:35 +00:00
|
|
|
- name: get HOME environment var value
|
|
|
|
shell: "echo $HOME"
|
|
|
|
register: home_var_value
|
2014-02-21 16:34:06 +00:00
|
|
|
|
2017-08-05 04:12:35 +00:00
|
|
|
- name: use env lookup to get HOME var
|
2014-12-09 16:31:21 +00:00
|
|
|
set_fact:
|
2017-08-05 04:12:35 +00:00
|
|
|
test_val: "{{ lookup('env', 'HOME') }}"
|
2014-02-21 16:34:06 +00:00
|
|
|
|
2017-08-05 04:12:35 +00:00
|
|
|
- debug: var=home_var_value.stdout
|
2014-12-09 16:31:21 +00:00
|
|
|
- debug: var=test_val
|
2014-02-21 16:34:06 +00:00
|
|
|
|
|
|
|
- name: compare values
|
|
|
|
assert:
|
2014-12-09 16:31:21 +00:00
|
|
|
that:
|
2017-08-05 04:12:35 +00:00
|
|
|
- "test_val == home_var_value.stdout"
|
2014-03-18 16:29:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
# PIPE LOOKUP
|
|
|
|
|
|
|
|
# https://github.com/ansible/ansible/issues/6550
|
|
|
|
- name: confirm pipe lookup works with a single positional arg
|
2014-12-09 16:31:21 +00:00
|
|
|
debug: msg="{{ lookup('pipe', 'ls') }}"
|
2014-03-18 16:29:54 +00:00
|
|
|
|
|
|
|
|
2014-12-09 16:31:21 +00:00
|
|
|
# LOOKUP TEMPLATING
|
|
|
|
|
|
|
|
- name: use bare interpolation
|
|
|
|
debug: msg="got {{item}}"
|
2016-09-15 21:41:54 +00:00
|
|
|
with_items: "{{things1}}"
|
2014-12-09 16:31:21 +00:00
|
|
|
register: bare_var
|
|
|
|
|
|
|
|
- name: verify that list was interpolated
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "bare_var.results[0].item == 1"
|
|
|
|
- "bare_var.results[1].item == 2"
|
2014-03-18 16:29:54 +00:00
|
|
|
|
2015-06-11 17:44:31 +00:00
|
|
|
- name: use list with bare strings in it
|
|
|
|
debug: msg={{item}}
|
|
|
|
with_items:
|
|
|
|
- things2
|
|
|
|
- things1
|
|
|
|
|
2014-12-09 16:31:21 +00:00
|
|
|
- name: use list with undefined var in it
|
|
|
|
debug: msg={{item}}
|
2016-09-15 21:41:54 +00:00
|
|
|
with_items: "{{things2}}"
|
2015-06-11 17:44:31 +00:00
|
|
|
ignore_errors: True
|
2014-03-18 16:29:54 +00:00
|
|
|
|
2015-05-12 16:43:16 +00:00
|
|
|
|
|
|
|
# BUG #10073 nested template handling
|
|
|
|
|
|
|
|
- name: set variable that clashes
|
|
|
|
set_fact:
|
|
|
|
LOGNAME: foobar
|
|
|
|
|
|
|
|
|
|
|
|
- name: get LOGNAME environment var value
|
|
|
|
shell: echo {{ '$LOGNAME' }}
|
|
|
|
register: known_var_value
|
|
|
|
|
|
|
|
- name: do the lookup for env LOGNAME
|
|
|
|
set_fact:
|
|
|
|
test_val: "{{ lookup('env', 'LOGNAME') }}"
|
|
|
|
|
|
|
|
- debug: var=test_val
|
|
|
|
|
|
|
|
- name: compare values
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "test_val == known_var_value.stdout"
|
|
|
|
|
2015-06-15 21:12:18 +00:00
|
|
|
|
|
|
|
- name: set with_dict
|
|
|
|
shell: echo "{{ item.key + '=' + item.value }}"
|
|
|
|
with_dict: "{{ mydict }}"
|
2015-06-15 23:41:57 +00:00
|
|
|
|
|
|
|
# URL Lookups
|
|
|
|
|
|
|
|
- name: Test that retrieving a url works
|
|
|
|
set_fact:
|
|
|
|
web_data: "{{ lookup('url', 'https://gist.githubusercontent.com/abadger/9858c22712f62a8effff/raw/43dd47ea691c90a5fa7827892c70241913351963/test') }}"
|
|
|
|
|
|
|
|
- name: Assert that the url was retrieved
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "'one' in web_data"
|
|
|
|
|
|
|
|
- name: Test that retrieving a url with invalid cert fails
|
|
|
|
set_fact:
|
2016-06-28 01:58:13 +00:00
|
|
|
web_data: "{{ lookup('url', 'https://{{ badssl_host }}/') }}"
|
2015-06-15 23:41:57 +00:00
|
|
|
ignore_errors: True
|
|
|
|
register: url_invalid_cert
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- "url_invalid_cert.failed"
|
2017-11-27 22:58:08 +00:00
|
|
|
- "'Error validating the server' in url_invalid_cert.msg or ( url_invalid_cert.msg is search('hostname .* doesn.t match .*'))"
|
2015-06-15 23:41:57 +00:00
|
|
|
|
|
|
|
- name: Test that retrieving a url with invalid cert with validate_certs=False works
|
|
|
|
set_fact:
|
2016-06-28 01:58:13 +00:00
|
|
|
web_data: "{{ lookup('url', 'https://{{ badssl_host }}/', validate_certs=False) }}"
|
2015-06-15 23:41:57 +00:00
|
|
|
register: url_no_validate_cert
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
2016-06-28 01:58:13 +00:00
|
|
|
- "'{{ badssl_host_substring }}' in web_data"
|
2016-02-11 15:46:44 +00:00
|
|
|
|
|
|
|
- name: Test cartesian lookup
|
|
|
|
debug: var={{item}}
|
|
|
|
with_cartesian:
|
|
|
|
- ["A", "B", "C"]
|
|
|
|
- ["1", "2", "3"]
|
|
|
|
register: product
|
|
|
|
|
|
|
|
- name: Verify cartesian lookup
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- product.results[0]['item'] == ["A", "1"]
|
|
|
|
- product.results[1]['item'] == ["A", "2"]
|
|
|
|
- product.results[2]['item'] == ["A", "3"]
|
|
|
|
- product.results[3]['item'] == ["B", "1"]
|
|
|
|
- product.results[4]['item'] == ["B", "2"]
|
|
|
|
- product.results[5]['item'] == ["B", "3"]
|
|
|
|
- product.results[6]['item'] == ["C", "1"]
|
|
|
|
- product.results[7]['item'] == ["C", "2"]
|
|
|
|
- product.results[8]['item'] == ["C", "3"]
|
2016-11-29 21:23:06 +00:00
|
|
|
|
|
|
|
# Template lookups
|
|
|
|
|
|
|
|
# ref #18526
|
|
|
|
- name: Test that we have a proper jinja search path in template lookup
|
|
|
|
set_fact:
|
|
|
|
hello_world: "{{ lookup('template', 'hello.txt') }}"
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- "hello_world|trim == 'Hello world!'"
|
2018-02-08 00:32:14 +00:00
|
|
|
|
2018-12-11 09:52:12 +00:00
|
|
|
|
|
|
|
- name: Test that we have a proper jinja search path in template lookup with different variable start and end string
|
|
|
|
vars:
|
|
|
|
my_var: world
|
|
|
|
set_fact:
|
|
|
|
hello_world_string: "{{ lookup('template', 'hello_string.txt', variable_start_string='[%', variable_end_string='%]') }}"
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- "hello_world_string|trim == 'Hello world!'"
|
|
|
|
|
2018-02-08 00:32:14 +00:00
|
|
|
# Vars lookups
|
|
|
|
|
|
|
|
- name: Test that we can give it a single value and receive a single value
|
|
|
|
set_fact:
|
|
|
|
var_host: '{{ lookup("vars", "ansible_host") }}'
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- 'var_host == ansible_host'
|
|
|
|
|
|
|
|
- name: Test that we can give a list of values to var and receive a list of values back
|
|
|
|
set_fact:
|
2018-02-21 16:25:56 +00:00
|
|
|
var_host_info: '{{ query("vars", "ansible_host", "ansible_user") }}'
|
2018-02-08 00:32:14 +00:00
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- 'var_host_info[0] == ansible_host'
|
|
|
|
- 'var_host_info[1] == ansible_user'
|