diff --git a/examples/ansible/templating.yml b/examples/ansible/templating.yml index a5c64f7..4f94cdd 100644 --- a/examples/ansible/templating.yml +++ b/examples/ansible/templating.yml @@ -2,6 +2,9 @@ - name: Show off Ansible's templating hosts: all + vars: + random_string: "" + random_integer: random(-34485, 45666) tasks: # A.K.A. ternary operator. @@ -70,12 +73,31 @@ ansible.builtin.debug: var: "'string' | type_debug" + - name: Make assertions. + ansible.builtin.assert: + that: + - 12 | type_debug == "int" + - 12 < 24 + - name: Show off Ansible's testing hosts: all vars: - url: "https://example.com/users/foo/resources/bar" + resource_url: "https://example.com/users/foo/resources/bar" + random_string: "{{ lookup('password', '/dev/null length=8 chars=ascii_letters') }}" tasks: + - name: Check a variable is a string. + ansible.builtin.debug: + var: "'{{ random_string }}' is string" + + - name: Check a value is an integer. + ansible.builtin.debug: + var: 34 is integer + + - name: Check a given URL is in fact an URL. + ansible.builtin.debug: + var: "'{{ resource_url }}' is url" + - name: Compare a semver version number. ansible.builtin.debug: var: "'2.0.0-rc.1+build.123' is version('2.1.0-rc.2+build.423', 'ge', version_type='semver')" @@ -91,10 +113,10 @@ ansible.builtin.debug: msg: "{{ item }}" with_items: - - "{{ url is match('https://example.com/users/.*/resources') }}" - - "{{ url is search('users/.*/resources/.*') }}" - - "{{ url is search('USERS', ignorecase=true) }}" - - "{{ url is regex('example\\.com/\\w+/foo') }}" + - "{{ resource_url is match('https://example.com/users/.*/resources') }}" + - "{{ resource_url is search('users/.*/resources/.*') }}" + - "{{ resource_url is search('USERS', ignorecase=true) }}" + - "{{ resource_url is regex('example\\.com/\\w+/foo') }}" - name: Show off Ansible's loops hosts: all diff --git a/knowledge base/ansible.md b/knowledge base/ansible.md index 146b049..93056bf 100644 --- a/knowledge base/ansible.md +++ b/knowledge base/ansible.md @@ -32,6 +32,7 @@ ```sh # Install. +pip3 install 'ansible' pip3 install --user 'ansible' && port install 'sshpass' # darwin sudo pamac install 'ansible' 'sshpass' # manjaro linux @@ -193,7 +194,6 @@ Return a boolean result. var: "{{ item }}" with_items: ["ansible_local", "playbook_dir", "role_path"] - # Fail when any of the given variables is an empty string. # Returns the ones which are empty. - when: lookup('vars', item) == ''