在Ansible playbook中测试哈希变量中所有成员的值

时间:2017-04-29 15:20:45

标签: ansible jinja2

只有在缺少预定义文件列表中的一个或多个文件时才需要运行任务。我尝试了以下(以及一些变体):

touch a

cat test.yml
- hosts: localhost
  vars:
    filelist:
      - a
      - b

  tasks:
  - stat:
      path: "{{ item }}"
    with_items: "{{ filelist }}"
    register: all_files

  - debug:
      var: all_files

  - debug:
      msg: "Some file(s) missing"
    when: "false in all_files['results'][*]['stat']['exists']"

ansible-playbook test.yml 
...
TASK [debug] ********************************************************************
ok: [localhost] => {
    "all_files": {
...
                "item": "a", 
                "stat": {
...
                    "exists": true, 
...
                "item": "b", 
                "stat": {
                    "exists": false
...
TASK [debug] ********************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "The conditional check 'false in all_files['results'][*]['stat']['exists']' failed. The error was: template error while templating string: unexpected '*'. String: {% if false in all_files['results'][*]['stat']['exists'] %} True {% else %} False {% endif %}\n\nThe error appears to have been in 'test.yml': line 16, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - debug:\n    ^ here\n"}
...

在以下情况下使用的正确语法是什么:'条款?或者这完全是错误的方式?

1 个答案:

答案 0 :(得分:1)

ls -la /home/selfbot