Stat.exists 包含 ansible 中的变量列表

Stat.exists with list of variables in ansible

我在 Ansible 中使用字典检查现有文件时遇到问题。

  tasks:
  - name: Checking existing file id
    stat: path=/tmp/{{ item.id }}.conf
    with_items: "{{ file_vars }}"
    register: check_file_id

  - name: Checking existing file name
    stat: path=/tmp/{{ item.name }}.conf
    with_items: "{{ file_vars }}"
    register: check_file_name

  - name: Checking file exists
    debug: msg='File_id exists'
    when: check_file_id.stat.exists == True

  - name: Checking file name exists
    debug: msg='File name exists'
    when: check_file_name.stat.exists == True

  vars:
    file_vars:
      - { id: 1, name: one }
      - { id: 2, name: two }

然后,如果我尝试 运行 剧本,我收到错误:

FAILED! => {"failed": true, "msg": "The conditional check 'check_file_id.stat.exists == True' failed. The error was: error while evaluating conditional (check_file_id.stat.exists == True): 'dict' object has no attribute 'stat'\n\n

我试过调试它:

- debug: var=check_file_id 得到:

"results": [
    {
        "_ansible_item_result": true, 
        "_ansible_no_log": false, 
        "changed": false, 
        "invocation": {
            "module_args": {
                "checksum_algorithm": "sha1", 
                "follow": false, 
                "get_checksum": true, 
                "get_md5": true, 
                "mime": false, 
                "path": "/tmp/1.conf"
            }, 
            "module_name": "stat"
        }, 
        "item": {
            "id": 1, 
            "name": "one"
        }, 
        "stat": {
            "exists": false
        }
    }, 
    {
        "_ansible_item_result": true, 
        "_ansible_no_log": false, 
        "changed": false, 
        "invocation": {
            "module_args": {
                "checksum_algorithm": "sha1", 
                "follow": false, 
                "get_checksum": true, 
                "get_md5": true, 
                "mime": false, 
                "path": "/tmp/2.conf"
            }, 
            "module_name": "stat"
        }, 
        "item": {
            "id": 2, 
            "name": "two"
        }, 
        "stat": {
            "exists": false
        }
    }
]

我哪里错了? 是否可以将 stat.exists 与变量列表一起使用?

感谢解答!

问题是您正在循环注册 check_file_id。您需要阅读 the documentation on using register in a loop,其中讨论了这样做的含义。您的变量有一个 results 键,其中包含循环每次迭代的结果。您可以在 debug 输出中看到。

在后续任务中,您应该迭代 check_file_id.results 而不是 file_vars,像这样:

- hosts: localhost
  gather_facts: false
  vars:
    file_vars:
      - {id: 1, name: foo}
      - {id: 2, name: bar}
  tasks:
    - name: Checking existing file id
      stat:
        path: ./files/{{ item.id }}.conf
      with_items: "{{ file_vars }}"
      register: check_file_id

    - name: Checking existing file name
      stat:
        path: ./files/{{ item.name }}.conf
      with_items: "{{ file_vars }}"
      register: check_file_name

    - debug:
        msg: 'file id {{item.item.id}} (name {{item.item.name}}) exists'
      with_items: "{{ check_file_id.results }}"
      when: item.stat.exists

    - debug:
        msg: 'file name {{item.item.name}} (id {{item.item.id}}) exists'
      with_items: "{{ check_file_name.results }}"
      when: item.stat.exists