You're getting your "target" variable from vars_prompt just fine. It's a
'unicode object' (i.e. a string), so you can't use its non-existent 'ms'
attribute. If you really want to pull in the "target" host interactively,
the easiest fix is to restructure your "lparlist.yml" file like this:
---
lpars:
lpar1:
hmc: hmc1
ms: ms1
lpar2:
hmc: hmc2
ms: ms2
Note: it's no longer a list, so "lparlist.yml" may not be a great name.
Then you use "target" (which would be either "lpar1" or "lpar2") like so:
- name: Start a logical partition.
powervm_lpar_instance:
hmc_host: "{{ lpars[target].hmc }}"
hmc_auth: "{{ curr_hmc_auth }}"
system_name: "{{ lpars[target].ms }}"
vm_name: "{{ target }}"
action: poweron
But if you do it that way, you've inadvertently re-implements the "hosts:"
functionality. Given your inventory, I think you should hard-code the group
in hosts:
hosts: hmc
and drop "vars_prompt" and "target" altogether. Limit the hosts on the
command line when necessary:
ansible-playbook playbookname.yml --limit=hmc1
Then your playbook becomes
- name: Start a logical partition.
powervm_lpar_instance:
hmc_host: "{{ inventory_hostname }}" # either 'hmc1' or 'hmc2'
hmc_auth: "{{ curr_hmc_auth }}"
system_name: "{{ item.value.ms }}" # either 'ms1' or 'ms2'
vm_name: "{{ item.key }}" # either 'lpar1' or 'lpar2'
action: poweron
loop: "{{ lpars | dict2items }}"
where: item.value.hmc == inventory_hostname
On Monday, February 20, 2023 at 12:29:45 PM UTC-5 Kenady Inampudi wrote:
> need help in passing external variables using vars_prompt
>
> working example i have a list of servers in a vars_file lparlist.yml
>
> i need to use vars_prompt so i can provide the server name without editing
> the playbook every time
>
>
> working example
>
> *inventory*
> cat inventory
> [hmc]
> hmc1
> hmc2
>
>
> *vars_file*
> cat lparlist.yml
>
> ---
> - lpar1:
> hmc: hmc1
> ms: ms1
> - lpar2:
> hmc: hmc2
> ms: ms2
>
> *actual playbook:*
>
> ---
> - name: Start a logical partition
> hosts: "{{ lpar1.hmc }}"
> collections:
> - ibm.power_hmc
> connection: local
> vars_files:
> - lparlist.yml
> vars:
> curr_hmc_auth:
> username: username
> password: password
> tasks:
> - name: Start a logical partition.
> powervm_lpar_instance:
> hmc_host: "{{ lpar1.hmc }}"
> hmc_auth: "{{ curr_hmc_auth }}"
> system_name: "{{ lpar1.ms }}"
> vm_name: lpar1
> action: poweron
>
>
>
> *output*
>
> PLAY [Start a logical partition]
> *******************************************************************************************************************************************************************************
>
> TASK [Gathering Facts]
> ********************************************************************************************************************************************************************************************
> ok: [hmc1]
>
> TASK [Start a logical partition.]
> ******************************************************************************************************************************************************************************
> changed: [hmc1]
>
> PLAY RECAP
> ********************************************************************************************************************************************************************************************************
> hmc1 : ok=2 changed=1 unreachable=0 failed=0
> skipped=0 rescued=0 ignored=0
>
>
>
> *not working playbook*
>
>
> ---
> - name: Start a logical partition
> hosts: "{{ lpar1.hmc }}"
> vars_prompt:
> - name: target
> prompt: Enter Server Name
> private: false
> collections:
> - ibm.power_hmc
> connection: local
> vars_files:
> - lparlist.yml
> vars:
> curr_hmc_auth:
> username: username
> password: password
> tasks:
> - name: Start a logical partition.
> powervm_lpar_instance:
> hmc_host: "{{ target.hmc }}"
> hmc_auth: "{{ curr_hmc_auth }}"
> system_name: "{{ target.ms }}"
> vm_name: "{{ target }}"
> action: poweron
>
>
> *output*
>
> PLAY [Start a logical partition]
> *******************************************************************************************************************************************************************************
>
> TASK [Gathering Facts]
> ********************************************************************************************************************************************************************************************
> ok: [hmc1]
>
> TASK [Start a logical partition.]
> ******************************************************************************************************************************************************************************
> fatal: [hmc1]: FAILED! => {"msg": "The task includes an option with an
> undefined variable. The error was: 'unicode object' has no attribute
> 'ms'\n\nThe error appears to be in '/home/inampk48/hmc/p3/poweron.yml':
> line 17, column 7, but may\nbe elsewhere in the file depending on the exact
> syntax problem.\n\nThe offending line appears to be:"\n - name: Start a
> logical partition.\n ^ here\n"}
>
>
> PLAY RECAP
> ********************************************************************************************************************************************************************************************************
> hmc1 : ok=1 changed=0 unreachable=0 failed=1
> skipped=0 rescued=0 ignored=0
>
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/ff8063f4-a46d-4798-9e5e-324bc40fb80cn%40googlegroups.com.