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

时间:2020-03-28 04:06:43

标签: ansible ansible-2.x ansible-inventory ansible-facts ansible-template

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

感谢您的回答!

2 个答案:

答案 0 :(得分: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 
    - item.0.stat.exists
    - "{{ serverid }} == {{ item.1[0] }}"
  with_together:
    - '{{ alarm_details.results }}'
    - '{{ devangtest }}'

答案 1 :(得分:0)

提供库存

 private fun getCurrentFragment() :Fragment?{

        val fragment = (binding!!.pager.adapter as MyViewPager2Adapter).currentFragmentWeakReference?.get()

        retrun fragment
    }

剧本

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') }}"

该剧本在主机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 的现有目录/tmp/adsdsdasd中提供

test_01