Skip to content

Commit 2aa3bce

Browse files
holserclaude
authored andcommitted
(multiple) Fix home directory path when delegating to localhost
When tasks delegate to localhost, ansible_user_dir may contain the remote host's value rather than the local machine's home directory. Use lookup('env', 'HOME') to ensure the correct local home path is used for SSH config operations. Co-Authored-By: Claude <[email protected]> Signed-off-by: Sergii Golovatiuk <[email protected]>
1 parent 4f1d278 commit 2aa3bce

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

roles/libvirt_manager/tasks/clean_layout.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@
339339
- name: Clean local ssh config
340340
vars:
341341
cifmw_ssh_jumper_target: 'localhost'
342-
cifmw_ssh_jumper_config_dir: "{{ ansible_user_dir }}/.ssh"
342+
cifmw_ssh_jumper_config_dir: "{{ lookup('env', 'HOME') }}/.ssh"
343343
ansible.builtin.include_role:
344344
name: "ssh_jumper"
345345
tasks_from: "cleanup.yml"

roles/ssh_jumper/tasks/manage_ssh_jumper_entry.yml

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,42 @@
1818
delegate_to: "{{ _config.target | default(omit) }}"
1919
vars:
2020
_ssh_dir: >-
21-
{{
22-
_config.ssh_dir |
23-
default(cifmw_ssh_jumper_defaults.ssh_dir)
24-
}}
21+
{%- if _config.target is defined and _config.target == 'localhost' -%}
22+
{{ lookup('env', 'HOME') }}/.ssh
23+
{%- else -%}
24+
{{ _config.ssh_dir }}
25+
{%- endif -%}
2526
ansible.builtin.file:
26-
path: "{{ _ssh_dir }}/cifmw_ssh_config.d//{{ inventory_hostname }}"
27+
path: "{{ _ssh_dir }}/cifmw_ssh_config.d/{{ inventory_hostname }}"
2728
state: directory
2829
mode: '0700'
2930

3031
- name: Include ~/.ssh/cifmw_ssh_config.d/inventory_hostname/*.conf
3132
delegate_to: "{{ _config.target | default(omit) }}"
3233
vars:
33-
_ssh_dir: "{{ _config.ssh_dir | default(cifmw_ssh_jumper_defaults.ssh_dir) }}"
34+
_ssh_dir: >-
35+
{%- if _config.target is defined and _config.target == 'localhost' -%}
36+
{{ lookup('env', 'HOME') }}/.ssh
37+
{%- else -%}
38+
{{ _config.ssh_dir }}
39+
{%- endif -%}
3440
ansible.builtin.lineinfile:
3541
insertbefore: BOF
3642
create: true
3743
state: present
38-
path: "{{ _ssh_dir | default((ansible_user_dir, '/.ssh') | path_join) }}/config"
44+
path: "{{ _ssh_dir }}/config"
3945
mode: '0600'
4046
line: 'Include cifmw_ssh_config.d/{{ inventory_hostname }}/*.conf'
4147

4248
- name: "Inject ssh jumpers for {{ _config.hostname }}"
4349
delegate_to: "{{ _config.target | default(omit) }}"
4450
vars:
45-
_ssh_dir: "{{ _config.ssh_dir | default(cifmw_ssh_jumper_defaults.ssh_dir) }}"
51+
_ssh_dir: >-
52+
{%- if _config.target is defined and _config.target == 'localhost' -%}
53+
{{ lookup('env', 'HOME') }}/.ssh
54+
{%- else -%}
55+
{{ _config.ssh_dir }}
56+
{%- endif -%}
4657
_dir: "{{ _ssh_dir }}/cifmw_ssh_config.d/{{ inventory_hostname }}"
4758
_filename: >-
4859
{%- if _config.proxy_host is defined and _config.proxy_host is not none -%}

roles/ssh_jumper/vars/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# under the License.
1616

1717
cifmw_ssh_jumper_defaults:
18-
ssh_dir: "{{ ansible_user_dir }}/.ssh"
18+
ssh_dir: "{{ ansible_user_dir | default(lookup('env', 'HOME')) }}/.ssh"
1919
user: zuul
2020
patterns: []
2121
proxy_user: "{{ ansible_user | default(lookup('env', 'USER')) }}"

0 commit comments

Comments
 (0)