获取:评估条件(my_result.stat.exists)时出错:'bool object'没有属性'stat'而期望是字典

Getting : error while evaluating conditional (my_result.stat.exists): 'bool object' has no attribute 'stat' while expcting to be dictionary

我有这个 ansible 简单的剧本: 其中磁盘

上存在“/home/ec2-user/test1.txt”
---
- name: check if exsist
  hosts: localhost
  gather_facts: false
  tasks:

    - name: Get project name
      shell: "echo 'hhhhhhhh'"
      register: name_var


    - name: Check if thetest.log exists in path
      stat:
        path: "/home/ec2-user/test1.txt1"
      register: log_result_1

    - name: Check if TheTest.log exists in p_home
      stat:
        path: "/home/ec2-user/test1.txt"
      register: log_result_2
      when: name_var.stdout is match("hhhhhhhh")

    - set_fact:
        my_result: "{{ log_result_1.stat.exists or (log_result_2 is not skipped and log_result_2.stat.exists) }}"

    - debug:
        msg: "#################################### result_value ######################################"
    - debug:
        var: my_result


    - name: Create temporary log folder
      file:
        path: "/home/ec2-user/log_tmp"
        state: directory
      when: my_result.stat.exists

我希望 my_result 在这种情况下具有正确寄存器的值 log_result_2

但我收到此错误:

ansible-playbook 2.9.7
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/ec2-user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/ec2-user/venv_ansible_new/lib64/python3.7/site-packages/ansible
  executable location = /home/ec2-user/venv_ansible_new/bin/ansible-playbook
  python version = 3.7.6 (default, Feb 26 2020, 20:54:15) [GCC 7.3.1 20180712 (Red Hat 7.3.1-6)]
Using /etc/ansible/ansible.cfg as config file
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAYBOOK: test3.yml *************************************************************************************************************************************************************************************************************************
1 plays in test3.yml

PLAY [check if exsist] **********************************************************************************************************************************************************************************************************************
META: ran handlers

TASK [Get project name] *********************************************************************************************************************************************************************************************************************
task path: /home/ec2-user/test3.yml:7
Tuesday 05 May 2020  09:56:22 +0000 (0:00:00.070)       0:00:00.070 ***********
changed: [localhost] => changed=true
  cmd: echo 'hhhhhhhh'
  delta: '0:00:00.002594'
  end: '2020-05-05 09:56:22.684370'
  rc: 0
  start: '2020-05-05 09:56:22.681776'
  stderr: ''
  stderr_lines: <omitted>
  stdout: hhhhhhhh
  stdout_lines: <omitted>

TASK [Check if thetest.log exists in path] **************************************************************************************************************************************************************************************************
task path: /home/ec2-user/test3.yml:12
Tuesday 05 May 2020  09:56:22 +0000 (0:00:00.256)       0:00:00.327 ***********
ok: [localhost] => changed=false
  stat:
    exists: false

TASK [Check if TheTest.log exists in p_home] ************************************************************************************************************************************************************************************************
task path: /home/ec2-user/test3.yml:17
Tuesday 05 May 2020  09:56:22 +0000 (0:00:00.250)       0:00:00.577 ***********
ok: [localhost] => changed=false
  stat:
    atime: 1588660417.6106787
    attr_flags: ''
    attributes: []
    block_size: 4096
    blocks: 8
    charset: us-ascii
    checksum: 75bae7e3ee07809acd2477a8427b5b32e605a6b4
    ctime: 1588660417.6106787
    dev: 66305
    device_type: 0
    executable: false
    exists: true
    gid: 1000
    gr_name: ec2-user
    inode: 77678
    isblk: false
    ischr: false
    isdir: false
    isfifo: false
    isgid: false
    islnk: false
    isreg: true
    issock: false
    isuid: false
    mimetype: text/plain
    mode: '0664'
    mtime: 1588660417.6106787
    nlink: 1
    path: /home/ec2-user/test1.txt
    pw_name: ec2-user
    readable: true
    rgrp: true
    roth: true
    rusr: true
    size: 18
    uid: 1000
    version: '1015088889'
    wgrp: true
    woth: false
    writeable: true
    wusr: true
    xgrp: false
    xoth: false
    xusr: false

TASK [set_fact] *****************************************************************************************************************************************************************************************************************************
task path: /home/ec2-user/test3.yml:23
Tuesday 05 May 2020  09:56:23 +0000 (0:00:00.188)       0:00:00.765 ***********
ok: [localhost] => changed=false
  ansible_facts:
    my_result: true

TASK [debug] ********************************************************************************************************************************************************************************************************************************
task path: /home/ec2-user/test3.yml:26
Tuesday 05 May 2020  09:56:23 +0000 (0:00:00.030)       0:00:00.796 ***********
ok: [localhost] =>
  msg: '#################################### result_value ######################################'

TASK [debug] ********************************************************************************************************************************************************************************************************************************
task path: /home/ec2-user/test3.yml:28
Tuesday 05 May 2020  09:56:23 +0000 (0:00:00.011)       0:00:00.808 ***********
ok: [localhost] =>
  my_result: true

TASK [Create temporary log folder] **********************************************************************************************************************************************************************************************************
task path: /home/ec2-user/test3.yml:32
Tuesday 05 May 2020  09:56:23 +0000 (0:00:00.010)       0:00:00.819 ***********
fatal: [localhost]: FAILED! =>
  msg: |-
    The conditional check 'my_result.stat.exists' failed. The error was: error while evaluating conditional (my_result.stat.exists): 'bool object' has no attribute 'stat'

    The error appears to be in '/home/ec2-user/test3.yml': line 32, column 7, but may
    be elsewhere in the file depending on the exact syntax problem.

    The offending line appears to be:


        - name: Create temporary log folder
          ^ here

更改临时文件夹创建任务如下:

- name: Create temporary log folder
  file:
    path: "/home/ec2-user/log_tmp"
    state: directory
  when: my_result

my_result 已被 set_fact 任务设置为布尔值。这是您可以设置实际字典而不是布尔值的方法。

- set_fact:
    my_result: "{% if log_result_1.stat.exists %} {{ log_result_1 }} {% elif log_result_2 is not skipped and log_result_2.stat.exists %} {{ log_result_2 }} {% endif %}"