尝试以某种方式在任务下添加一个when条件,使得在我们传递的--exta-vars值中应该有一个特定的字符串

时间:2019-04-11 00:10:17

标签: ansible

我正在尝试通过传递--extra-vars来运行一个有趣的剧本,如下所示: ansible-playbook -i tests/inventory.el --tags chromium tests/linux.yml -e "###_instance=ci.###.com" -u vn0suim --vault-password-file tests/secret

因此,剧本必须在--tag chromium主机文件中ips的服务器上使用inventory.el运行该特定任务

这是我写的任务:

- name: Install chromium chromium-headless and open-sans-fonts
  yum:
    name: "{{ item }}"
    loop:
      - https://###.com/content/repositories/centos-7/os/x86_64/Packages/dbus-1.10.24-12.el7.x86_64.rpm
      - https://###.com/content/repositories/centos-7/os/x86_64/Packages/dbus-libs-1.10.24-12.el7.x86_64.rpm
      - chromium
      - chromium-headless
      - open-sans-fonts
    state: present
  when: "{{looper_instance}} == ci.###.com"
  tags:
    - chromium

不幸的是,它失败并显示以下错误:

fatal: [###.ip-address]: FAILED! => {"msg": "The conditional check '{{##_instance}} == ci.###.com' failed. The error was: error while evaluating conditional ({{##_instance}} == ci.###.com): 'ci' is undefined\n\nThe error appears to have been in '/Users/vn0suim/.ansible/roles/###-role/tasks/linux_###.yml': line 300, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Install chromium chromium-headless and open-sans-fonts\n  ^ here\n"}

Mac计算机上的ansible和python版本是

ansible 2.7.9
python version = 3.7.3

我尝试以不同方式更改when条件。 一种这样的方式是

when: {{ ##_instance }} == "ci.###.com"

该任务必须成功在清单.el hosts文件中安装任务中提到的软件包

1 个答案:

答案 0 :(得分:0)

调试了好几次,找到了解决方案。认为与遇到类似问题的人分享解决方案可能会有所帮助。

而不是, when: {{ foo_instance }} == "ci.foo.com"

我们可以给, when: foo_instance == "ci.foo.com"