mirror of https://github.com/ansible/ansible.git
Allow hostvars delegation (#70331)
* ensure hostvars are available on delegation * also inventory_hostname must point to current host and not delegated one * fix get_connection since it was still mixing original host vars and delegated ones * also return connection vars for delegation and non delegation alike * add test to ensure we have expected usage when directly assigning for non delegated hostpull/70812/head
parent
06a4fc2833
commit
84adaba6f5
@ -0,0 +1,4 @@
|
|||||||
|
bugfixes:
|
||||||
|
- ensure delegated vars can resolve hostvars object and access vars from hostvars[inventory_hostname].
|
||||||
|
- fix issue with inventory_hostname and delegated host vars mixing on connection settings.
|
||||||
|
- add magic/connection vars updates from delegated host info.
|
@ -0,0 +1,76 @@
|
|||||||
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
|
from __future__ import (absolute_import, division, print_function)
|
||||||
|
__metaclass__ = type
|
||||||
|
|
||||||
|
DOCUMENTATION = '''
|
||||||
|
connection: fakelocal
|
||||||
|
short_description: dont execute anything
|
||||||
|
description:
|
||||||
|
- This connection plugin just verifies parameters passed in
|
||||||
|
author: ansible (@core)
|
||||||
|
version_added: histerical
|
||||||
|
options:
|
||||||
|
password:
|
||||||
|
description: Authentication password for the C(remote_user). Can be supplied as CLI option.
|
||||||
|
vars:
|
||||||
|
- name: ansible_password
|
||||||
|
remote_user:
|
||||||
|
description:
|
||||||
|
- User name with which to login to the remote server, normally set by the remote_user keyword.
|
||||||
|
ini:
|
||||||
|
- section: defaults
|
||||||
|
key: remote_user
|
||||||
|
vars:
|
||||||
|
- name: ansible_user
|
||||||
|
'''
|
||||||
|
|
||||||
|
from ansible.errors import AnsibleConnectionFailure
|
||||||
|
from ansible.plugins.connection import ConnectionBase
|
||||||
|
from ansible.utils.display import Display
|
||||||
|
|
||||||
|
display = Display()
|
||||||
|
|
||||||
|
|
||||||
|
class Connection(ConnectionBase):
|
||||||
|
''' Local based connections '''
|
||||||
|
|
||||||
|
transport = 'fakelocal'
|
||||||
|
has_pipelining = True
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
|
||||||
|
super(Connection, self).__init__(*args, **kwargs)
|
||||||
|
self.cwd = None
|
||||||
|
|
||||||
|
def _connect(self):
|
||||||
|
''' verify '''
|
||||||
|
|
||||||
|
if self.get_option('remote_user') == 'invaliduser' and self.get_option('password') == 'badpassword':
|
||||||
|
raise AnsibleConnectionFailure('Got invaliduser and badpassword')
|
||||||
|
|
||||||
|
if not self._connected:
|
||||||
|
display.vvv(u"ESTABLISH FAKELOCAL CONNECTION FOR USER: {0}".format(self._play_context.remote_user), host=self._play_context.remote_addr)
|
||||||
|
self._connected = True
|
||||||
|
return self
|
||||||
|
|
||||||
|
def exec_command(self, cmd, in_data=None, sudoable=True):
|
||||||
|
''' run a command on the local host '''
|
||||||
|
|
||||||
|
super(Connection, self).exec_command(cmd, in_data=in_data, sudoable=sudoable)
|
||||||
|
|
||||||
|
return 0, '{"msg": "ALL IS GOOD"}', ''
|
||||||
|
|
||||||
|
def put_file(self, in_path, out_path):
|
||||||
|
''' transfer a file from local to local '''
|
||||||
|
|
||||||
|
super(Connection, self).put_file(in_path, out_path)
|
||||||
|
|
||||||
|
def fetch_file(self, in_path, out_path):
|
||||||
|
''' fetch a file from local to local -- for compatibility '''
|
||||||
|
|
||||||
|
super(Connection, self).fetch_file(in_path, out_path)
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
''' terminate the connection; nothing to do here '''
|
||||||
|
self._connected = False
|
@ -0,0 +1,64 @@
|
|||||||
|
- name: ensure delegated host has hostvars available for resolving connection
|
||||||
|
hosts: testhost
|
||||||
|
gather_facts: false
|
||||||
|
tasks:
|
||||||
|
|
||||||
|
- name: ensure delegated host uses current host as inventory_hostname
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- inventory_hostname == ansible_delegated_vars['testhost5']['inventory_hostname']
|
||||||
|
delegate_to: testhost5
|
||||||
|
|
||||||
|
- name: Set info on inventory_hostname
|
||||||
|
set_fact:
|
||||||
|
login: invaliduser
|
||||||
|
mypass: badpassword
|
||||||
|
|
||||||
|
- name: test fakelocal
|
||||||
|
command: ls
|
||||||
|
ignore_unreachable: True
|
||||||
|
ignore_errors: True
|
||||||
|
remote_user: "{{ login }}"
|
||||||
|
vars:
|
||||||
|
ansible_password: "{{ mypass }}"
|
||||||
|
ansible_connection: fakelocal
|
||||||
|
register: badlogin
|
||||||
|
|
||||||
|
- name: ensure we skipped do to unreachable and not templating error
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- badlogin is unreachable
|
||||||
|
|
||||||
|
- name: delegate but try to use inventory_hostname data directly
|
||||||
|
command: ls
|
||||||
|
delegate_to: testhost5
|
||||||
|
ignore_unreachable: True
|
||||||
|
ignore_errors: True
|
||||||
|
remote_user: "{{ login }}"
|
||||||
|
vars:
|
||||||
|
ansible_password: "{{ mypass }}"
|
||||||
|
register: badlogin
|
||||||
|
|
||||||
|
- name: ensure we skipped do to unreachable and not templating error
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- badlogin is not unreachable
|
||||||
|
- badlogin is failed
|
||||||
|
- "'undefined' in badlogin['msg']"
|
||||||
|
|
||||||
|
- name: delegate ls to testhost5 as it uses ssh while testhost is local, but use vars from testhost
|
||||||
|
command: ls
|
||||||
|
remote_user: "{{ hostvars[inventory_hostname]['login'] }}"
|
||||||
|
delegate_to: testhost5
|
||||||
|
ignore_unreachable: True
|
||||||
|
ignore_errors: True
|
||||||
|
vars:
|
||||||
|
ansible_password: "{{ hostvars[inventory_hostname]['mypass'] }}"
|
||||||
|
register: badlogin
|
||||||
|
|
||||||
|
- name: ensure we skipped do to unreachable and not templating error
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- badlogin is unreachable
|
||||||
|
- badlogin is not failed
|
||||||
|
- "'undefined' not in badlogin['msg']"
|
Loading…
Reference in New Issue