任务之间的Ansible共享参数

时间:2019-01-09 18:18:27

标签: ansible

这可能是一个基本问题。我正在使用以下Ansible模块,并希望通过减少重复的行/变量来简化我的剧本。 https://github.com/HewlettPackard/hpe3par_ansible_module

我看到每个任务都引用了使用相同连接参数的模块。这些变量已经在参数文件中定义,但是有一种方法可以将参数移动到更全局的位置,因此不必在每个任务中都重复该变量。

我的剧本:

---
- 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 }}"

所需的剧本。

---
- 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"

1 个答案:

答案 0 :(得分:0)

一个选择是使用include_tasks。参见下面的示例。

> 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"