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

Ansible | The conditional check 'item.stat.exists' failed

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

感谢您的回答!

您可以使用:

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

鉴于库存

shell> cat hosts
[test]
10.1.0.51 serverid='1'
10.1.0.52 serverid='2'

剧本

shell> cat pb.yml
- hosts: test

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

  tasks:

    - name: Check directory exists or not.
      stat:
        path: "/tmp/{{ item.1 }}"
      register: alarm_details
      when: serverid|int == item.0|int
      loop: "{{ devangtest }}"

    - set_fact:
        dir_stat: "{{ alarm_details.results|
                      json_query('[].{dir: item[1], stat: stat.exists}') }}"

    - debug:
        msg: "/tmp/{{ item }} exists"
      loop: "{{ dir_stat|json_query('[?stat].dir') }}"

    - name: Create a directory if it does not exist
      file:
        path: "/tmp/{{ item }}/test2"
        state: directory
      loop: "{{ dir_stat|json_query('[?stat].dir') }}"

当 none 个目录存在时给出

shell> ansible-playbook pb.yml

PLAY [test] ***

TASK [Check directory exists or not.] ***
skipping: [10.1.0.52] => (item=[u'1', u'adsdsdasd']) 
ok: [10.1.0.51] => (item=[u'1', u'adsdsdasd'])
skipping: [10.1.0.51] => (item=[u'2', u'kafka2sda']) 
skipping: [10.1.0.51] => (item=[u'2', u'fggfdfgdf']) 
ok: [10.1.0.52] => (item=[u'2', u'kafka2sda'])
ok: [10.1.0.52] => (item=[u'2', u'fggfdfgdf'])

TASK [set_fact] ***
ok: [10.1.0.51]
ok: [10.1.0.52]

TASK [debug] ***

TASK [Create a directory if it does not exist] ***

PLAY RECAP ***
10.1.0.51: ok=2 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0   
10.1.0.52: ok=2 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0

主机 test_01 的现有目录 /tmp/adsdsdasd 剧本给出

shell> ansible-playbook pb.yml

PLAY [test] ***

TASK [Check directory exists or not.] ***
skipping: [10.1.0.52] => (item=[u'1', u'adsdsdasd']) 
ok: [10.1.0.52] => (item=[u'2', u'kafka2sda'])
ok: [10.1.0.51] => (item=[u'1', u'adsdsdasd'])
skipping: [10.1.0.51] => (item=[u'2', u'kafka2sda']) 
skipping: [10.1.0.51] => (item=[u'2', u'fggfdfgdf']) 
ok: [10.1.0.52] => (item=[u'2', u'fggfdfgdf'])

TASK [set_fact] ***
ok: [10.1.0.51]
ok: [10.1.0.52]

TASK [debug] ***
ok: [10.1.0.51] => (item=adsdsdasd) => {
    "msg": "/tmp/adsdsdasd exists"
}

TASK [Create a directory if it does not exist] ***
changed: [10.1.0.51] => (item=adsdsdasd)

PLAY RECAP ***
10.1.0.51: ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0   
10.1.0.52: ok=2 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0