Fix ansible_user undefined error in GKE and AKS Terraform tasks#189
Fix ansible_user undefined error in GKE and AKS Terraform tasks#189ssimpson89 wants to merge 1 commit intoctrliq:mainfrom
Conversation
When ansible_connection=local is used, ansible_user is never set and
referencing it causes an immediate failure. Replace /home/{{ ansible_user }}
with {{ ansible_env.HOME }} which resolves correctly in local connections.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Fixes failures in the k8s setup role when running Terraform provisioning with ansible_connection=local by avoiding a hard dependency on ansible_user for computing a home directory path.
Changes:
- Update GKE Terraform variables to set
home_dirfromansible_env.HOMEinstead of/home/{{ ansible_user }}. - Update AKS Terraform variables to set
home_dirfromansible_env.HOMEinstead of/home/{{ ansible_user }}.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| playbooks/roles/k8s_setup/tasks/k8s_setup_gke.yml | Changes Terraform home_dir variable to use ansible_env.HOME. |
| playbooks/roles/k8s_setup/tasks/k8s_setup_aks.yml | Changes Terraform home_dir variable to use ansible_env.HOME. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| state: present | ||
| variables: | ||
| home_dir: "/home/{{ ansible_user}}" | ||
| home_dir: "{{ ansible_env.HOME }}" |
There was a problem hiding this comment.
ansible_env.HOME will be undefined here unless the env fact subset is gathered. This role/play disables full fact gathering (playbooks/kubernetes_setup.yml sets gather_facts: no), and the only setup call in this task file gathers date_time only, so this change can still fail with an undefined-variable error. Consider using lookup('env', 'HOME') (works without facts for local connections) or expand the ansible.builtin.setup gather_subset to include env before referencing ansible_env.HOME.
| home_dir: "{{ ansible_env.HOME }}" | |
| home_dir: "{{ lookup('env', 'HOME') }}" |
| state: present | ||
| variables: | ||
| home_dir: "/home/{{ ansible_user}}" | ||
| home_dir: "{{ ansible_env.HOME }}" |
There was a problem hiding this comment.
ansible_env.HOME is only available when environment facts are gathered. This play/role disables normal fact gathering and the setup task in this file only gathers date_time, so ansible_env.HOME may be undefined and cause the same immediate failure you’re trying to avoid. Use lookup('env', 'HOME') or adjust the setup/fact gathering to include the env subset before using ansible_env.HOME.
| home_dir: "{{ ansible_env.HOME }}" | |
| home_dir: "{{ lookup('env', 'HOME') }}" |
When
ansible_connection=localis used,ansible_useris never set and referencing it causes an immediate failure. Replace/home/{{ ansible_user }}with{{ ansible_env.HOME }}which resolves correctly in local connections.