How to pass multiple values to "with_items" through ad-hoc command?
56
So, to loop your debug
task by passing the values from command line (or anywhere else), you will need to use a variable for the task (e.g. with_items: "{{ my_var }}"
).
There are many ways to supply value to variables, but for this specific requirement, we can use --extra-vars
.
Considering a playbook myplaybook.yml
as below:
- hosts: localhost
tasks:
- name: Here we are providing a list which have items containing multiple
debug:
msg: "current first value is {{ item.first }} and second value is {{ item.second }}"
with_items: "{{ my_var }}"
Can be run by passing this variable on the command line as:
ansible-playbook myplaybook.yml -e '{my_var: [{first: lemon, second: carrot}, {first: cow, second: goat}]}'
Do note the other ways (linked above) using which variables can be supplied and use them if they are more efficient than this approach.
Related videos on Youtube
Author by
Sashi K
Updated on December 02, 2022Comments
-
Sashi K over 1 year
Here is the example ansible-playbook, if I want to pass the values of {{ item.first }} and {{ item.second }} through ad-hoc command from the terminal.
How can we do it ?
Thanks in advance..
--- - hosts: localhost tasks: - name: Here we are providing a list which have items containing multiple debug: msg: "current first value is {{ item.first }} and second value is {{ item.second }}" with_items: - { first: lemon, second: carrot } - { first: cow, second: goat }
-
seshadri_c over 2 yearsDo you want to run this task/play, taking value from command line instead? If its the ad-hoc mode, you won't be able to use
with_items
anditem
. -
U880D over 2 yearsDoes variables in Ansible ad-hoc commands answer your quesstion?
-
Sashi K over 2 yearsthank you @seshadri_c can you please let me know what needs to be changed in the playbook. So, that the above task/play could be run from the command line. thanks in advance.
-
-
phandox over 10 yearsCan you open Command prompt (win8 pcsupport.about.com/od/windows-8/a/command-prompt-windows-8.htm Win7 - just click on start and write cmd then enter) and provide a output of ipconfig /all command ?
-
phandox over 10 yearsOT: Next time, for long output, you can use pastebin.com So, can you ping 192.168.1.1 ? What is the output? Again, do it in command prompt. What happened when you enter 192.168.1.1 into browser? Are you directly connected to router? Router <-> PC
-
user292033 over 10 yearsok, thanks. When i ping 192.168.1.1, i am getting a "request timed out". And when i hit 192.168.1.1, it tries to connect for a long time and displays "couldnt connect message". Yes, I am directly connected to the router. Modem->Router->PC
-
user292033 over 10 yearsAlso, I just did a reset of modem and here is the output of ipconfig /all.ipconfig
-
phandox over 10 yearsYou did reset modem or router? Anyway, now try to ping 192.168.0.1 (as I see your IP address has changed) and type it to browser to see what happens. Basicly, you need to get to router configuration, that's why we are doing this...
-
user292033 over 10 yearsSorry, I meant reset of router. It again changed to 192.168.1.1. Now, I am able to see the router page. Please let me know what changes i need to do here. Thank you so much.
-
100rabh over 10 yearsugh, please edit your question and add the details, don't cram it into comments
-
Sashi K over 2 yearsit worked...thank you. @seshadri_c