# run tasks based on variable

**URL:** <https://forum.ansible.com/t/run-tasks-based-on-variable/29196>\
**Category:** Ansible Project\
**Created:** [December 4, 2018, 7:03pm UTC](https://forum.ansible.com/t/run-tasks-based-on-variable/29196 "2018-12-04T19:03:21Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bill\_Nolf](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@Bill\_Nolf](https://forum.ansible.com/u/Bill_Nolf)\
**Post date:** [December 4, 2018, 7:03pm UTC](https://forum.ansible.com/t/run-tasks-based-on-variable/29196/1 "2018-12-04T19:03:21Z")

</div>

I have the following vars:

vms  
test1  
name: test1  
disk\_size:

- size: 10
- size: 20

test2  
name: test2  
disk\_size:

- size: 10

I need to be able to set tasks on test1 and  
then the same set on test2. Is there a way to do

this in ansible?

thanks

---

<div class="post-metadata">

**Author:** ![Bill\_Nolf](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@Bill\_Nolf](https://forum.ansible.com/u/Bill_Nolf)\
**Post date:** [December 4, 2018, 7:03pm UTC](https://forum.ansible.com/t/run-tasks-based-on-variable/29196/2 "2018-12-04T19:03:52Z")

</div>

Ansible 2.4

---

<div class="post-metadata">

**Author:** ![powcarz](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@powcarz](https://forum.ansible.com/u/powcarz)\
**Post date:** [December 4, 2018, 7:31pm UTC](https://forum.ansible.com/t/run-tasks-based-on-variable/29196/3 "2018-12-04T19:31:08Z")

</div>

Yes

---

<div class="post-metadata">

**Author:** ![Bill\_Nolf](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@Bill\_Nolf](https://forum.ansible.com/u/Bill_Nolf)\
**Post date:** [December 4, 2018, 7:32pm UTC](https://forum.ansible.com/t/run-tasks-based-on-variable/29196/4 "2018-12-04T19:32:25Z")

</div>

How

---

<div class="post-metadata">

**Author:** ![powcarz](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@powcarz](https://forum.ansible.com/u/powcarz)\
**Post date:** [December 4, 2018, 7:33pm UTC](https://forum.ansible.com/t/run-tasks-based-on-variable/29196/5 "2018-12-04T19:33:35Z")

</div>

Depends on what you actually want to do.

---

<div class="post-metadata">

**Author:** ![Bill\_Nolf](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@Bill\_Nolf](https://forum.ansible.com/u/Bill_Nolf)\
**Post date:** [December 5, 2018, 12:32pm UTC](https://forum.ansible.com/t/run-tasks-based-on-variable/29196/6 "2018-12-05T12:32:56Z")

</div>

What I’m try to do is create argument string that needs to be run each time the name of the vm changes.  
The argument contains the name and uses the disk size.

I was able to build an array containing the correct argument string but it uses all disks.

example  
–disk name=test1 size 10  
–disk name=test1 size 20  
–disk name=test2 size 20

What I need is

–disk name=test1 size 10  
–disk name=test1 size 10

run command

–disk name=test2 size 20  
run command

The number of vms could be 1 to many

thanks

---

<div class="post-metadata">

**Author:** ![powcarz](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@powcarz](https://forum.ansible.com/u/powcarz)\
**Post date:** [December 5, 2018, 5:12pm UTC](https://forum.ansible.com/t/run-tasks-based-on-variable/29196/7 "2018-12-05T17:12:30Z")

</div>

Hi Bill

What you are describing, are the extra variables. Please check the docs: [https://docs.ansible.com/ansible/2.5/user\_guide/playbooks\_variables.html#passing-variables-on-the-command-line](https://docs.ansible.com/ansible/2.5/user_guide/playbooks_variables.html#passing-variables-on-the-command-line)

---

<div class="post-metadata">

**Author:** ![Bill\_Nolf](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@Bill\_Nolf](https://forum.ansible.com/u/Bill_Nolf)\
**Post date:** [December 10, 2018, 12:26pm UTC](https://forum.ansible.com/t/run-tasks-based-on-variable/29196/8 "2018-12-10T12:26:27Z")

</div>

I don’t think that is what I need

I need to do something like  
If name not equal previous-name; then  
viirt-install disk-arg  
previous-name =name  
disk-arg = “”  
else  
disks-arg=disk-arg+ disk  
fi

Thanks

---

<div class="post-metadata">

**Author:** ![powcarz](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@powcarz](https://forum.ansible.com/u/powcarz)\
**Post date:** [December 10, 2018, 1:28pm UTC](https://forum.ansible.com/t/run-tasks-based-on-variable/29196/9 "2018-12-10T13:28:15Z")

</div>

Bill,  
You are giving very little information… please spend some time and describe in details what your idea is, plus some extra answers:  
How does Ansible know what the previous-name was? How do you store the previous-name value for the next playbook run?

Are you running playbook periodically? Are you executing the playbook per VM, or do you store all VMs configurations in yaml?  
At what stage of developing the playbook are you? Send us what you already got, with the -vvv output and explanaition what did you expect vs what the playbook actually did.

---

<div class="post-metadata">

**Author:** ![Bill\_Nolf](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@Bill\_Nolf](https://forum.ansible.com/u/Bill_Nolf)\
**Post date:** [December 10, 2018, 1:39pm UTC](https://forum.ansible.com/t/run-tasks-based-on-variable/29196/10 "2018-12-10T13:39:28Z")

</div>

Sorry I will try to give more details

---

<div class="post-metadata">

**Author:** ![Bill\_Nolf](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@Bill\_Nolf](https://forum.ansible.com/u/Bill_Nolf)\
**Post date:** [December 10, 2018, 2:42pm UTC](https://forum.ansible.com/t/run-tasks-based-on-variable/29196/11 "2018-12-10T14:42:54Z")

</div>

The playbook is run each time a vm needs to be allocated. The details for each vm is store in the group\_vars file as follows:

vms  
one

- name: vmone
- mem: 4096
- cpus: 2
- disks
- size: 10
- size: 20  
two
- name: vmtwo
- mem: 4096
- cpus: 1
- disks:
- size: 10

How to set the previous name and how to loop through each VM is the problem I’m having. I can store the disk into an array. Vm s have one or two disks. I want to process the disk for vmone and run virt-install then repeat for vmtwo, etc  
I wish I could give you the playbooks but they reside on another network which I can’t download from.  
I hope this provides enough info that you are able to give me something to work with. Once I have a starting point I will work with it until I’m able to get it to do what I need  
Thanks

---

<div class="post-metadata">

**Author:** ![powcarz](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@powcarz](https://forum.ansible.com/u/powcarz)\
**Post date:** [December 10, 2018, 11:51pm UTC](https://forum.ansible.com/t/run-tasks-based-on-variable/29196/12 "2018-12-10T23:51:53Z")

</div>

Sorry Bill, I can’t help you if you don’t cooperate. I am not going to waste my time on guessing what you are trying to do. Maybe someone else is willing asking you over and over.  
I can only suggest you to check virt and virt\_pool because since you are mentioning virt-install command.

Good luck  
Piotr

---

<div class="post-metadata">

**Author:** ![Bill\_Nolf](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@Bill\_Nolf](https://forum.ansible.com/u/Bill_Nolf)\
**Post date:** [December 11, 2018, 2:37am UTC](https://forum.ansible.com/t/run-tasks-based-on-variable/29196/13 "2018-12-11T02:37:10Z")

</div>

Thanks. I was trying to explain what I was trying to do. Sorry for wasting your time

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [December 11, 2018, 3:20am UTC](https://forum.ansible.com/t/run-tasks-based-on-variable/29196/14 "2018-12-11T03:20:57Z")

</div>

Hey Bill.

We would like to help. But we simply do not know what you are trying to achieve. Just tell us, in plain terms, what you are trying to do. Not the details, not the Ansible code - just the end result you seek. We can drill down from that.

Also, those playbooks that you cannot download: What do they do (in very plain terms - inputs and outputs)?

You may be better off setting up a testbed where you control all the components; then you can make playbooks you DO control to emulate the ones on the remotes that you can’t see.

You might also like to break your overall job down into smaller testable chunks, such as detecting a change of name, executing a command, iterating over a group, building a suitable command and so on.

I’m afraid I know zilch about (VMWare?) VMs, so can’t help with the detail.

Regards, K.
