Skip to content

Commit 6da2c6a

Browse files
authored
Merge pull request #1189 from moreati/issue1083-host
ansible_mitogen: Templated target host
2 parents 0d09174 + f50a61f commit 6da2c6a

File tree

7 files changed

+31
-30
lines changed

7 files changed

+31
-30
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: 7 additions & 12 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,12 +40,10 @@ ansible_host=localhost
4340
ansible_user="{{ lookup('pipe', 'whoami') }}"
4441

4542
[tt_targets_inventory]
46-
tt-host-key-checking ansible_host_key_checking="{{ 'false' | trim }}" ansible_password=has_sudo_nopw_password ansible_user=mitogen__has_sudo_nopw
47-
tt-password ansible_password="{{ 'has_sudo_nopw_password' | trim }}" ansible_user=mitogen__has_sudo_nopw
48-
tt-port ansible_password=has_sudo_nopw_password ansible_port="{{ 22 | int }}" ansible_user=mitogen__has_sudo_nopw
49-
tt-private-key-file ansible_private_key_file="{{ git_basedir }}/tests/data/docker/mitogen__has_sudo_pubkey.key" ansible_user=mitogen__has_sudo_pubkey
50-
tt-remote-user ansible_password=has_sudo_nopw_password ansible_user="{{ 'mitogen__has_sudo_nopw' | trim }}"
51-
tt-ssh-executable ansible_password=has_sudo_nopw_password ansible_ssh_executable="{{ 'ssh' | trim }}" ansible_user=mitogen__has_sudo_nopw
52-
53-
[tt_targets_inventory:vars]
54-
ansible_host=localhost
43+
tt-host ansible_host="{{ 'localhost' | trim }}" ansible_password=has_sudo_nopw_password ansible_user=mitogen__has_sudo_nopw
44+
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
45+
tt-password ansible_host=localhost ansible_password="{{ 'has_sudo_nopw_password' | trim }}" ansible_user=mitogen__has_sudo_nopw
46+
tt-port ansible_host=localhost ansible_password=has_sudo_nopw_password ansible_port="{{ 22 | int }}" ansible_user=mitogen__has_sudo_nopw
47+
tt-private-key-file ansible_host=localhost ansible_private_key_file="{{ git_basedir }}/tests/data/docker/mitogen__has_sudo_pubkey.key" ansible_user=mitogen__has_sudo_pubkey
48+
tt-remote-user ansible_host=localhost ansible_password=has_sudo_nopw_password ansible_user="{{ 'mitogen__has_sudo_nopw' | trim }}"
49+
tt-ssh-executable ansible_host=localhost ansible_password=has_sudo_nopw_password ansible_ssh_executable="{{ 'ssh' | trim }}" 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: 7 additions & 11 deletions
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,13 +70,10 @@ ansible_python_interpreter={{ tt.python_path }}
7170
ansible_user=mitogen__has_sudo_nopw
7271

7372
[tt_targets_inventory]
74-
tt-host-key-checking ansible_host_key_checking="{{ '{{' }} 'false' | trim {{ '}}' }}" ansible_password=has_sudo_nopw_password ansible_port={{ tt.port }} ansible_user=mitogen__has_sudo_nopw
75-
tt-password ansible_password="{{ '{{' }} 'has_sudo_nopw_password' | trim {{ '}}' }}" ansible_port={{ tt.port }} ansible_user=mitogen__has_sudo_nopw
76-
tt-port ansible_password=has_sudo_nopw_password ansible_port="{{ '{{' }} {{ tt.port }} | int {{ '}}' }}" ansible_user=mitogen__has_sudo_nopw
77-
tt-private-key-file ansible_port={{ tt.port }} ansible_private_key_file="{{ '{{' }} git_basedir {{ '}}' }}/tests/data/docker/mitogen__has_sudo_pubkey.key" ansible_user=mitogen__has_sudo_pubkey
78-
tt-remote-user ansible_password=has_sudo_nopw_password ansible_port={{ tt.port }} ansible_user="{{ '{{' }} 'mitogen__has_sudo_nopw' | trim {{ '}}' }}"
79-
tt-ssh-executable ansible_password=has_sudo_nopw_password ansible_port={{ tt.port }} ansible_ssh_executable="{{ '{{' }} 'ssh' | trim {{ '}}' }}" ansible_user=mitogen__has_sudo_nopw
80-
81-
[tt_targets_inventory:vars]
82-
ansible_host={{ tt.hostname }}
83-
ansible_python_interpreter={{ tt.python_path }}
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
74+
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
75+
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
76+
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
77+
tt-private-key-file ansible_host={{ tt.hostname }} ansible_port={{ tt.port }} ansible_private_key_file="{{ '{{' }} git_basedir {{ '}}' }}/tests/data/docker/mitogen__has_sudo_pubkey.key" ansible_python_interpreter={{ tt.python_path }} ansible_user=mitogen__has_sudo_pubkey
78+
tt-remote-user ansible_host={{ tt.hostname }} ansible_password=has_sudo_nopw_password ansible_port={{ tt.port }} ansible_python_interpreter={{ tt.python_path }} ansible_user="{{ '{{' }} 'mitogen__has_sudo_nopw' | trim {{ '}}' }}"
79+
tt-ssh-executable ansible_host={{ tt.hostname }} ansible_password=has_sudo_nopw_password ansible_port={{ tt.port }} ansible_python_interpreter={{ tt.python_path }} ansible_ssh_executable="{{ '{{' }} 'ssh' | trim {{ '}}' }}" ansible_user=mitogen__has_sudo_nopw

0 commit comments

Comments
 (0)