Budhac
Budhac

Reputation: 33

ansible sharing parameters between tasks

This may be an elementary question. I am using the following Ansible modules and was wanting to streamline my playbooks by reducing duplicate lines/variables. https://github.com/HewlettPackard/hpe3par_ansible_module

I see that each task references a module that is using the same connection parameters. The variables are already defined in a parameters file but is there a way to move parameters to be more global so it doesn't have to be repeated in each task.

My playbook:

---
- name: Create 3PAR host and volume
  hosts: localhost
  tasks:
    - name: Load Storage System Vars
      include_vars: 'properties/storage_system_properties.yml'

    - name: Load Host Vars
      include_vars: 'properties/host_properties.yml'

    - name: Create Host "{{ host_name }}"
      hpe3par_host:
        storage_system_ip="{{ storage_system_ip }}"
        storage_system_username="{{ storage_system_username }}"
        storage_system_password="{{ storage_system_password }}"
        state=present
        host_name="{{ host_name }}"
        host_persona="{{ host_persona }}"
        host_domain="{{ host_domain }}"
        host_iscsi_names="{{ host_iscsi_names }}"

    - name: Create Volume "{{ volume_name }}"
      hpe3par_volume:
        storage_system_ip="{{ storage_system_ip }}"
        storage_system_username="{{ storage_system_username }}"
        storage_system_password="{{ storage_system_password }}"
        state=present
        volume_name="{{ volume_name }}"
        cpg="{{ cpg }}"
        size="{{ size }}"

    - name: Create VLUN
      hpe3par_vlun:
        storage_system_ip="{{ storage_system_ip }}"
        storage_system_username="{{ storage_system_username }}"
        storage_system_password="{{ storage_system_password }}"
        state=export_volume_to_host
        volume_name="{{ volume_name }}"
        host_name="{{ host_name }}"

Desired playbook.

---
- name: Create 3PAR host and volume
  hosts: localhost

  vars_file: 
    - properties/storage_system_properties.yml

  tasks:

    - name: Load Host Vars
      include_vars: 'properties/host_properties.yml'

    - name: Create Host "{{ host_name }}"
      hpe3par_host:
        state=present
        host_name="{{ host_name }}"
        host_persona="{{ host_persona }}"
        host_domain="{{ host_domain }}"
        host_iscsi_names="{{ host_iscsi_names }}"

    - name: Create Volume "{{ volume_name }}"
      hpe3par_volume:
        state=present
        volume_name="{{ volume_name }}"
        cpg="{{ cpg }}"
        size="{{ size }}"

    - name: Create VLUN
      hpe3par_vlun:
        state=export_volume_to_host
        volume_name="{{ volume_name }}"
        host_name="{{ host_name }}"

properties/storage_system_properties.yml

storage_system_ip: "192.168.1.10"
storage_system_username: "3paruser"
storage_system_password: "3parpass"

Upvotes: 0

Views: 354

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68124

An option would be to use include_tasks. See example below.

> cat tasks-001.yml
- debug: msg="{{ var_001 }}-{{ var_002 }}-{{ var_003 }}"

> cat test-19.yml
- hosts: localhost
  gather_facts: no
  vars:
    var_001: "001"
    var_002: "002"
    var_003: "003"
  tasks:
    - include_tasks: tasks-001.yml
    - include_tasks: tasks-001.yml
      vars:
        var_003: "444"
    - include_tasks: tasks-001.yml
      vars:
        var_003: "555"
    - include_tasks: tasks-001.yml
      vars:
        var_001: "111"
        var_002: "222"
    - include_tasks: tasks-001.yml
      vars:
        var_001: "111"
        var_002: "222"
        var_003: "333"
    - include_tasks: tasks-001.yml

> ansible-playbook test-19.yml | grep msg
    "msg": "001-002-003"
    "msg": "001-002-444"
    "msg": "001-002-555"
    "msg": "111-222-003"
    "msg": "111-222-333"
    "msg": "001-002-003"

Upvotes: 0

Related Questions