Split integration tests out from Makefile. (#17976)

This commit is contained in:
Matt Clay 2016-10-12 14:57:53 -07:00 committed by GitHub
commit 80a5c70ad7
169 changed files with 612 additions and 420 deletions

View file

@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -eu
echo "arg[#]: $#"
echo "arg[0]: $0"
i=0
for arg in "$@"; do
i=$((i+1))
echo "arg[$i]: ${arg}"
done

View file

@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -eux
# Verify that extra data before module JSON output during async call is ignored.
ANSIBLE_DEBUG=0 LC_ALL=bogus ansible-playbook test_async.yml -i localhost, -e ansible_connection=ssh -v "$@"
# Verify that the warning exists by examining debug output.
ANSIBLE_DEBUG=1 LC_ALL=bogus ansible-playbook test_async.yml -i localhost, -e ansible_connection=ssh -v "$@" \
| grep 'bash: warning: setlocale: LC_ALL: cannot change locale (bogus)' > /dev/null

View file

@ -0,0 +1,10 @@
- hosts: localhost
gather_facts: false
tasks:
# make sure non-JSON data before module output is ignored
- name: async ping with invalid locale via ssh
ping:
async: 10
poll: 1
register: result
- debug: var=result

View file

@ -0,0 +1,14 @@
.PHONY: all clean
all:
# Compiled versions of these binary modules are available at the url below.
# This avoids a dependency on go and keeps the binaries out of our git repository.
# https://ansible-ci-files.s3.amazonaws.com/test/integration/roles/test_binary_modules/
cd library; \
GOOS=linux GOARCH=amd64 go build -o helloworld_linux helloworld.go; \
GOOS=windows GOARCH=amd64 go build -o helloworld_win32nt.exe helloworld.go; \
GOOS=darwin GOARCH=amd64 go build -o helloworld_darwin helloworld.go; \
GOOS=freebsd GOARCH=amd64 go build -o helloworld_freebsd helloworld.go
clean:
rm -f library/helloworld_*

View file

@ -0,0 +1,21 @@
- hosts: testhost_binary_modules
tasks:
- debug: var=ansible_system
- name: set module filename (POSIX)
set_fact:
module_filename: "helloworld_{{ ansible_system | lower }}"
when: ansible_system != 'Win32NT'
- name: set module filename (Win32NT)
set_fact:
module_filename: "helloworld_{{ ansible_system | lower }}.exe"
when: ansible_system == 'Win32NT'
- name: download binary module
tags: test_binary_modules
local_action:
module: get_url
url: "https://ansible-ci-files.s3.amazonaws.com/test/integration/roles/test_binary_modules/{{ module_filename }}"
dest: "{{ playbook_dir }}/library/{{ module_filename }}"
mode: 0755

View file

@ -0,0 +1 @@
helloworld_*

View file

@ -0,0 +1,89 @@
// 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/>.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
type ModuleArgs struct {
Name string
}
type Response struct {
Msg string `json:"msg"`
Changed bool `json:"changed"`
Failed bool `json:"failed"`
}
func ExitJson(responseBody Response) {
returnResponse(responseBody)
}
func FailJson(responseBody Response) {
responseBody.Failed = true
returnResponse(responseBody)
}
func returnResponse(responseBody Response) {
var response []byte
var err error
response, err = json.Marshal(responseBody)
if err != nil {
response, _ = json.Marshal(Response{Msg: "Invalid response object"})
}
fmt.Println(string(response))
if responseBody.Failed {
os.Exit(1)
} else {
os.Exit(0)
}
}
func main() {
var response Response
if len(os.Args) != 2 {
response.Msg = "No argument file provided"
FailJson(response)
}
argsFile := os.Args[1]
text, err := ioutil.ReadFile(argsFile)
if err != nil {
response.Msg = "Could not read configuration file: " + argsFile
FailJson(response)
}
var moduleArgs ModuleArgs
err = json.Unmarshal(text, &moduleArgs)
if err != nil {
response.Msg = "Configuration file not valid JSON: " + argsFile
FailJson(response)
}
var name string = "World"
if moduleArgs.Name != "" {
name = moduleArgs.Name
}
response.Msg = "Hello, " + name + "!"
ExitJson(response)
}

View file

@ -0,0 +1,54 @@
- debug: var=ansible_system
- name: ping
ping:
when: ansible_system != 'Win32NT'
- name: win_ping
win_ping:
when: ansible_system == 'Win32NT'
- name: Hello, World!
action: "helloworld_{{ ansible_system|lower }}"
register: hello_world
- assert:
that:
- 'hello_world.msg == "Hello, World!"'
- name: Hello, Ansible!
action: "helloworld_{{ ansible_system|lower }}"
args:
name: Ansible
register: hello_ansible
- assert:
that:
- 'hello_ansible.msg == "Hello, Ansible!"'
- name: Async Hello, World!
action: "helloworld_{{ ansible_system|lower }}"
async: 10
poll: 1
when: ansible_system != 'Win32NT'
register: async_hello_world
- assert:
that:
- 'async_hello_world.msg == "Hello, World!"'
when: not async_hello_world|skipped
- name: Async Hello, Ansible!
action: "helloworld_{{ ansible_system|lower }}"
args:
name: Ansible
async: 10
poll: 1
when: ansible_system != 'Win32NT'
register: async_hello_ansible
- assert:
that:
- 'async_hello_ansible.msg == "Hello, Ansible!"'
when: not async_hello_ansible|skipped

View file

@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -eux
[ -f "${INVENTORY}" ]
ANSIBLE_HOST_KEY_CHECKING=false ansible-playbook download_binary_modules.yml -i "${INVENTORY}" -v "$@"
ANSIBLE_HOST_KEY_CHECKING=false ansible-playbook test_binary_modules.yml -i "${INVENTORY}" -v "$@"

View file

@ -0,0 +1,6 @@
- hosts: testhost_binary_modules
roles:
- role: test_binary_modules
tags:
- test_binary_modules

View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
set -eux
cd ../binary_modules
INVENTORY=../../inventory ./test.sh "$@"

View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
set -eux
cd ../binary_modules
INVENTORY=../../inventory.winrm ./test.sh "$@"

View file

@ -0,0 +1,2 @@
- name: EXPECTED FAILURE
fail: msg="{{msg}}"

View file

@ -0,0 +1,103 @@
- name: simple block test
hosts: localhost
gather_facts: yes
strategy: "{{test_strategy|default('linear')}}"
vars:
block_tasks_run: false
block_rescue_run: false
block_always_run: false
nested_block_always_run: false
tasks_run_after_failure: false
rescue_run_after_failure: false
always_run_after_failure: false
tasks:
- block:
- name: set block tasks run flag
set_fact:
block_tasks_run: true
- name: EXPECTED FAILURE fail in tasks
fail:
- name: tasks flag should not be set after failure
set_fact:
tasks_run_after_failure: true
rescue:
- name: set block rescue run flag
set_fact:
block_rescue_run: true
- name: EXPECTED FAILURE fail in rescue
fail:
- name: tasks flag should not be set after failure in rescue
set_fact:
rescue_run_after_failure: true
always:
- name: set block always run flag
set_fact:
block_always_run: true
#- block:
# - meta: noop
# always:
# - name: set nested block always run flag
# set_fact:
# nested_block_always_run: true
# - name: fail in always
# fail:
# - name: tasks flag should not be set after failure in always
# set_fact:
# always_run_after_failure: true
- meta: clear_host_errors
post_tasks:
- assert:
that:
- block_tasks_run
- block_rescue_run
- block_always_run
#- nested_block_always_run
- not tasks_run_after_failure
- not rescue_run_after_failure
- not always_run_after_failure
- debug: msg="TEST COMPLETE"
- name: block with includes
hosts: localhost
gather_facts: yes
strategy: "{{test_strategy|default('linear')}}"
vars:
rescue_run_after_include_fail: false
always_run_after_include_fail_in_rescue: false
tasks_run_after_failure: false
rescue_run_after_failure: false
always_run_after_failure: false
tasks:
- block:
- name: include fail.yml in tasks
include: fail.yml
args:
msg: "failed from tasks"
- name: tasks flag should not be set after failure
set_fact:
tasks_run_after_failure: true
rescue:
- set_fact:
rescue_run_after_include_fail: true
- name: include fail.yml in rescue
include: fail.yml
args:
msg: "failed from rescue"
- name: flag should not be set after failure in rescue
set_fact:
rescue_run_after_failure: true
always:
- set_fact:
always_run_after_include_fail_in_rescue: true
- meta: clear_host_errors
post_tasks:
- assert:
that:
- rescue_run_after_include_fail
- always_run_after_include_fail_in_rescue
- not tasks_run_after_failure
- not rescue_run_after_failure
- not always_run_after_failure
- debug: msg="TEST COMPLETE"

View file

@ -0,0 +1,3 @@
- include: fail.yml
args:
msg: "nested {{msg}}"

View file

@ -0,0 +1,3 @@
- include: nested_fail.yml
args:
msg: "nested {{msg}}"

View file

@ -0,0 +1,20 @@
#!/usr/bin/env bash
set -eux
# remove old output log
rm -f block_test.out
# run the test and check to make sure the right number of completions was logged
ansible-playbook -vv main.yml -i ../../inventory "$@" | tee block_test.out
env python -c \
'import sys, re; sys.stdout.write(re.sub("\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]", "", sys.stdin.read()))' \
<block_test.out >block_test_wo_colors.out
[ "$(grep -c 'TEST COMPLETE' block_test.out)" = "$(egrep '^[0-9]+ plays in' block_test_wo_colors.out | cut -f1 -d' ')" ]
# cleanup the output log again, to make sure the test is clean
rm -f block_test.out block_test_wo_colors.out
# run test with free strategy and again count the completions
ansible-playbook -vv main.yml -i ../../inventory -e test_strategy=free "$@" | tee block_test.out
env python -c \
'import sys, re; sys.stdout.write(re.sub("\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]", "", sys.stdin.read()))' \
<block_test.out >block_test_wo_colors.out
[ "$(grep -c 'TEST COMPLETE' block_test.out)" = "$(egrep '^[0-9]+ plays in' block_test_wo_colors.out | cut -f1 -d' ')" ]

View file

@ -0,0 +1,6 @@
- hosts: testhost
vars:
- output_dir: .
roles:
- { role: test_always_run, tags: test_always_run }
- { role: test_check_mode, tags: test_check_mode }

View file

@ -0,0 +1,18 @@
# test code for the always_run option
# (c) 2014, James Cammarata <jcammarata@ansible.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/>.

View file

@ -0,0 +1,29 @@
# test code for the always_run option
# (c) 2014, James Cammarata <jcammarata@ansible.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/>.
- name: run a command while in check mode
shell: echo "running"
check_mode: no
register: result
- name: assert that the command was run
assert:
that:
- "result.changed == true"
- "result.stdout == 'running'"
- "result.rc == 0"

View file

@ -0,0 +1 @@
templated_var_loaded

View file

@ -0,0 +1,50 @@
# test code for the template module
# (c) 2014, Michael DeHaan <michael.dehaan@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/>.
- name: fill in a basic template in check mode
template: src=foo.j2 dest={{output_dir}}/checkmode_foo.templated mode=0644
register: template_result
- name: check whether file exists
stat: path={{output_dir}}/checkmode_foo.templated
register: foo
- name: verify that the file was marked as changed in check mode
assert:
that:
- "template_result|changed"
- "not foo.stat.exists"
- name: Actually create the file, disable check mode
template: src=foo.j2 dest={{output_dir}}/checkmode_foo.templated2 mode=0644
check_mode: no
register: checkmode_disabled
- name: fill in template with new content
template: src=foo.j2 dest={{output_dir}}/checkmode_foo.templated2 mode=0644
register: template_result2
- name: remove templated file
file: path={{output_dir}}/checkmode_foo.templated2 state=absent
check_mode: no
- name: verify that the file was not changed
assert:
that:
- "checkmode_disabled|changed"
- "not template_result2|changed"

View file

@ -0,0 +1 @@
{{ templated_var }}

View file

@ -0,0 +1 @@
templated_var: templated_var_loaded

View file

@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -eux
ansible-playbook check_mode.yml -i ../../inventory -v --check "$@"

View file

@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -eux
[ -f "${INVENTORY}" ]
# Run connection tests with both the default and C locale.
ansible-playbook test_connection.yml -i "${INVENTORY}" "$@"
LC_ALL=C LANG=C ansible-playbook test_connection.yml -i "${INVENTORY}" "$@"

View file

@ -0,0 +1,40 @@
- hosts: "{{ hosts }}"
gather_facts: no
serial: 1
tasks:
### raw with unicode arg and output
- name: raw with unicode arg and output
raw: echo 汉语
register: command
- name: check output of raw with unicode arg and output
assert:
that:
- "'汉语' in command.stdout"
- command | changed # as of 2.2, raw should default to changed: true for consistency w/ shell/command/script modules
### copy local file with unicode filename and content
- name: create local file with unicode filename and content
local_action: lineinfile dest={{ local_tmp }}-汉语/汉语.txt create=true line=汉语
- name: remove remote file with unicode filename and content
action: "{{ action_prefix }}file path={{ remote_tmp }}-汉语/汉语.txt state=absent"
- name: create remote directory with unicode name
action: "{{ action_prefix }}file path={{ remote_tmp }}-汉语 state=directory"
- name: copy local file with unicode filename and content
action: "{{ action_prefix }}copy src={{ local_tmp }}-汉语/汉语.txt dest={{ remote_tmp }}-汉语/汉语.txt"
### fetch remote file with unicode filename and content
- name: remove local file with unicode filename and content
local_action: file path={{ local_tmp }}-汉语/汉语.txt state=absent
- name: fetch remote file with unicode filename and content
fetch: src={{ remote_tmp }}-汉语/汉语.txt dest={{ local_tmp }}-汉语/汉语.txt fail_on_missing=true validate_checksum=true flat=true
### remove local and remote temp files
- name: remove local temp file
local_action: file path={{ local_tmp }}-汉语 state=absent
- name: remove remote temp file
action: "{{ action_prefix }}file path={{ remote_tmp }}-汉语 state=absent"

View file

@ -0,0 +1 @@
../connection_posix/test.sh

View file

@ -0,0 +1,6 @@
[chroot]
chroot-pipelining ansible_ssh_pipelining=true
chroot-no-pipelining ansible_ssh_pipelining=false
[chroot:vars]
ansible_host=/
ansible_connection=chroot

View file

@ -0,0 +1 @@
../connection_posix/test.sh

View file

@ -0,0 +1,6 @@
[docker]
docker-pipelining ansible_ssh_pipelining=true
docker-no-pipelining ansible_ssh_pipelining=false
[docker:vars]
ansible_host=ubuntu-latest
ansible_connection=docker

View file

@ -0,0 +1 @@
../connection_posix/test.sh

View file

@ -0,0 +1,7 @@
[jail]
jail-pipelining ansible_ssh_pipelining=true
jail-no-pipelining ansible_ssh_pipelining=false
[jail:vars]
ansible_host=freebsd_10_2
ansible_connection=jail
ansible_python_interpreter=/usr/local/bin/python

View file

@ -0,0 +1 @@
../connection_posix/test.sh

View file

@ -0,0 +1,6 @@
[libvirt_lxc]
libvirt_lxc-pipelining ansible_ssh_pipelining=true
libvirt_lxc-no-pipelining ansible_ssh_pipelining=false
[libvirt_lxc:vars]
ansible_host=lv-ubuntu-wily-amd64
ansible_connection=libvirt_lxc

View file

@ -0,0 +1 @@
../connection_posix/test.sh

View file

@ -0,0 +1,6 @@
[local]
local-pipelining ansible_ssh_pipelining=true
local-no-pipelining ansible_ssh_pipelining=false
[local:vars]
ansible_host=localhost
ansible_connection=local

View file

@ -0,0 +1 @@
../connection_posix/test.sh

View file

@ -0,0 +1,17 @@
[lxc]
lxc-pipelining ansible_ssh_pipelining=true
lxc-no-pipelining ansible_ssh_pipelining=false
[lxc:vars]
# 1. install lxc
# 2. install python2-lxc
# $ pip install git+https://github.com/lxc/python2-lxc.git
# 3. create container:
# $ sudo lxc-create -t download -n centos-7-amd64 -- -d centos -r 7 -a amd64
# 4. start container:
# $ sudo lxc-start -n centos-7-amd64 -d
# 5. run test:
# $ sudo -E make test_connection_lxc
# 6. stop container
# $ sudo lxc-stop -n centos-7-amd64
ansible_host=centos-7-amd64
ansible_connection=lxc

View file

@ -0,0 +1 @@
../connection_posix/test.sh

View file

@ -0,0 +1,6 @@
[lxd]
lxd-pipelining ansible_ssh_pipelining=true
lxd-no-pipelining ansible_ssh_pipelining=false
[lxd:vars]
ansible_host=centos-7-amd64
ansible_connection=lxd

View file

@ -0,0 +1 @@
../connection_posix/test.sh

View file

@ -0,0 +1,6 @@
[paramiko_ssh]
paramiko_ssh-pipelining ansible_ssh_pipelining=true
paramiko_ssh-no-pipelining ansible_ssh_pipelining=false
[paramiko_ssh:vars]
ansible_host=localhost
ansible_connection=paramiko_ssh

View file

@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -eux
# Connection tests for POSIX platforms use this script by linking to it from the appropriate 'connection_' target dir.
# The name of the inventory group to test is extracted from the directory name following the 'connection_' prefix.
group=$(python -c \
"from os import path; print(path.basename(path.abspath(path.dirname('$0'))).replace('connection_', ''))")
cd ../connection
INVENTORY="../connection_${group}/test_connection.inventory" ./test.sh \
-e hosts="${group}" \
-e action_prefix= \
-e local_tmp=/tmp/ansible-local \
-e remote_tmp=/tmp/ansible-remote \
"$@"

View file

@ -0,0 +1 @@
../connection_posix/test.sh

View file

@ -0,0 +1,6 @@
[ssh]
ssh-pipelining ansible_ssh_pipelining=true
ssh-no-pipelining ansible_ssh_pipelining=false
[ssh:vars]
ansible_host=localhost
ansible_connection=ssh

View file

@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -eux
cd ../connection
INVENTORY=../../inventory.winrm ./test.sh \
-e hosts=winrm \
-e action_prefix=win_ \
-e local_tmp=/tmp/ansible-local \
-e remote_tmp=c:/windows/temp/ansible-remote \
"$@"

View file

@ -0,0 +1,3 @@
{{ templated_var }}
{{ templated_dict | to_nice_json }}

View file

@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -eux
ANSIBLE_HOST_KEY_CHECKING=false ansible-playbook test_delegate_to.yml -i ../../inventory -v "$@"

View file

@ -0,0 +1,56 @@
- hosts: testhost3
vars:
- template_role: ./roles/test_template
- output_dir: "{{ playbook_dir }}"
- templated_var: foo
- templated_dict: { 'hello': 'world' }
tasks:
- name: Test no delegate_to
setup:
register: setup_results
- assert:
that:
- '"127.0.0.3" in setup_results.ansible_facts.ansible_env["SSH_CONNECTION"]'
- name: Test delegate_to with host in inventory
setup:
register: setup_results
delegate_to: testhost4
- assert:
that:
- '"127.0.0.4" in setup_results.ansible_facts.ansible_env["SSH_CONNECTION"]'
- name: Test delegate_to with host not in inventory
setup:
register: setup_results
delegate_to: 127.0.0.254
- assert:
that:
- '"127.0.0.254" in setup_results.ansible_facts.ansible_env["SSH_CONNECTION"]'
#
# Smoketest some other modules do not error as a canary
#
- name: Test file works with delegate_to and a host in inventory
file: path={{ output_dir }}/foo.txt mode=0644 state=touch
delegate_to: testhost4
- name: Test file works with delegate_to and a host not in inventory
file: path={{ output_dir }}/tmp.txt mode=0644 state=touch
delegate_to: 127.0.0.254
- name: Test template works with delegate_to and a host in inventory
template: src={{ template_role }}/templates/foo.j2 dest={{ output_dir }}/foo.txt
delegate_to: testhost4
- name: Test template works with delegate_to and a host not in inventory
template: src={{ template_role }}/templates/foo.j2 dest={{ output_dir }}/foo.txt
delegate_to: 127.0.0.254
- name: remove test file
file: path={{ output_dir }}/foo.txt state=absent
- name: remove test file
file: path={{ output_dir }}/tmp.txt state=absent

View file

@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -eux
ansible-playbook test_environment.yml -i ../../inventory "$@"

View file

@ -0,0 +1,66 @@
- hosts: testhost
vars:
- test1:
key1: val1
tasks:
- name: check that envvar does not exist
shell: echo $key1
register: test_env
- name: assert no val in stdout
assert:
that:
- '"val1" not in test_env.stdout_lines'
- name: check that envvar does exist
shell: echo $key1
environment: "{{test1}}"
register: test_env2
- name: assert val1 in stdout
assert:
that:
- '"val1" in test_env2.stdout_lines'
- hosts: testhost
vars:
- test1:
key1: val1
- test2:
key1: not1
other1: val2
environment: "{{test1}}"
tasks:
- name: check that play envvar does exist
shell: echo $key1
register: test_env3
- name: assert val1 in stdout
assert:
that:
- '"val1" in test_env3.stdout_lines'
- name: check that task envvar does exist
shell: echo $key1; echo $other1
register: test_env4
environment: "{{test2}}"
- name: assert all vars appear as expected
assert:
that:
- '"val1" not in test_env4.stdout_lines'
- '"not1" in test_env4.stdout_lines'
- '"val2" in test_env4.stdout_lines'
- block:
- name: check that task envvar does exist in block
shell: echo $key1; echo $other1
register: test_env5
- name: assert all vars appear as expected in block
assert:
that:
- '"val1" not in test_env5.stdout_lines'
- '"not1" in test_env5.stdout_lines'
- '"val2" in test_env5.stdout_lines'
environment: "{{test2}}"

View file

@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -eux
ansible-playbook test_gathering_facts.yml -i ../../inventory -v "$@"

View file

@ -0,0 +1,133 @@
---
- hosts: facthost0
tags: [ 'fact_min' ]
connection: local
gather_subset: "all"
gather_facts: yes
tasks:
- setup:
register: facts
- debug: var=facts
- name: Test that retrieving all facts works
assert:
that:
- '"{{ ansible_user_id|default("UNDEF_MIN") }}" != "UNDEF_MIN"'
- '"{{ ansible_interfaces|default("UNDEF_NET") }}" != "UNDEF_NET"'
- '"{{ ansible_mounts|default("UNDEF_HW") }}" != "UNDEF_HW"'
- '"{{ ansible_virtualization_role|default("UNDEF_VIRT") }}" != "UNDEF_VIRT"'
- hosts: facthost1
tags: [ 'fact_min' ]
connection: local
gather_subset: "!all"
gather_facts: yes
tasks:
- name: Test that only retrieving minimal facts work
assert:
that:
- '"{{ ansible_user_id|default("UNDEF_MIN") }}" != "UNDEF_MIN"'
- '"{{ ansible_interfaces|default("UNDEF_NET") }}" == "UNDEF_NET"'
- '"{{ ansible_mounts|default("UNDEF_HW") }}" == "UNDEF_HW"'
- '"{{ ansible_virtualization_role|default("UNDEF_VIRT") }}" == "UNDEF_VIRT"'
- hosts: facthost2
tags: [ 'fact_network' ]
connection: local
gather_subset: "network"
gather_facts: yes
tasks:
- name: Test that retrieving network facts work
assert:
that:
- '"{{ ansible_user_id|default("UNDEF_MIN") }}" != "UNDEF_MIN"'
- '"{{ ansible_interfaces|default("UNDEF_NET") }}" != "UNDEF_NET"'
- '"{{ ansible_mounts|default("UNDEF_HW") }}" == "UNDEF_HW"'
- '"{{ ansible_virtualization_role|default("UNDEF_VIRT") }}" == "UNDEF_VIRT"'
- hosts: facthost3
tags: [ 'fact_hardware' ]
connection: local
gather_subset: "hardware"
gather_facts: yes
tasks:
- name: Test that retrieving hardware facts work
assert:
that:
- '"{{ ansible_user_id|default("UNDEF_MIN") }}" != "UNDEF_MIN"'
- '"{{ ansible_interfaces|default("UNDEF_NET") }}" == "UNDEF_NET"'
- '"{{ ansible_mounts|default("UNDEF_HW") }}" != "UNDEF_HW"'
- '"{{ ansible_virtualization_role|default("UNDEF_VIRT") }}" == "UNDEF_VIRT"'
- hosts: facthost4
tags: [ 'fact_virtual' ]
connection: local
gather_subset: "virtual"
gather_facts: yes
tasks:
- name: Test that retrieving virtualization facts work
assert:
that:
- '"{{ ansible_user_id|default("UNDEF_MIN") }}" != "UNDEF_MIN"'
- '"{{ ansible_interfaces|default("UNDEF_NET") }}" == "UNDEF_NET"'
- '"{{ ansible_mounts|default("UNDEF_HW") }}" == "UNDEF_HW"'
- '"{{ ansible_virtualization_role|default("UNDEF_VIRT") }}" != "UNDEF_VIRT"'
- hosts: facthost5
tags: [ 'fact_comma_string' ]
connection: local
gather_subset: "virtual,network"
gather_facts: yes
tasks:
- name: Test that retrieving virtualization and network as a string works
assert:
that:
- '"{{ ansible_user_id|default("UNDEF_MIN") }}" != "UNDEF_MIN"'
- '"{{ ansible_interfaces|default("UNDEF_NET") }}" != "UNDEF_NET"'
- '"{{ ansible_mounts|default("UNDEF_HW") }}" == "UNDEF_HW"'
- '"{{ ansible_virtualization_role|default("UNDEF_VIRT") }}" != "UNDEF_VIRT"'
- hosts: facthost6
tags: [ 'fact_yaml_list' ]
connection: local
gather_subset:
- virtual
- network
gather_facts: yes
tasks:
- name: Test that retrieving virtualization and network as a string works
assert:
that:
- '"{{ ansible_user_id|default("UNDEF_MIN") }}" != "UNDEF_MIN"'
- '"{{ ansible_interfaces|default("UNDEF_NET") }}" != "UNDEF_NET"'
- '"{{ ansible_mounts|default("UNDEF_HW") }}" == "UNDEF_HW"'
- '"{{ ansible_virtualization_role|default("UNDEF_VIRT") }}" != "UNDEF_VIRT"'
- hosts: facthost7
tags: [ 'fact_negation' ]
connection: local
gather_subset: "!hardware"
gather_facts: yes
tasks:
- name: Test that negation of fact subsets work
assert:
that:
- '"{{ ansible_user_id|default("UNDEF_MIN") }}" != "UNDEF_MIN"'
- '"{{ ansible_interfaces|default("UNDEF_NET") }}" != "UNDEF_NET"'
- '"{{ ansible_mounts|default("UNDEF_HW") }}" == "UNDEF_HW"'
- '"{{ ansible_virtualization_role|default("UNDEF_VIRT") }}" != "UNDEF_VIRT"'
- hosts: facthost8
tags: [ 'fact_mixed_negation_addition' ]
connection: local
gather_subset: "!hardware,network"
gather_facts: yes
tasks:
- name: Test that negation and additional subsets work together
assert:
that:
- '"{{ ansible_user_id|default("UNDEF_MIN") }}" != "UNDEF_MIN"'
- '"{{ ansible_interfaces|default("UNDEF_NET") }}" != "UNDEF_NET"'
- '"{{ ansible_mounts|default("UNDEF_HW") }}" == "UNDEF_HW"'
- '"{{ ansible_virtualization_role|default("UNDEF_VIRT") }}" == "UNDEF_VIRT"'

View file

@ -0,0 +1,6 @@
[lamini]
alpaca genus=vicugna
llama genus=lama
[lamini:vars]
ansible_connection=local

View file

@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -eux
ansible-playbook test_group_by.yml -i inventory.group_by -v "$@"

View file

@ -0,0 +1,127 @@
# test code for the group_by module
# (c) 2014, Chris Church <cchurch@ansible.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/>.
- name: Create overall groups
hosts: lamini
gather_facts: false
tasks:
- debug: var=genus
- name: group by genus
group_by: key={{ genus }}
- name: group by first three letters of genus with key in quotes
group_by: key="{{ genus | truncate(3, true, '') }}"
- name: group by first two letters of genus with key not in quotes
group_by: key={{ genus | truncate(2, true, '') }}
- name: group by genus in uppercase using complex args
group_by: { key: "{{ genus | upper() }}" }
- name: Vicunga group validation
hosts: vicugna
gather_facts: false
tasks:
- name: verify that only the alpaca is in this group
assert: { that: "inventory_hostname == 'alpaca'" }
- name: set a fact to check that we ran this play
set_fact: genus_vicugna=true
- name: Lama group validation
hosts: lama
gather_facts: false
tasks:
- name: verify that only the llama is in this group
assert: { that: "inventory_hostname == 'llama'" }
- name: set a fact to check that we ran this play
set_fact: genus_lama=true
- name: Vic group validation
hosts: vic
gather_facts: false
tasks:
- name: verify that only the alpaca is in this group
assert: { that: "inventory_hostname == 'alpaca'" }
- name: set a fact to check that we ran this play
set_fact: genus_vic=true
- name: Lam group validation
hosts: lam
gather_facts: false
tasks:
- name: verify that only the llama is in this group
assert: { that: "inventory_hostname == 'llama'" }
- name: set a fact to check that we ran this play
set_fact: genus_lam=true
- name: Vi group validation
hosts: vi
gather_facts: false
tasks:
- name: verify that only the alpaca is in this group
assert: { that: "inventory_hostname == 'alpaca'" }
- name: set a fact to check that we ran this play
set_fact: genus_vi=true
- name: La group validation
hosts: la
gather_facts: false
tasks:
- name: verify that only the llama is in this group
assert: { that: "inventory_hostname == 'llama'" }
- name: set a fact to check that we ran this play
set_fact: genus_la=true
- name: VICUGNA group validation
hosts: VICUGNA
gather_facts: false
tasks:
- name: verify that only the alpaca is in this group
assert: { that: "inventory_hostname == 'alpaca'" }
- name: set a fact to check that we ran this play
set_fact: genus_VICUGNA=true
- name: LAMA group validation
hosts: LAMA
gather_facts: false
tasks:
- name: verify that only the llama is in this group
assert: { that: "inventory_hostname == 'llama'" }
- name: set a fact to check that we ran this play
set_fact: genus_LAMA=true
- name: genus group validation (expect skipped)
hosts: 'genus'
gather_facts: false
tasks:
- name: no hosts should match this group
fail: msg="should never get here"
- name: alpaca validation of groups
hosts: alpaca
gather_facts: false
tasks:
- name: check that alpaca matched all four groups
assert: { that: ["genus_vicugna", "genus_vic", "genus_vi", "genus_VICUGNA"] }
- name: llama validation of groups
hosts: llama
gather_facts: false
tasks:
- name: check that llama matched all four groups
assert: { that: ["genus_lama", "genus_lam", "genus_la", "genus_LAMA"] }

View file

@ -0,0 +1,6 @@
[testgroup]
A
B
C
D
E

View file

@ -0,0 +1,2 @@
- name: echoing handler
command: echo CALLED_HANDLER_{{ inventory_hostname }}

View file

@ -0,0 +1,26 @@
---
# We notify for A and B, and hosts B and C fail.
# When forcing, we expect A and B to run handlers
# When not forcing, we expect only B to run handlers
- name: notify the handler for host A and B
shell: echo
notify:
- echoing handler
when: inventory_hostname == 'A' or inventory_hostname == 'B'
- name: EXPECTED FAILURE fail task for all
fail: msg="Fail All"
when: fail_all is defined and fail_all
- name: EXPECTED FAILURE fail task for A
fail: msg="Fail A"
when: inventory_hostname == 'A'
- name: EXPECTED FAILURE fail task for C
fail: msg="Fail C"
when: inventory_hostname == 'C'
- name: echo after A and C have failed
command: echo CALLED_TASK_{{ inventory_hostname }}

View file

@ -0,0 +1,5 @@
- name: set handler fact
set_fact:
handler_called: True
- name: test handler
debug: msg="handler called"

View file

@ -0,0 +1,2 @@
dependencies: []

View file

@ -0,0 +1,52 @@
# test code for the async keyword
# (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/>.
- name: reset handler_called variable to false for all hosts
set_fact:
handler_called: False
tags: scenario1
- name: notify the handler for host A only
shell: echo
notify:
- set handler fact
when: inventory_hostname == 'A'
tags: scenario1
- name: force handler execution now
meta: "flush_handlers"
tags: scenario1
- debug: var=handler_called
tags: scenario1
- name: validate the handler only ran on one host
assert:
that:
- "inventory_hostname == 'A' and handler_called == True or handler_called == False"
tags: scenario1
- name: 'test notify with loop'
debug: msg='a task'
changed_when: item == 1
notify: test handler
with_items:
- 1
- 2
tags: scenario2

View file

@ -0,0 +1,7 @@
- name: set_handler_fact_1
set_fact:
handler1_called: True
- name: set_handler_fact_2
set_fact:
handler2_called: True

View file

@ -0,0 +1,41 @@
# test code for the async keyword
# (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/>.
- name: notify the first handler
shell: echo
notify:
- set_handler_fact_1
- name: force handler execution now
meta: "flush_handlers"
- name: assert handler1 ran and not handler2
assert:
that:
- "handler1_called is defined"
- "handler2_called is not defined"
- name: reset handler1_called
set_fact:
handler1_called: False
- name: notify the second handler
shell: echo
notify:
- set_handler_fact_2

View file

@ -0,0 +1,36 @@
#!/usr/bin/env bash
set -eux
ansible-playbook test_handlers.yml -i inventory.handlers -v "$@" --tags scenario1
[ "$(ansible-playbook test_handlers.yml -i inventory.handlers -v "$@" --tags scenario2 -l A \
| egrep -o 'RUNNING HANDLER \[test_handlers : .*?]')" = "RUNNING HANDLER [test_handlers : test handler]" ]
# Not forcing, should only run on successful host
[ "$(ansible-playbook test_force_handlers.yml -i inventory.handlers -v "$@" --tags normal \
| egrep -o CALLED_HANDLER_. | sort | uniq | xargs)" = "CALLED_HANDLER_B" ]
# Forcing from command line
[ "$(ansible-playbook test_force_handlers.yml -i inventory.handlers -v "$@" --tags normal --force-handlers \
| egrep -o CALLED_HANDLER_. | sort | uniq | xargs)" = "CALLED_HANDLER_A CALLED_HANDLER_B" ]
# Forcing from command line, should only run later tasks on unfailed hosts
[ "$(ansible-playbook test_force_handlers.yml -i inventory.handlers -v "$@" --tags normal --force-handlers \
| egrep -o CALLED_TASK_. | sort | uniq | xargs)" = "CALLED_TASK_B CALLED_TASK_D CALLED_TASK_E" ]
# Forcing from command line, should call handlers even if all hosts fail
[ "$(ansible-playbook test_force_handlers.yml -i inventory.handlers -v "$@" --tags normal --force-handlers -e fail_all=yes \
| egrep -o CALLED_HANDLER_. | sort | uniq | xargs)" = "CALLED_HANDLER_A CALLED_HANDLER_B" ]
# Forcing from ansible.cfg
[ "$(ANSIBLE_FORCE_HANDLERS=true ansible-playbook test_force_handlers.yml -i inventory.handlers -v "$@" --tags normal \
| egrep -o CALLED_HANDLER_. | sort | uniq | xargs)" = "CALLED_HANDLER_A CALLED_HANDLER_B" ]
# Forcing true in play
[ "$(ansible-playbook test_force_handlers.yml -i inventory.handlers -v "$@" --tags force_true_in_play \
| egrep -o CALLED_HANDLER_. | sort | uniq | xargs)" = "CALLED_HANDLER_A CALLED_HANDLER_B" ]
# Forcing false in play, which overrides command line
[ "$(ansible-playbook test_force_handlers.yml -i inventory.handlers -v "$@" --tags force_false_in_play --force-handlers \
| egrep -o CALLED_HANDLER_. | sort | uniq | xargs)" = "CALLED_HANDLER_B" ]

View file

@ -0,0 +1,30 @@
---
- name: test force handlers (default)
tags: normal
hosts: testgroup
gather_facts: False
connection: local
roles:
- { role: test_force_handlers }
tasks:
- debug: msg="you should see this with --tags=normal"
- name: test force handlers (set to true)
tags: force_true_in_play
hosts: testgroup
gather_facts: False
connection: local
force_handlers: True
roles:
- { role: test_force_handlers, tags: force_true_in_play }
- name: test force handlers (set to false)
tags: force_false_in_play
hosts: testgroup
gather_facts: False
connection: local
force_handlers: False
roles:
- { role: test_force_handlers, tags: force_false_in_play }

View file

@ -0,0 +1,26 @@
---
- name: run handlers
hosts: A
gather_facts: False
connection: local
roles:
- { role: test_handlers_meta, tags: ['scenario1'] }
- name: verify final handler was run
hosts: A
gather_facts: False
connection: local
tasks:
- name: verify handler2 ran
assert:
that:
- "not hostvars[inventory_hostname]['handler1_called']"
- "'handler2_called' in hostvars[inventory_hostname]"
tags: ['scenario1']
- name: test handlers
hosts: testgroup
gather_facts: False
connection: local
roles:
- { role: test_handlers }

View file

@ -0,0 +1,21 @@
# test code for the hash variable behavior
# (c) 2014, James Cammarata <jcammarata@ansible.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/>.
---
test_hash:
default_vars: "this is in role defaults/main.yml"

View file

@ -0,0 +1,18 @@
# test code for the hash variable behavior
# (c) 2014, James Cammarata <jcammarata@ansible.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/>.

View file

@ -0,0 +1,42 @@
# test code for the hash variable behavior
# (c) 2014, James Cammarata <jcammarata@ansible.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/>.
- name: get the hash behavior env setting
shell: env | grep ^ANSIBLE_HASH_BEHAVIOUR'=' | cut -f2- -d'='
register: hash_behavior
# This only works with the local connection. The way this test is run means the
connection: local
delegate_to: localhost
- name: debug hash behavior result
debug: var=hash_behavior.stdout
- name: assert hash behavior is merge or replace
assert:
that:
- "hash_behavior.stdout in ('merge', 'replace')"
- name: debug test_hash var
debug: var=test_hash
- name: assert the dictionary values match
assert:
that:
- "hash_behavior.stdout == 'merge' and test_hash == merged_hash or hash_behavior.stdout == 'replace' and test_hash == replaced_hash"

View file

@ -0,0 +1,21 @@
# test code for the hash variable behavior
# (c) 2014, James Cammarata <jcammarata@ansible.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/>.
---
test_hash:
role_vars: "this is in role vars/main.yml"

View file

@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -eux
JSON_ARG='{"test_hash":{"extra_args":"this is an extra arg"}}'
ANSIBLE_HASH_BEHAVIOUR=replace ansible-playbook test_hash.yml -i ../../inventory -v "$@" -e "${JSON_ARG}"
ANSIBLE_HASH_BEHAVIOUR=merge ansible-playbook test_hash.yml -i ../../inventory -v "$@" -e "${JSON_ARG}"

View file

@ -0,0 +1,20 @@
---
- hosts: testhost
vars_files:
- vars/test_hash_vars.yml
vars:
test_hash:
playbook_vars: "this is a playbook variable"
replaced_hash:
extra_args: "this is an extra arg"
merged_hash:
default_vars: "this is in role defaults/main.yml"
extra_args: "this is an extra arg"
group_vars_all: "this is in group_vars/all"
host_vars_testhost: "this is in host_vars/testhost"
playbook_vars: "this is a playbook variable"
role_argument: "this is a role argument variable"
role_vars: "this is in role vars/main.yml"
vars_file: "this is in a vars_file"
roles:
- { role: test_hash_behavior, test_hash: {'role_argument':'this is a role argument variable'} }

View file

@ -0,0 +1,3 @@
---
test_hash:
vars_file: "this is in a vars_file"

View file

@ -0,0 +1 @@
42 ansible_host=127.0.0.42 ansible_connection=local

View file

@ -0,0 +1,49 @@
#!/usr/bin/env bash
set -eux
# Hosts in playbook has a list of strings consisting solely of digits
ansible-playbook test_hosts_field.yml -i inventory.hosts_field -e 'target_kv=42' \
-e '{ "target_json_cli": "42", "target_json_cli_list": ["42", "localhost"] }' -e "@test_hosts_field.json" \
-t string_digit_host_in_list -v "$@" | tee test_hosts_field.out
grep 'Running on 42' test_hosts_field.out 2>&1
test "$(grep -c 'ok=1' test_hosts_field.out)" = 1
# Hosts taken from kv extra_var on the CLI
ansible-playbook test_hosts_field.yml -i inventory.hosts_field -e 'target_kv=42' \
-e '{ "target_json_cli": "42", "target_json_cli_list": ["42", "localhost"] }' -e "@test_hosts_field.json" \
-t hosts_from_kv_string -v "$@" | tee test_hosts_field.out
grep 'Running on 42' test_hosts_field.out 2>&1
test "$(grep -c 'ok=1' test_hosts_field.out)" = 1
# hosts is taken from an all digit json extra_vars string on the CLI
ansible-playbook test_hosts_field.yml -i inventory.hosts_field -e 'target_kv=42' \
-e '{ "target_json_cli": "42", "target_json_cli_list": ["42", "localhost"] }' -e "@test_hosts_field.json" \
-t hosts_from_cli_json_string -v "$@" | tee test_hosts_field.out
grep 'Running on 42' test_hosts_field.out 2>&1
test "$(grep -c 'ok=1' test_hosts_field.out)" = 1
# hosts is taken from a json list in extra_vars on the CLI
ansible-playbook test_hosts_field.yml -i inventory.hosts_field -e 'target_kv=42' \
-e '{ "target_json_cli": "42", "target_json_cli_list": ["42", "localhost"] }' -e "@test_hosts_field.json" \
-t hosts_from_cli_json_list -v "$@" | tee test_hosts_field.out
grep 'Running on 42' test_hosts_field.out 2>&1
grep 'Running on localhost' test_hosts_field.out 2>&1
test "$(grep -c 'ok=1' test_hosts_field.out)" = 2
# hosts is taken from a json string in an extra_vars file
ansible-playbook test_hosts_field.yml -i inventory.hosts_field -e 'target_kv=42' \
-e '{ "target_json_cli": "42", "target_json_cli_list": ["42", "localhost"] }' -e "@test_hosts_field.json" \
-t hosts_from_json_file_string -v "$@" | tee test_hosts_field.out
grep 'Running on 42' test_hosts_field.out 2>&1
test "$(grep -c 'ok=1' test_hosts_field.out)" = 1
# hosts is taken from a json list in an extra_vars file
ansible-playbook test_hosts_field.yml -i inventory.hosts_field -e 'target_kv=42' \
-e '{ "target_json_cli": "42", "target_json_cli_list": ["42", "localhost"] }' -e "@test_hosts_field.json" \
-t hosts_from_json_file_list -v "$@" | tee test_hosts_field.out
grep 'Running on 42' test_hosts_field.out 2>&1
grep 'Running on localhost' test_hosts_field.out 2>&1
test "$(grep -c 'ok=1' test_hosts_field.out)" = 2
rm test_hosts_field.out

View file

@ -0,0 +1 @@
{ "target_json_file": "42", "target_json_file_list": ["42", "localhost"] }

View file

@ -0,0 +1,62 @@
---
#- name: Host in playbook is an integer
# hosts: 42
# tags: numeric_host
# tasks:
# - command: echo 'Running on {{ inventory_hostname }}'
#- name: Host in playbook is a string of digits
# hosts: "42"
# tags: string_digit_host
# tasks:
# - command: echo 'Running on {{ inventory_hostname }}'
#- name: Host in playbook is a list of integer
# hosts:
# - 42
# tags: numeric_host_in_list
# tasks:
# - command: echo 'Running on {{ inventory_hostname }}'
- name: Host in playbook is a list of strings of digits
hosts:
- "42"
gather_facts: False
tags: string_digit_host_in_list
tasks:
- command: echo 'Running on {{ inventory_hostname }}'
- name: Hosts taken from kv extra_var on the CLI
hosts: "{{ target_kv }}"
gather_facts: False
tags: hosts_from_kv_string
tasks:
- command: echo 'Running on {{ inventory_hostname }}'
- name: Hosts taken from a json string on the CLI
hosts: "{{ target_json_cli }}"
gather_facts: False
tags: hosts_from_cli_json_string
tasks:
- command: echo 'Running on {{ inventory_hostname }}'
- name: Hosts taken from a json list on the CLI
hosts: "{{ target_json_cli_list }}"
gather_facts: False
tags: hosts_from_cli_json_list
tasks:
- command: echo 'Running on {{ inventory_hostname }}'
- name: Hosts is taken from a json string in an extra_vars file
hosts: "{{ target_json_file }}"
gather_facts: False
tags: hosts_from_json_file_string
tasks:
- command: echo 'Running on {{ inventory_hostname }}'
- name: Hosts is taken from a json list in an extra_vars file
hosts: "{{ target_json_file_list }}"
gather_facts: False
tags: hosts_from_json_file_list
tasks:
- command: echo 'Running on {{ inventory_hostname }}'

View file

@ -0,0 +1,2 @@
- include: more_handlers.yml

View file

@ -0,0 +1,14 @@
- name: included_handler
set_fact:
ca: 4001
cb: 4002
cc: 4003
- name: verify_handler
assert:
that:
- "ca == 4001"
- "cb == 4002"
- "cc == 4003"

View file

@ -0,0 +1,10 @@
- set_fact:
ca: "{{ a }}"
- debug: var=ca
- set_fact:
cb: "{{b}}"
- debug: var=cb
- set_fact:
cc: "{{ c }}"
- debug: var=cc

View file

@ -0,0 +1,84 @@
# test code for the ping module
# (c) 2014, James Cammarata <jcammarata@ansible.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/>.
- include: included_task1.yml a=1 b=2 c=3
- name: verify non-variable include params
assert:
that:
- "ca == '1'"
- "cb == '2'"
- "cc == '3'"
- set_fact:
a: 101
b: 102
c: 103
- include: included_task1.yml a={{a}} b={{b}} c=103
- name: verify variable include params
assert:
that:
- "ca == 101"
- "cb == 102"
- "cc == 103"
# Test that strings are not turned into numbers
- set_fact:
a: "101"
b: "102"
c: "103"
- include: included_task1.yml a={{a}} b={{b}} c=103
- name: verify variable include params
assert:
that:
- "ca == '101'"
- "cb == '102'"
- "cc == '103'"
# now try long form includes
- include: included_task1.yml
vars:
a: 201
b: 202
c: 203
- debug: var=a
- debug: var=b
- debug: var=c
- name: verify long-form include params
assert:
that:
- "ca == 201"
- "cb == 202"
- "cc == 203"
- name: test handlers with includes
shell: echo 1
notify:
# both these via a handler include
- included_handler
- verify_handler

View file

@ -0,0 +1,4 @@
- set_fact:
ca: 33000
cb: 33001
cc: 33002

View file

@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -eux
ansible-playbook test_includes.yml -i ../../inventory "$@"

View file

@ -0,0 +1,3 @@
- include: test_includes2.yml parameter1=asdf parameter2=jkl
- include: test_includes3.yml

View file

@ -0,0 +1,22 @@
- name: verify playbook includes can take parameters
hosts: testhost
tasks:
- assert:
that:
- "parameter1 == 'asdf'"
- "parameter2 == 'jkl'"
- name: verify task include logic
hosts: testhost
gather_facts: True
roles:
- { role: test_includes, tags: test_includes }
tasks:
- include: roles/test_includes/tasks/not_a_role_task.yml
- include: roles/test_includes/tasks/empty.yml
- assert:
that:
- "ca == 33000"
- "cb == 33001"
- "cc == 33002"

View file

@ -0,0 +1,7 @@
---
- hosts: localhost
tasks:
- include: test_includes4.yml
with_items: ["a"]
loop_control:
loop_var: r

View file

@ -0,0 +1,2 @@
- set_fact:
p: 1

View file

@ -0,0 +1,64 @@
- name: setup state
hosts: testhost
connection: local
gather_facts: false
tasks:
- file: path={{playbook_dir}}/files state=directory
- file: path={{playbook_dir}}/roles/showfile/files state=directory
- copy: dest={{playbook_dir}}/roles/showfile/files/testfile content='in role files'
- copy: dest={{playbook_dir}}/roles/showfile/tasks/testfile content='in role tasks'
- copy: dest={{playbook_dir}}/roles/showfile/testfile content='in role'
- copy: dest={{playbook_dir}}/files/testfile content='in files'
- copy: dest={{playbook_dir}}/testfile content='in local'
- set_fact: role_out="in role files" play_out="in files" stage='setup'
- include: testplay.yml
- name: remove from role/files
hosts: testhost
connection: local
gather_facts: false
tasks:
- file: path={{playbook_dir}}/roles/showfile/files/testfile state=absent
- set_fact: role_out="in role tasks" play_out="in files" stage='remove 1'
- include: testplay.yml
- name: remove from role/tasks
hosts: testhost
connection: local
gather_facts: false
tasks:
- file: path={{playbook_dir}}/roles/showfile/tasks/testfile state=absent
- set_fact: role_out="in files" play_out="in files" stage='remote 2'
- include: testplay.yml
- name: remove from role dir
hosts: testhost
connection: local
gather_facts: false
tasks:
- file: path={{playbook_dir}}/roles/showfile/testfile state=absent
- set_fact: role_out="in files" play_out="in files" stage='remove 3'
- include: testplay.yml
- name: remove from play/files
hosts: testhost
connection: local
gather_facts: false
tasks:
- file: path={{playbook_dir}}/files/testfile state=absent
- set_fact: role_out="in local" play_out="in local" stage='remove 4'
- include: testplay.yml
- name: cleanup
hosts: testhost
connection: local
gather_facts: false
tasks:
- file: path={{playbook_dir}}/testfile state=absent
- file: path={{playbook_dir}}/files state=absent
- file: path={{playbook_dir}}/roles/showfile/files state=absent

View file

@ -0,0 +1,2 @@
- name: relative to role
set_fact: role_result="{{lookup('file', 'testfile')}}"

View file

@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -eux
ansible-playbook play.yml -i ../../inventory -v "$@"

View file

@ -0,0 +1,19 @@
- name: test initial state
hosts: testhost
connection: local
gather_facts: false
roles:
- showfile
tasks:
- name: from play
set_fact: play_result="{{lookup('file', 'testfile')}}"
- name: output output {{stage}}
debug: msg="play> {{play_out}}, role> {{role_out}}"
- name: verify that result match expected
assert:
that:
- 'play_result == play_out'
- 'role_result == role_out'

View file

@ -0,0 +1,24 @@
[global]
# A comment
value1=Text associated with value1 and global section
value2=Same for value2 and global section
value.dot=Properties with dot
field.with.space = another space
[section1]
value1=section1/value1
value2=section1/value2
[value_section]
value1=1
value2=2
value3=3
other1=4
other2=5
[other_section]
value1=1
value2=2
value3=3
other1=4
other2=5

View file

@ -0,0 +1,5 @@
# A comment
value1=Text associated with value1
value2=Same for value2
value.dot=Properties with dot
field.with.space = another space

View file

@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -eux
ansible-playbook test_lookup_properties.yml -i ../../inventory -v "$@"

View file

@ -0,0 +1,40 @@
---
- name: "Lookup test"
hosts: "localhost"
# connection: local
tasks:
- name: "read properties value"
set_fact:
test1: "{{lookup('ini', 'value1 type=properties file=lookup.properties')}}"
test2: "{{lookup('ini', 'value2 type=properties file=lookup.properties')}}"
test_dot: "{{lookup('ini', 'value.dot type=properties file=lookup.properties')}}"
field_with_space: "{{lookup('ini', 'field.with.space type=properties file=lookup.properties')}}"
- debug: var={{item}}
with_items: [ 'test1', 'test2', 'test_dot', 'field_with_space' ]
- name: "read ini value"
set_fact:
value1_global: "{{lookup('ini', 'value1 section=global file=lookup.ini')}}"
value2_global: "{{lookup('ini', 'value2 section=global file=lookup.ini')}}"
value1_section1: "{{lookup('ini', 'value1 section=section1 file=lookup.ini')}}"
- debug: var={{item}}
with_items: [ 'value1_global', 'value2_global', 'value1_section1' ]
- name: "read ini value with section and regexp"
set_fact:
value_section: "{{lookup('ini', 'value[1-2] section=value_section file=lookup.ini re=true')}}"
other_section: "{{lookup('ini', 'other[1-2] section=other_section file=lookup.ini re=true')}}"
- debug: var={{item}}
with_items: [ 'value_section', 'other_section' ]
- name: "Reading unknown value"
set_fact:
unknown: "{{lookup('ini', 'value2 default=unknown section=section1 file=lookup.ini')}}"
- debug: var=unknown
- name: "Looping over section section1"
debug: msg="{{item}}"
with_ini: value[1-2] section=section1 file=lookup.ini re=true
- name: "Looping over section value_section"
debug: msg="{{item}}"
with_ini: value[1-2] section=value_section file=lookup.ini re=true
- debug: msg="{{item}}"
with_ini: value[1-2] section=section1 file=lookup.ini re=true
register: _
- debug: var=_

View file

@ -0,0 +1,66 @@
# TODO: test against real connection plugins to ensure they're not leaking module args
- name: normal play
hosts: testhost
gather_facts: no
tasks:
- name: args should be logged in the absence of no_log
shell: echo "LOG_ME_TASK_SUCCEEDED"
- name: failed args should be logged in the absence of no_log
shell: echo "LOG_ME_TASK_FAILED"
failed_when: true
ignore_errors: true
- name: item args should be logged in the absence of no_log
shell: echo {{ item }}
with_items: [ "LOG_ME_ITEM", "LOG_ME_SKIPPED", "LOG_ME_ITEM_FAILED" ]
when: item != "LOG_ME_SKIPPED"
failed_when: item == "LOG_ME_ITEM_FAILED"
ignore_errors: true
- name: args should not be logged when task-level no_log set
shell: echo "DO_NOT_LOG_TASK_SUCCEEDED"
no_log: true
- name: failed args should not be logged when task-level no_log set
shell: echo "DO_NOT_LOG_TASK_FAILED"
no_log: true
failed_when: true
ignore_errors: true
- name: skipped task args should be suppressed with no_log
shell: echo "DO_NOT_LOG_TASK_SKIPPED"
no_log: true
when: false
- name: items args should be suppressed with no_log in every state
shell: echo {{ item }}
no_log: true
with_items: [ "DO_NOT_LOG_ITEM", "DO_NOT_LOG_ITEM_SKIPPED", "DO_NOT_LOG_ITEM_FAILED" ]
when: item != "DO_NOT_LOG_ITEM_SKIPPED"
failed_when: item == "DO_NOT_LOG_ITEM_FAILED"
ignore_errors: yes
- name: async task args should suppressed with no_log
async: 10
poll: 1
shell: echo "DO_NOT_LOG_ASYNC_TASK_SUCCEEDED"
no_log: true
- name: play-level no_log set
hosts: testhost
gather_facts: no
no_log: true
tasks:
- name: args should not be logged when play-level no_log set
shell: echo "DO_NOT_LOG_PLAY"
- name: args should not be logged when both play- and task-level no_log set
shell: echo "DO_NOT_LOG_TASK_AND_PLAY"
no_log: true
- name: args should be logged when task-level no_log overrides play-level
shell: echo "LOG_ME_OVERRIDE"
no_log: false

View file

@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -eux
# This test expects 7 loggable vars and 0 non-loggable ones.
# If either mismatches it fails, run the ansible-playbook command to debug.
[ "$(ansible-playbook no_log_local.yml -i ../../inventory -vvvvv "$@" | awk \
'BEGIN { logme = 0; nolog = 0; } /LOG_ME/ { logme += 1;} /DO_NOT_LOG/ { nolog += 1;} END { printf "%d/%d", logme, nolog; }')" = "26/0" ]

Some files were not shown because too many files have changed in this diff Show more