仅在本地主机上运行 ansible 任务一次

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

我有下面的伪代码要在 ansible 中实现。

changed = true

if (changed)
{
   print("The first block executes")
}
else
{
  print("The second block executes.")
  changed = false
}

现在我需要将上述逻辑转换为ansible playbook,这样当我第一次运行playbook时,它会打印第一个块,并且所有后续调用都必须打印第二个块。我在下面尝试过,但没有成功。

hosts: localhost
become: true
vars:
  changed: "true"
tasks:
  - name: First block
    debug:
      msg: "The first block is printed"
    when: changed == "true"
  - name: Second block
    debug:
      msg: "The second block is printed"
  - set_fact:
      changed: "false"

但是上述逻辑对我不起作用。如果这个用例可以在 ansible 中实现,有人可以指导我吗?

ansible ansible-2.x ansible-inventory
1个回答
0
投票

删除变量周围的引号。

- name: First block
  debug:
    msg: "The first block is printed"
  when: changed

- name: Second block
  debug:
    msg: "The second block is printed"
  when: not changed

Ansible 基于 python,是“真实的”。
这应该按预期工作。

© www.soinside.com 2019 - 2024. All rights reserved.