2

I'm starting with ansible. The main reason I'm looing into ansible vs other configuratoin management tools is its ability to orchestrate, that is to create programmatically, virtual machines on my virtualization environment. Currently proxmox-ve.

Here is what my first (proxmox-related) playbook looks like:

# Creates a host within proxmox environment
- hosts: localhost
  tasks:
  - name: "create vm"
    proxmox:
      node: cvirt
      api_user: user@pve
      api_password: password
      api_host: 192.168.5.4
      password: hostpass
      hostname: ansiblehost1
      storage: local-lvm
      timeout: 300
      ostemplate: 'local:vztmpl/centos-7-vagrant_amd64.tar.xz'
  - name: "start vm"
    proxmox:
      node: cvirt
      api_user: user@pve
      api_password: password
      api_host: 192.168.5.4
      hostname: ansiblehost
      state: started

Now, what annoys me with this playbook is that I have to repeat (over and over) the node, api_user, api_password, api_host options to every task I define.

What I would have liked to do was something along these lines:

# Defines options which will be inherited
- commonvariables:
  node: cvirt
  api_user: user@pve
  api_password: password
  api_host: 192.168.5.4

# Creates a host within proxmox environment
- hosts: localhost
  tasks:
  - name: "create vm"
    proxmox:
      inherits_options_from: commonvariables
      hostname: ansiblehost1
      storage: local-lvm
      timeout: 300
      ostemplate: 'local:vztmpl/centos-7-vagrant_amd64.tar.xz'
  - name: "start vm"
    proxmox:
      inherits_options_from: commonvariables
      hostname: ansiblehost
      state: started

But I can't find anything that would allow me to do something like this.

I think I could do it with roles, Or I could do it with loops. But then what if I wanted to pass an extra option in some cases (netif, say)?

Any idea how I can achieve this 'options inheritance'?

In case it matters, I running: ansible 2.3.1.0

precchia
  • 35
  • 5
  • You could pass in a command line var and then use j2 filters to union it with the default set that you have already defined in code, for the extra option. – Shawn Mehan Aug 05 '17 at 18:00
  • You can make custom action plugin and fill in common parameters from other variables before calling actual module. See this for code example: https://stackoverflow.com/questions/45074728/use-ansible-facts-in-module-code/45082984#45082984 – Konstantin Suvorov Aug 07 '17 at 08:59

0 Answers0