验证ansible-playbook中额外的变量值是否正确

问题描述 投票:0回答:1

我有一个 ansible 剧本,需要传递 --extra-vars 并且想知道是否有任何方法可以验证该值是否已定义列表变量之一

示例测试.yml

- hosts: localhost
  gather_facts: false
  vars:
    prod_mgmt_network: "{{ ['server00']|product(range(start,end))|map('join')|list }}"
    start: 4281
    end: 4283
  tasks:
    - debug:
        var: prod_mgmt_network

示例命令

ansible-playbook --extra-vars hostName=server004181 test.yml

如果 --extra-vars hostName 值不是定义的变量之一,我想要一个执行失败的任务

ansible
1个回答
0
投票

您可以使用

assert
fail
任务:

- name: check that variable is defined using assert
  assert:
    that: hostname is defined
    fail_msg: "You must define the variable 'hostname'"

或者:

- name: check that variable is defined using fail
  when: hostname is not defined
  fail:
    msg: "You must define the 'hostname' variable"
© www.soinside.com 2019 - 2024. All rights reserved.