【问题标题】:Stat.exists with list of variables in ansibleStat.exists 与 ansible 中的变量列表
【发布时间】:2016-08-06 09:22:43
【问题描述】:

我在 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 }

然后,如果我尝试运行 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

我试过调试它:

- 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 与变量列表一起使用?

感谢您的回答!

【问题讨论】:

    标签: variables ansible jinja2 ansible-playbook


    【解决方案1】:

    问题是您在循环中注册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
    

    【讨论】:

      猜你喜欢
      • 2021-05-14
      • 1970-01-01
      • 2016-08-05
      • 2022-06-28
      • 2015-12-18
      • 2017-08-30
      • 1970-01-01
      • 2021-03-31
      • 2019-04-21
      相关资源
      最近更新 更多