| 1 |
# test code for the add_host action |
| 2 |
# (c) 2015, Matt Davis <[email protected]> |
| 3 |
|
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
# See https://github.com/ansible/ansible/issues/36045 |
| 20 |
- set_fact: |
| 21 |
inventory_data: |
| 22 |
ansible_ssh_common_args: "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" |
| 23 |
# ansible_ssh_host: "127.0.0.3" |
| 24 |
ansible_host: "127.0.0.3" |
| 25 |
ansible_ssh_pass: "foobar" |
| 26 |
# ansible_ssh_port: "2222" |
| 27 |
ansible_port: "2222" |
| 28 |
ansible_ssh_private_key_file: "/tmp/inventory-cloudj9cGz5/identity" |
| 29 |
ansible_ssh_user: "root" |
| 30 |
hostname: "newdynamichost2" |
| 31 |
|
| 32 |
- name: Show inventory_data for 36045 |
| 33 |
debug: |
| 34 |
msg: "{{ inventory_data }}" |
| 35 |
|
| 36 |
- name: Add host from dict 36045 |
| 37 |
add_host: "{{ inventory_data }}" |
| 38 |
|
| 39 |
- name: show newly added host |
| 40 |
debug: |
| 41 |
msg: "{{hostvars['newdynamichost2'].group_names}}" |
| 42 |
|
| 43 |
- name: ensure that dynamically-added newdynamichost2 is visible via hostvars, groups 36045 |
| 44 |
assert: |
| 45 |
that: |
| 46 |
- hostvars['newdynamichost2'] is defined |
| 47 |
- hostvars['newdynamichost2'].group_names is defined |
| 48 |
|
| 49 |
# end of https://github.com/ansible/ansible/issues/36045 related tests |
| 50 |
|
| 51 |
- name: add a host to the runtime inventory |
| 52 |
add_host: |
| 53 |
name: newdynamichost |
| 54 |
groups: newdynamicgroup |
| 55 |
a_var: from add_host |
| 56 |
|
| 57 |
- debug: msg={{hostvars['newdynamichost'].group_names}} |
| 58 |
|
| 59 |
- name: ensure that dynamically-added host is visible via hostvars, groups, etc (there are several caches that could break this) |
| 60 |
assert: |
| 61 |
that: |
| 62 |
- hostvars['bogushost'] is not defined # there was a bug where an undefined host was a "type" instead of an instance- ensure this works before we rely on it |
| 63 |
- hostvars['newdynamichost'] is defined |
| 64 |
- hostvars['newdynamichost'].group_names is defined |
| 65 |
- "'newdynamicgroup' in hostvars['newdynamichost'].group_names" |
| 66 |
- hostvars['newdynamichost']['bogusvar'] is not defined |
| 67 |
- hostvars['newdynamichost']['a_var'] is defined |
| 68 |
- hostvars['newdynamichost']['a_var'] == 'from add_host' |
| 69 |
- groups['bogusgroup'] is not defined # same check as above to ensure that bogus groups are undefined... |
| 70 |
- groups['newdynamicgroup'] is defined |
| 71 |
- "'newdynamichost' in groups['newdynamicgroup']" |
| 72 |
|
| 73 |
# Tests for idempotency |
| 74 |
- name: Add testhost01 dynamic host |
| 75 |
add_host: |
| 76 |
name: testhost01 |
| 77 |
register: add_testhost01 |
| 78 |
|
| 79 |
- name: Try adding testhost01 again, with no changes |
| 80 |
add_host: |
| 81 |
name: testhost01 |
| 82 |
register: add_testhost01_idem |
| 83 |
|
| 84 |
- name: Add a host variable to testhost01 |
| 85 |
add_host: |
| 86 |
name: testhost01 |
| 87 |
foo: bar |
| 88 |
register: hostvar_testhost01 |
| 89 |
|
| 90 |
- name: Add the same host variable to testhost01, with no changes |
| 91 |
add_host: |
| 92 |
name: testhost01 |
| 93 |
foo: bar |
| 94 |
register: hostvar_testhost01_idem |
| 95 |
|
| 96 |
- name: Add another host, testhost02 |
| 97 |
add_host: |
| 98 |
name: testhost02 |
| 99 |
register: add_testhost02 |
| 100 |
|
| 101 |
- name: Add it again for good measure |
| 102 |
add_host: |
| 103 |
name: testhost02 |
| 104 |
register: add_testhost02_idem |
| 105 |
|
| 106 |
- name: Add testhost02 to a group |
| 107 |
add_host: |
| 108 |
name: testhost02 |
| 109 |
groups: |
| 110 |
- testhostgroup |
| 111 |
register: add_group_testhost02 |
| 112 |
|
| 113 |
- name: Add testhost01 to the same group |
| 114 |
add_host: |
| 115 |
name: testhost01 |
| 116 |
groups: |
| 117 |
- testhostgroup |
| 118 |
register: add_group_testhost01 |
| 119 |
|
| 120 |
- name: Add testhost02 to the group again |
| 121 |
add_host: |
| 122 |
name: testhost02 |
| 123 |
groups: |
| 124 |
- testhostgroup |
| 125 |
register: add_group_testhost02_idem |
| 126 |
|
| 127 |
- name: Add testhost01 to the group again |
| 128 |
add_host: |
| 129 |
name: testhost01 |
| 130 |
groups: |
| 131 |
- testhostgroup |
| 132 |
register: add_group_testhost01_idem |
| 133 |
|
| 134 |
- assert: |
| 135 |
that: |
| 136 |
- add_testhost01 is changed |
| 137 |
- add_testhost01_idem is not changed |
| 138 |
- hostvar_testhost01 is changed |
| 139 |
- hostvar_testhost01_idem is not changed |
| 140 |
- add_testhost02 is changed |
| 141 |
- add_testhost02_idem is not changed |
| 142 |
- add_group_testhost02 is changed |
| 143 |
- add_group_testhost01 is changed |
| 144 |
- add_group_testhost02_idem is not changed |
| 145 |
- add_group_testhost01_idem is not changed |
| 146 |
- groups['testhostgroup']|length == 2 |
| 147 |
- "'testhost01' in groups['testhostgroup']" |
| 148 |
- "'testhost02' in groups['testhostgroup']" |
| 149 |
- hostvars['testhost01']['foo'] == 'bar' |
| 150 |
|
| 151 |
- name: Give invalid input |
| 152 |
add_host: namenewdynamichost groupsnewdynamicgroup a_varfromadd_host |
| 153 |
ignore_errors: true |
| 154 |
register: badinput |
| 155 |
|
| 156 |
- name: verify we detected bad input |
| 157 |
assert: |
| 158 |
that: |
| 159 |
- badinput is failed |
| 160 |
|
| 161 |
- name: Add hosts in a loop |
| 162 |
add_host: |
| 163 |
name: 'host_{{item}}' |
| 164 |
loop: |
| 165 |
- 1 |
| 166 |
- 2 |
| 167 |
- 2 |
| 168 |
register: add_host_loop_res |
| 169 |
|
| 170 |
- name: verify correct changed results |
| 171 |
assert: |
| 172 |
that: |
| 173 |
- add_host_loop_res.results[0] is changed |
| 174 |
- add_host_loop_res.results[1] is changed |
| 175 |
- add_host_loop_res.results[2] is not changed |
| 176 |
- add_host_loop_res is changed |
| 177 |
|
| 178 |
- name: Add host with host:port in name |
| 179 |
add_host: |
| 180 |
name: '127.0.1.1:2222' |
| 181 |
register: hostport |
| 182 |
|
| 183 |
- assert: |
| 184 |
that: |
| 185 |
- hostport.add_host.host_name == '127.0.1.1' |
| 186 |
- hostport.add_host.host_vars.ansible_ssh_port == 2222 |