如何在ansible中定义变量时运行任务?

时间:2021-02-26 07:05:09

I am looking for a way to perform a task when ansible variable is not registers /undefined e.g

当ansible变量不是寄存器/未定义时,我正在寻找一种执行任务的方法,例如

-- name: some task
   command:  sed -n '5p' "{{app.dirs.includes}}/BUILD.info" | awk '{print  $2}'
   when: (! deployed_revision) AND ( !deployed_revision.stdout )
   register: deployed_revision

3 个解决方案

#1


From the ansible docs: If a required variable has not been set, you can skip or fail using Jinja2’s defined test. For example:

来自ansible docs:如果尚未设置必需变量,则可以跳过或失败使用Jinja2定义的测试。例如:

tasks:

- shell: echo "I've got '{{ foo }}' and am not afraid to use it!"
  when: foo is defined

- fail: msg="Bailing out. this play requires 'bar'"
  when: bar is not defined

So in your case, when: deployed_revision is not defined should work

所以在你的情况下,当:deploy_revision没有定义应该工作

#2


As per latest Ansible Version 2.5, to check if a variable is defined and depending upon this if you want to run any task, use undefined keyword.

根据最新的Ansible Version 2.5,要检查是否定义了变量,如果要运行任何任务,请使用undefined关键字。

tasks:
    - shell: echo "I've got '{{ foo }}' and am not afraid to use it!"
      when: foo is defined

    - fail: msg="Bailing out. this play requires 'bar'"
      when: bar is undefined

Ansible Documentation

#3


Strictly stated you must check all of the following: defined, not empty AND not None.

严格说明您必须检查以下所有内容:已定义,非空且不是无。

For "normal" variables it makes a difference if defined and set or not set. See foo and bar in the example below. Both are defined but only foo is set.

对于“正常”变量,如果定义和设置或未设置,则会产生差异。请参阅下面的示例中的foo和bar。两者都已定义,但只设置了foo。

On the other side registered variables are set to the result of the running command and vary from module to module. They are mostly json structures. You probably must check the subelement you're interested in. See xyz and xyz.msg in the example below:

另一方面,已注册的变量设置为运行命令的结果,并且因模块而异。它们大多是json结构。您可能必须检查您感兴趣的子元素。请参阅下面的示例中的xyz和xyz.msg:

cat > test.yml <<EOF
- hosts: 127.0.0.1

  vars:
    foo: ""          # foo is defined and foo == '' and foo != None
    bar:             # bar is defined and bar != '' and bar == None

  tasks:

  - debug:
      msg : ""
    register: xyz    # xyz is defined and xyz != '' and xyz != None
                     # xyz.msg is defined and xyz.msg == '' and xyz.msg != None

  - debug:
      msg: "foo is defined and foo == '' and foo != None"
    when: foo is defined and foo == '' and foo != None

  - debug:
      msg: "bar is defined and bar != '' and bar == None"
    when: bar is defined and bar != '' and bar == None

  - debug:
      msg: "xyz is defined and xyz != '' and xyz != None"
    when: xyz is defined and xyz != '' and xyz != None
  - debug:
      msg: "{{ xyz }}"

  - debug:
      msg: "xyz.msg is defined and xyz.msg == '' and xyz.msg != None"
    when: xyz.msg is defined and xyz.msg == '' and xyz.msg != None
  - debug:
      msg: "{{ xyz.msg }}"
EOF
ansible-playbook -v test.yml

#1


From the ansible docs: If a required variable has not been set, you can skip or fail using Jinja2’s defined test. For example:

来自ansible docs:如果尚未设置必需变量,则可以跳过或失败使用Jinja2定义的测试。例如:

tasks:

- shell: echo "I've got '{{ foo }}' and am not afraid to use it!"
  when: foo is defined

- fail: msg="Bailing out. this play requires 'bar'"
  when: bar is not defined

So in your case, when: deployed_revision is not defined should work

所以在你的情况下,当:deploy_revision没有定义应该工作

#2


As per latest Ansible Version 2.5, to check if a variable is defined and depending upon this if you want to run any task, use undefined keyword.

根据最新的Ansible Version 2.5,要检查是否定义了变量,如果要运行任何任务,请使用undefined关键字。

tasks:
    - shell: echo "I've got '{{ foo }}' and am not afraid to use it!"
      when: foo is defined

    - fail: msg="Bailing out. this play requires 'bar'"
      when: bar is undefined

Ansible Documentation

#3


Strictly stated you must check all of the following: defined, not empty AND not None.

严格说明您必须检查以下所有内容:已定义,非空且不是无。

For "normal" variables it makes a difference if defined and set or not set. See foo and bar in the example below. Both are defined but only foo is set.

对于“正常”变量,如果定义和设置或未设置,则会产生差异。请参阅下面的示例中的foo和bar。两者都已定义,但只设置了foo。

On the other side registered variables are set to the result of the running command and vary from module to module. They are mostly json structures. You probably must check the subelement you're interested in. See xyz and xyz.msg in the example below:

另一方面,已注册的变量设置为运行命令的结果,并且因模块而异。它们大多是json结构。您可能必须检查您感兴趣的子元素。请参阅下面的示例中的xyz和xyz.msg:

cat > test.yml <<EOF
- hosts: 127.0.0.1

  vars:
    foo: ""          # foo is defined and foo == '' and foo != None
    bar:             # bar is defined and bar != '' and bar == None

  tasks:

  - debug:
      msg : ""
    register: xyz    # xyz is defined and xyz != '' and xyz != None
                     # xyz.msg is defined and xyz.msg == '' and xyz.msg != None

  - debug:
      msg: "foo is defined and foo == '' and foo != None"
    when: foo is defined and foo == '' and foo != None

  - debug:
      msg: "bar is defined and bar != '' and bar == None"
    when: bar is defined and bar != '' and bar == None

  - debug:
      msg: "xyz is defined and xyz != '' and xyz != None"
    when: xyz is defined and xyz != '' and xyz != None
  - debug:
      msg: "{{ xyz }}"

  - debug:
      msg: "xyz.msg is defined and xyz.msg == '' and xyz.msg != None"
    when: xyz.msg is defined and xyz.msg == '' and xyz.msg != None
  - debug:
      msg: "{{ xyz.msg }}"
EOF
ansible-playbook -v test.yml