2014-02-21 17:34:06 +01:00
|
|
|
# test code for lookup plugins
|
|
|
|
# (c) 2014, James Tanner <tanner.jc@gmail.com>
|
|
|
|
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# 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
|
|
|
|
set_fact:
|
|
|
|
foo: "{{ lookup('file', output_dir + '/foo.txt' ) }}"
|
|
|
|
|
|
|
|
- debug: var=foo
|
|
|
|
|
|
|
|
- name: verify file lookup
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "foo == 'bar'"
|
|
|
|
|
|
|
|
|
|
|
|
# PASSWORD LOOKUP
|
|
|
|
|
|
|
|
- name: remove previous password files
|
2014-08-19 18:30:04 +02:00
|
|
|
file: dest={{output_dir}}/lookup/password state=absent
|
|
|
|
with_items:
|
|
|
|
- "{{output_dir}}/lookup/password"
|
|
|
|
- "{{output_dir}}/lookup"
|
2014-02-21 17:34:06 +01:00
|
|
|
|
|
|
|
- name: create a password file
|
|
|
|
set_fact:
|
2014-08-19 18:30:04 +02: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 17:34:06 +01:00
|
|
|
|
|
|
|
- name: get password length
|
2014-08-19 18:30:04 +02:00
|
|
|
shell: wc -c {{output_dir}}/lookup/password | awk '{print $1}'
|
2014-02-21 17:34:06 +01:00
|
|
|
register: wc_result
|
|
|
|
|
|
|
|
- debug: var=wc_result.stdout
|
|
|
|
|
|
|
|
- name: read password
|
2014-08-19 18:30:04 +02:00
|
|
|
shell: cat {{output_dir}}/lookup/password
|
2014-02-21 17:34:06 +01:00
|
|
|
register: cat_result
|
|
|
|
|
|
|
|
- debug: var=cat_result.stdout
|
|
|
|
|
|
|
|
- name: verify password
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "wc_result.stdout == '9'"
|
|
|
|
- "cat_result.stdout == newpass"
|
|
|
|
|
2014-12-09 17:31:21 +01:00
|
|
|
# ENV LOOKUP
|
2014-02-21 17:34:06 +01:00
|
|
|
|
|
|
|
- name: get first environment var name
|
|
|
|
shell: env | head -n1 | cut -d\= -f1
|
|
|
|
register: known_var_name
|
|
|
|
|
|
|
|
- name: get first environment var value
|
|
|
|
shell: echo {{ '$' + known_var_name.stdout }}
|
|
|
|
register: known_var_value
|
|
|
|
|
|
|
|
- name: use env lookup to get known var
|
2014-12-09 17:31:21 +01:00
|
|
|
set_fact:
|
2014-02-21 17:34:06 +01:00
|
|
|
test_val: "{{ lookup('env', known_var_name.stdout) }}"
|
|
|
|
|
|
|
|
- debug: var=known_var_name.stdout
|
|
|
|
- debug: var=known_var_value.stdout
|
2014-12-09 17:31:21 +01:00
|
|
|
- debug: var=test_val
|
2014-02-21 17:34:06 +01:00
|
|
|
|
|
|
|
- name: compare values
|
|
|
|
assert:
|
2014-12-09 17:31:21 +01:00
|
|
|
that:
|
2014-02-21 17:34:06 +01:00
|
|
|
- "test_val == known_var_value.stdout"
|
2014-03-18 17:29:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
# PIPE LOOKUP
|
|
|
|
|
|
|
|
# https://github.com/ansible/ansible/issues/6550
|
|
|
|
- name: confirm pipe lookup works with a single positional arg
|
2014-12-09 17:31:21 +01:00
|
|
|
debug: msg="{{ lookup('pipe', 'ls') }}"
|
2014-03-18 17:29:54 +01:00
|
|
|
|
|
|
|
|
2014-12-09 17:31:21 +01:00
|
|
|
# LOOKUP TEMPLATING
|
|
|
|
|
|
|
|
- name: use bare interpolation
|
|
|
|
debug: msg="got {{item}}"
|
|
|
|
with_items: things1
|
|
|
|
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 17:29:54 +01:00
|
|
|
|
2015-06-11 19:44:31 +02:00
|
|
|
- name: use list with bare strings in it
|
|
|
|
debug: msg={{item}}
|
|
|
|
with_items:
|
|
|
|
- things2
|
|
|
|
- things1
|
|
|
|
|
2014-12-09 17:31:21 +01:00
|
|
|
- name: use list with undefined var in it
|
|
|
|
debug: msg={{item}}
|
|
|
|
with_items: things2
|
2015-06-11 19:44:31 +02:00
|
|
|
ignore_errors: True
|
2014-03-18 17:29:54 +01:00
|
|
|
|
2015-05-12 18:43:16 +02: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 23:12:18 +02:00
|
|
|
|
|
|
|
- name: set with_dict
|
|
|
|
shell: echo "{{ item.key + '=' + item.value }}"
|
|
|
|
with_dict: "{{ mydict }}"
|
2015-06-16 01:41:57 +02: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:
|
|
|
|
web_data: "{{ lookup('url', 'https://kennethreitz.org/') }}"
|
|
|
|
ignore_errors: True
|
|
|
|
register: url_invalid_cert
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- "url_invalid_cert.failed"
|
|
|
|
- "'Error validating the server' in url_invalid_cert.msg"
|
|
|
|
|
|
|
|
- name: Test that retrieving a url with invalid cert with validate_certs=False works
|
|
|
|
set_fact:
|
|
|
|
web_data: "{{ lookup('url', 'https://kennethreitz.org/', validate_certs=False) }}"
|
|
|
|
register: url_no_validate_cert
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- "'kennethreitz.org' in web_data"
|