run tasks based on variable

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

Ansible 2.4

Yes

How

Depends on what you actually want to do.

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

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

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

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.

Sorry I will try to give more details

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

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

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

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.