如何正确使用Ansible变量?

时间:2018-11-02 10:30:25

标签: ansible

- name: Create Dirs for rs disk
  file:
    name: "{{ /rs{{ items }}"
    state: directory
  with_sequence: start=1 end={{ disk_group[inventory_hostname]['rs'] | length }}
  when: disk_group[inventory_hostname]['rs'] is defined

tasks

当“ when”子句为false时,“ with_sequence”中未定义如何避免可变的错误。

事实上,使用ignore_errors可以做到这一点,但我确实认为这不是一个好主意。

1 个答案:

答案 0 :(得分:0)

您可能想尝试 include_tasks

的此示例
> cat task.yml
  - debug: var=item
    with_sequence: start=1 end={{ rs|length }}

> cat playbook.yml
  - hosts: localhost
    vars:
  #    rs: test
    tasks:
      - include_tasks: task.yml
        when: rs is defined