Passing vm name as parameter in saltstack

I have created a vm using salt stack. I want to create another vm with exactly same configuration but using different name. I was wondering if there are any examples of parameterizing vm names.

You can use a jinja forloop:

{% for vm_name in ['vm1', 'vm2'] ℅}
    do what you want here with {{ vm_name }}
{% endfor %}
1 Like

Absolutely - and you use the handy qvm.clone to clone your first qube.

{% for vm_name in ['vm1', 'vm2'] %}
create_clones:
  qvm.clone:
    - name: {{ vm_name }}
    - source: UR_QUBE
{% endfor %}

replacing UR_QUBE with the name of your first qube.

Thanks. Though you answered my question, I realized I was not specific enough in my question. I don’t want to create VMs in a loop but rather create it at different times. Possibly passing parameter from command line or passing parameter from different sls file. something like vm-1.sls and vm-2.sls and it imports another sls or template with different parameters.

Clone did not work for me. I cloned a standalone qube and looks like ip routing table has entries with the ip address of cloned vm. I was not able to make it work right away. I thought why not parameterize and create two separate VMs.

So, you can create a file and use it as a template, like this in vm1.sls:

{% set vm_name = 'vm1' %}
{% extends 'template_file.sls' %}

You can use {{ grains['id'] }} but I don’t think it’s a good idea.

2 Likes

If you really want to do things from the command line without having to create any sort of nesting .sls file it can be done, but it’s a bit ungainly. It involves temporarily defining things in the pillar, on the command line

In your sls file:

{% set vmname = pillar[ 'vmname' ]|string %}

And on the command line:

sudo qubesctl state.apply <the name of your sls file without the .sls> pillar='{"vmname": "<desired_qube_name>"}'

2 Likes

There are a number of ways of passing in parameters - using pillar data
is one.
Instead of the method @SteveC suggested, you can pass in the pillar at
the command line. Here’s clone.sls

qvm-pillar-clone:
  qvm.clone:
    - name: {{ pillar['newqube'] }}
    - source: base_qube

Call this with:
qubesctl state.apply clone pillar='{"newqube": "pillar_clone"}'

Or you can pass in an environment variable - like this, using
clone2.sls

qvm-env-clone:
  qvm.clone:
    - name: {{ salt['environ.get']('newqube') }}
    - source: base_qube

Call this with:
newqube=environ_qube qubesctl state.apply clone2

Or, as it’s all python there are many other routes to the same solution.

I’ve given clone examples, but the principle can be applied in almost
any state file.

2 Likes