Stat.exists与ansible中的变量列表一起存在

Mil*_*ilk 5 variables jinja2 ansible ansible-playbook

我在Ansible中使用dictonary检查现有文件时遇到问题.

  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 }
Run Code Online (Sandbox Code Playgroud)

然后,如果我试图运行playbook,我收到错误:

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
Run Code Online (Sandbox Code Playgroud)

我试过调试它:

- 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
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

哪里错了?是否可以使用stat.exists变量列表?

谢谢你的回答!

lar*_*sks 16

问题是你正在check_file_id循环注册.您需要阅读循环中使用的文档register,其中讨论了执行此操作的含义.您的变量有一个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
Run Code Online (Sandbox Code Playgroud)