根据Ansible中的条件添加命令标志

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

我有一个下面的命令写成ansible,

- set_fact:
    user_arg: true
    some_arg: "--with some-ver=1.0.0 if {{ user_arg is defined else '' }}"
    path: "path/to/file"
- command: "some-command {{ some_arg }} {{ path }}"

请注意,如果定义了--with参数,我要添加参数名称(some-var=1.0.0)及其值(user)。>>

我想使用args并将其切换为类似的外观>]

- set_fact:
    user_arg: true
    some_arg: "--with some-ver=1.0.0 if {{ user_arg is defined else '' }}"
    path: "path/to/file"
- command: "some-command {{ path }}"
  args:
    some_arg: "{{ some_arg }}"

但是这会将some_arg添加为空值,如果user_arg未定义,我根本不希望添加它。谁能帮我这个忙。

我有一个如下所示的命令,-an set_fact:user_arg:true some_arg:“ --with some-ver = 1.0.0,如果{{user_arg被定义为else``}}}” path:“ path / to /文件”-命令:“ some -...

ansible
2个回答
1
投票

您不能使用set_fact设置变量并在同一set_fact中使用它。 您必须将它们分解:

- set_fact:
    user_arg: true
    path: "path/to/file"

- set_fact:
    some_arg: "{% if user_arg is defined %}--with some-ver=1.0.0{% endif %}"

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.