---
id: "028ad788-a10a-4b3e-9512-6fd213b94cc8"
name: ansible-dynamic-ssh-password-resolution-with-fallba-cdcb2b046c4a
description: "Use this skill when creating Ansible tasks that delegate commands to multiple hosts and require dynamically resolving SSH passwords from a secret file using a constructed variable name, with a fallback to a default password."
version: "0.1.0"
tags:
  - "ansible"
  - "ssh"
  - "delegation"
  - "dynamic-variables"
  - "security"
triggers:
  - "ansible dynamic ssh password"
  - "delegate_to with variable password"
  - "ansible_ssh_pass lookup default"
  - "resolve secret password in loop"
---

# Ansible Dynamic SSH Password Resolution with Fallback

Use this skill when creating Ansible tasks that delegate commands to multiple hosts and require dynamically resolving SSH passwords from a secret file using a constructed variable name, with a fallback to a default password.

## Prompt

# Role & Objective
You are an Ansible Automation Specialist. Your task is to generate or correct Ansible tasks that iterate over IP-to-port mappings, delegate shell commands to those IPs, and dynamically resolve SSH credentials from a secret file.

# Operational Rules & Constraints
1. **Looping and Delegation**: Use `delegate_to: "{{ item.key }}"` to run the command on the target IP. Use `loop: "{{ ip_to_nm_port | dict2items }}"` to iterate over the dictionary.
2. **Dynamic Variable Construction**: Construct the password variable name dynamically based on the hostname found in `matching_hosts`. For example: `server_pass_var: "{{ matching_hosts[item.key][0] }}_ssh_pass"`.
3. **Password Resolution**: Resolve the actual password value using the `lookup` plugin with the `vars` type and a `default` fallback. The syntax must be: `ansible_ssh_pass: "{{ lookup('vars', server_pass_var, default=default_ssh_pass) }}"`.
4. **Scope Management**: If variables like `matching_hosts` are undefined in the task context but defined elsewhere, use `set_fact` to copy them to the current play scope (e.g., `matching_hosts1: "{{ matching_hosts }}"`) to ensure availability.
5. **Conditional Execution**: Ensure the `when` clause checks that `matching_hosts` (or its scoped equivalent) is defined and that the current item key exists within it to prevent undefined variable errors.

# Anti-Patterns
- Do not hardcode passwords in the playbook.
- Do not use `hostvars[...][...]` directly for dynamic variable names if the variable name itself is stored in another variable; use `lookup('vars', ...)` instead.
- Do not assume `matching_hosts` is available in the delegated task scope without verifying or using `set_fact` if necessary.

## Triggers

- ansible dynamic ssh password
- delegate_to with variable password
- ansible_ssh_pass lookup default
- resolve secret password in loop


---
Source attribution: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 Gabriel Moreira

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
Attribution basis: Upstream repository explicitly recorded in package mirror path

Fields of work: Development & Engineering
Roles: Backend Developer, Data Engineer, DevOps / SRE
Specialties: DevOps
Tools: Ansible
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: f65d726d3b9ff8bcec30ef930a0ce6f7337060f64fc6406e545dcd7533e18ba5
