From e457a51b253f5bd6f52a0ca50ab7ded832163e88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Mass=C3=A9?= Date: Wed, 27 Jun 2018 08:53:37 +0200 Subject: [PATCH] initial commit --- .gitignore | 1 + select-filter-and-search-test.yaml | 31 ++++++++++++++++ variable-order.yaml | 57 ++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 .gitignore create mode 100644 select-filter-and-search-test.yaml create mode 100644 variable-order.yaml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a8b42eb --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.retry diff --git a/select-filter-and-search-test.yaml b/select-filter-and-search-test.yaml new file mode 100644 index 0000000..b2d2e8d --- /dev/null +++ b/select-filter-and-search-test.yaml @@ -0,0 +1,31 @@ +--- + +- name: Uses the "select" filter and the "search" test to filter a list + gather_facts: no + hosts: localhost + vars: + input: + # This is a sample output of the "getent ahosts openshift39.openshift.test" command + # + # - command: openshift39.openshift.test + # register: getent + # + # => this is getent.stdout_lines + # + - 192.168.23.175 STREAM openshift39.openshift.test + - 192.168.23.175 DGRAM + - 192.168.23.175 RAW + - 192.168.23.176 STREAM + - 192.168.23.176 DGRAM + - 192.168.23.176 RAW + tasks: + - block: + - debug: + var: output + - assert: + that: + - output|length == 2 + - output|first == "192.168.23.175" + - output|last == "192.168.23.176" + vars: + output: '{{ input|select(''search'', ''\bSTREAM\b'')|map(''regex_replace'', ''^(\S+)\s+STREAM\b.*$'', ''\1'')|list }}' diff --git a/variable-order.yaml b/variable-order.yaml new file mode 100644 index 0000000..d9f9a14 --- /dev/null +++ b/variable-order.yaml @@ -0,0 +1,57 @@ +--- + +- name: Show how ansible defer variable evaluation to the last moment + gather_facts: no + hosts: localhost + tasks: + - name: Shows usage of interdependant variables + block: + - debug: + msg: '{{ abcd }}' + - name: Unit tests + assert: + that: + - abcd == "aadd" + vars: + a: "a" + b: '{{ a }}' + ab: '{{ a }}{{ b }}' + abcd: '{{ ab }}{{ cd }}' + cd: '{{ c }}{{ d }}' + c: '{{ d }}' + d: "d" + + - name: Create a temporary directory + tempfile: + state: directory + register: tempfile + + - name: Shows that variables are evaluated when used and not when assigned + block: + - name: Nope, the file does not exists... + set_fact: + test_output1: '{{ test_variable }}' + register: task1 + ignore_errors: true + + - name: Create the file + file: + path: '{{ test_file }}' + state: touch + + - name: Now, it should be fine... + set_fact: + test_output2: '{{ test_variable }}' + register: task2 + ignore_errors: true + + - name: Unit tests + assert: + that: + - task1 is failed + - task2 is not failed + - test_output1 is not defined + - test_output2 is defined + vars: + test_file: '{{ tempfile.path }}/i-am-here' + test_variable: '{{ lookup(''file'', test_file) }}'