Skip to content

Commit

Permalink
ansible_mitogen: Templated host option (e.g. ansible_host, ansible_ss…
Browse files Browse the repository at this point in the history
…h_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.
  • Loading branch information
moreati committed Nov 7, 2024
1 parent 6d9f2e1 commit f50a61f
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 11 deletions.
18 changes: 11 additions & 7 deletions ansible_mitogen/transport_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,18 +442,22 @@ def _become_option(self, name):
raise

LOG.info(
'Used PlayContext fallback for plugin=%r, option=%r',
self._connection, name,
'Used fallback=PlayContext.%s for plugin=%r, option=%r',
name, self._connection, name,
)
return getattr(self._play_context, name)


def _connection_option(self, name):
def _connection_option(self, name, fallback_attr=None):
try:
return self._connection.get_option(name, hostvars=self._task_vars)
except KeyError:
LOG.debug('Used PlayContext fallback for option=%r', name)
return getattr(self._play_context, name)
if fallback_attr is None:
fallback_attr = name
LOG.info(
'Used fallback=PlayContext.%s for plugin=%r, option=%r',
fallback_attr, self._connection, name,
)
return getattr(self._play_context, fallback_attr)

def transport(self):
return self._transport
Expand All @@ -462,7 +466,7 @@ def inventory_name(self):
return self._inventory_name

def remote_addr(self):
return self._play_context.remote_addr
return self._connection_option('host', fallback_attr='remote_addr')

def remote_user(self):
return self._connection_option('remote_user')
Expand Down
4 changes: 1 addition & 3 deletions tests/ansible/hosts/default.hosts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ ssh_args_canary_file=/tmp/ssh_args_by_inv_{{ inventory_hostname }}
[tt_targets_bare]
tt-bare

[tt_targets_bare:vars]
ansible_host=localhost

[tt_become_bare]
tt-become-bare

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

[tt_targets_inventory]
tt-host ansible_host="{{ 'localhost' | trim }}" ansible_password=has_sudo_nopw_password ansible_user=mitogen__has_sudo_nopw
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
tt-password ansible_host=localhost ansible_password="{{ 'has_sudo_nopw_password' | trim }}" ansible_user=mitogen__has_sudo_nopw
tt-port ansible_host=localhost ansible_password=has_sudo_nopw_password ansible_port="{{ 22 | int }}" ansible_user=mitogen__has_sudo_nopw
Expand Down
1 change: 1 addition & 0 deletions tests/ansible/integration/ssh/args_by_play_taskvar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
hosts: tt_targets_bare
gather_facts: false
vars:
ansible_host: "{{ hostvars[groups['test-targets'][0]].host | default('localhost') }}"
ansible_password: "{{ 'has_sudo_nopw_password' | trim }}"
ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}"
ansible_ssh_common_args: >-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
gather_facts: false
remote_user: "{{ 'mitogen__has_sudo_nopw' | trim }}"
vars:
ansible_host: "{{ hostvars[groups['test-targets'][0]].host | default('localhost') }}"
ansible_password: has_sudo_nopw_password
ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}"
tasks:
Expand Down
2 changes: 2 additions & 0 deletions tests/ansible/integration/ssh/templated_by_play_taskvar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
hosts: tt_targets_bare
gather_facts: false
vars:
ansible_host: "{{ hostvars[groups['test-targets'][0]].host | default('localhost') }}"
ansible_password: "{{ 'has_sudo_nopw_password' | trim }}"
ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}"
ansible_ssh_executable: "{{ 'ssh' | trim }}"
Expand All @@ -16,6 +17,7 @@
hosts: tt_targets_bare
gather_facts: false
vars:
ansible_host: "{{ hostvars[groups['test-targets'][0]].host | default('localhost') }}"
ansible_private_key_file: "{{ git_basedir }}/tests/data/docker/mitogen__has_sudo_pubkey.key"
ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}"
ansible_ssh_executable: "{{ 'ssh' | trim }}"
Expand Down
2 changes: 2 additions & 0 deletions tests/ansible/integration/ssh/templated_by_task_keyword.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# https://github.com/mitogen-hq/mitogen/issues/1132
remote_user: "{{ 'mitogen__has_sudo_nopw' | trim }}"
vars:
ansible_host: "{{ hostvars[groups['test-targets'][0]].host | default('localhost') }}"
ansible_password: has_sudo_nopw_password
ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}"
tasks:
Expand All @@ -19,6 +20,7 @@
delegate_to: "{{ groups.tt_targets_bare[0] }}"
remote_user: "{{ 'mitogen__has_sudo_nopw' | trim }}"
vars:
ansible_host: "{{ hostvars[groups['test-targets'][0]].host | default('localhost') }}"
ansible_password: has_sudo_nopw_password
ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}"
ping:
2 changes: 1 addition & 1 deletion tests/ansible/templates/test-targets.j2
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ ssh_args_canary_file=/tmp/ssh_args_by_inv_{{ '{{' }} inventory_hostname {{ '}}'
tt-bare

[tt_targets_bare:vars]
ansible_host={{ tt.hostname }}
ansible_python_interpreter={{ tt.python_path }}

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

[tt_targets_inventory]
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
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
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
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
Expand Down

0 comments on commit f50a61f

Please sign in to comment.