Skip to content

Commit f50a61f

Browse files
committed
ansible_mitogen: Templated host option (e.g. ansible_host, ansible_ssh_host)
A twist - for the connection option "host" the corresponding legacy PlayContext attribute is PlayContext.remote_addr. This may be the only case where a connection option name and the PlayContext attribute name differ.
1 parent 6d9f2e1 commit f50a61f

File tree

7 files changed

+19
-11
lines changed

7 files changed

+19
-11
lines changed

ansible_mitogen/transport_config.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -442,18 +442,22 @@ def _become_option(self, name):
442442
raise
443443

444444
LOG.info(
445-
'Used PlayContext fallback for plugin=%r, option=%r',
446-
self._connection, name,
445+
'Used fallback=PlayContext.%s for plugin=%r, option=%r',
446+
name, self._connection, name,
447447
)
448448
return getattr(self._play_context, name)
449449

450-
451-
def _connection_option(self, name):
450+
def _connection_option(self, name, fallback_attr=None):
452451
try:
453452
return self._connection.get_option(name, hostvars=self._task_vars)
454453
except KeyError:
455-
LOG.debug('Used PlayContext fallback for option=%r', name)
456-
return getattr(self._play_context, name)
454+
if fallback_attr is None:
455+
fallback_attr = name
456+
LOG.info(
457+
'Used fallback=PlayContext.%s for plugin=%r, option=%r',
458+
fallback_attr, self._connection, name,
459+
)
460+
return getattr(self._play_context, fallback_attr)
457461

458462
def transport(self):
459463
return self._transport
@@ -462,7 +466,7 @@ def inventory_name(self):
462466
return self._inventory_name
463467

464468
def remote_addr(self):
465-
return self._play_context.remote_addr
469+
return self._connection_option('host', fallback_attr='remote_addr')
466470

467471
def remote_user(self):
468472
return self._connection_option('remote_user')

tests/ansible/hosts/default.hosts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ ssh_args_canary_file=/tmp/ssh_args_by_inv_{{ inventory_hostname }}
2222
[tt_targets_bare]
2323
tt-bare
2424

25-
[tt_targets_bare:vars]
26-
ansible_host=localhost
27-
2825
[tt_become_bare]
2926
tt-become-bare
3027

@@ -43,6 +40,7 @@ ansible_host=localhost
4340
ansible_user="{{ lookup('pipe', 'whoami') }}"
4441

4542
[tt_targets_inventory]
43+
tt-host ansible_host="{{ 'localhost' | trim }}" ansible_password=has_sudo_nopw_password ansible_user=mitogen__has_sudo_nopw
4644
tt-host-key-checking ansible_host=localhost ansible_host_key_checking="{{ 'false' | trim }}" ansible_password=has_sudo_nopw_password ansible_user=mitogen__has_sudo_nopw
4745
tt-password ansible_host=localhost ansible_password="{{ 'has_sudo_nopw_password' | trim }}" ansible_user=mitogen__has_sudo_nopw
4846
tt-port ansible_host=localhost ansible_password=has_sudo_nopw_password ansible_port="{{ 22 | int }}" ansible_user=mitogen__has_sudo_nopw

tests/ansible/integration/ssh/args_by_play_taskvar.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
hosts: tt_targets_bare
33
gather_facts: false
44
vars:
5+
ansible_host: "{{ hostvars[groups['test-targets'][0]].host | default('localhost') }}"
56
ansible_password: "{{ 'has_sudo_nopw_password' | trim }}"
67
ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}"
78
ansible_ssh_common_args: >-

tests/ansible/integration/ssh/templated_by_play_keyword.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
gather_facts: false
44
remote_user: "{{ 'mitogen__has_sudo_nopw' | trim }}"
55
vars:
6+
ansible_host: "{{ hostvars[groups['test-targets'][0]].host | default('localhost') }}"
67
ansible_password: has_sudo_nopw_password
78
ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}"
89
tasks:

tests/ansible/integration/ssh/templated_by_play_taskvar.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
hosts: tt_targets_bare
33
gather_facts: false
44
vars:
5+
ansible_host: "{{ hostvars[groups['test-targets'][0]].host | default('localhost') }}"
56
ansible_password: "{{ 'has_sudo_nopw_password' | trim }}"
67
ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}"
78
ansible_ssh_executable: "{{ 'ssh' | trim }}"
@@ -16,6 +17,7 @@
1617
hosts: tt_targets_bare
1718
gather_facts: false
1819
vars:
20+
ansible_host: "{{ hostvars[groups['test-targets'][0]].host | default('localhost') }}"
1921
ansible_private_key_file: "{{ git_basedir }}/tests/data/docker/mitogen__has_sudo_pubkey.key"
2022
ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}"
2123
ansible_ssh_executable: "{{ 'ssh' | trim }}"

tests/ansible/integration/ssh/templated_by_task_keyword.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# https://github.com/mitogen-hq/mitogen/issues/1132
66
remote_user: "{{ 'mitogen__has_sudo_nopw' | trim }}"
77
vars:
8+
ansible_host: "{{ hostvars[groups['test-targets'][0]].host | default('localhost') }}"
89
ansible_password: has_sudo_nopw_password
910
ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}"
1011
tasks:
@@ -19,6 +20,7 @@
1920
delegate_to: "{{ groups.tt_targets_bare[0] }}"
2021
remote_user: "{{ 'mitogen__has_sudo_nopw' | trim }}"
2122
vars:
23+
ansible_host: "{{ hostvars[groups['test-targets'][0]].host | default('localhost') }}"
2224
ansible_password: has_sudo_nopw_password
2325
ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}"
2426
ping:

tests/ansible/templates/test-targets.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ ssh_args_canary_file=/tmp/ssh_args_by_inv_{{ '{{' }} inventory_hostname {{ '}}'
4444
tt-bare
4545

4646
[tt_targets_bare:vars]
47-
ansible_host={{ tt.hostname }}
4847
ansible_python_interpreter={{ tt.python_path }}
4948

5049
[tt_become_bare]
@@ -71,6 +70,7 @@ ansible_python_interpreter={{ tt.python_path }}
7170
ansible_user=mitogen__has_sudo_nopw
7271

7372
[tt_targets_inventory]
73+
tt-host ansible_host="{{ '{{' }} '{{ tt.hostname }}' | trim {{ '}}' }}" ansible_password=has_sudo_nopw_password ansible_port={{ tt.port }} ansible_python_interpreter={{ tt.python_path }} ansible_user=mitogen__has_sudo_nopw
7474
tt-host-key-checking ansible_host={{ tt.hostname }} ansible_host_key_checking="{{ '{{' }} 'false' | trim {{ '}}' }}" ansible_password=has_sudo_nopw_password ansible_port={{ tt.port }} ansible_python_interpreter={{ tt.python_path }} ansible_user=mitogen__has_sudo_nopw
7575
tt-password ansible_host={{ tt.hostname }} ansible_password="{{ '{{' }} 'has_sudo_nopw_password' | trim {{ '}}' }}" ansible_port={{ tt.port }} ansible_python_interpreter={{ tt.python_path }} ansible_user=mitogen__has_sudo_nopw
7676
tt-port ansible_host={{ tt.hostname }} ansible_password=has_sudo_nopw_password ansible_port="{{ '{{' }} {{ tt.port }} | int {{ '}}' }}" ansible_python_interpreter={{ tt.python_path }} ansible_user=mitogen__has_sudo_nopw

0 commit comments

Comments
 (0)