Ansible |条件检查'item.stat.exists'失败

问题描述 投票:0回答:1
**Getting an error like :**

"msg": "The conditional check 'item.stat.exists' failed. The error was: error while evaluating conditional (item.stat.exists): 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'stat'\n\nThe error appears to be in '/Ansible/roles/test/tasks/test1.yml': line 16, column 3, 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"

我有以下代码:

**Inventory File :**

[test]
192.168.0.61 serverid=2
192.168.0.60 serverid=1

**Variable File :**

devangtest:
  - ['1','adsdsdasd']
  - ['2','kafka2sda']
  - ['2','fggfdfgdf']

**Task File:**

- name: Check directory exists or not.
  stat:
    path: "/tmp/{{ item[1] }}"
  register: alarm_details
  when: "{{ serverid }} == {{ item[0] }}"
  with_items:
    - "{{ devangtest }}"
  ignore_errors: yes


- debug:
    msg: "{{ alarm_details.results }}"

- debug:
    msg: "The file or directory exists"
  when: item.stat.exists
  with_items:
    -  alarm_details.results
#  when: item.stat.isdir
  ignore_errors: yes

- name: Create a directory if it does not exist
  file:
    path: "/tmp/{{ item[1] }}/test2"
    state: directory
  when:
   - item.stat.exists == true
   - "{{ serverid }} == {{ item[0] }}"
  with_items:
    - "{{ alarm_details.results }}"
    - "{{ devangtest }}"

我想做的是检查文件夹以及它们是否不存在

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

感谢您的回答!

ansible ansible-2.x ansible-inventory ansible-facts ansible-template
1个回答
0
投票

您可以使用:

  - name: Create a directory if it does not exist
  file:
    path: "/tmp/{{ item.1[1] }}/test2"
    state: directory
  when:
    - item.0.stat.exists = True 
    - "{{ serverid }} == {{ item.1[0] }}"
  with_together:
    - '{{ alarm_details.results }}'
    - '{{ devangtest }}'
© www.soinside.com 2019 - 2024. All rights reserved.