首页 > 解决方案 > Ansible 迭代统计结果并创建目录

问题描述

致命:[172.24.66.221]:失败!=> {“msg”:“条件检查've_exists.stat.exists == False'失败。错误是:评估条件时出错(ve_exists.stat.exists == False):'dict object'没有属性' stat'\n\n错误似乎出现在“/var/lib/awx/projects/_47__tomcat_debug/test.yml”中:第 95 行,第 9 列,但可能\n在文件中的其他位置,具体取决于确切的语法问题。 \n\n违规行似乎是:\n\n\n - name: Create the directory\n ^ here\n"} fatal: [172.24.66.173]: FAILED! => {“msg”:“条件检查've_exists.stat.exists == False'失败。错误是:评估条件时出错(ve_exists.stat.exists == False):'dict object'没有属性'统计'

- name: Run the tasks inside the block
    block:
      - name: check the directory if already exist
        stat:
          path: /vtxnfs/{{ item }}
        with_items:
          - "vera_repository"
        register: ve_exists

      - name: Create the directory
        file:
          path: /vtxnfs/{{ item }}
          state: directory
          owner: "{{ user }}"
          group: "{{ user }}"
          mode: "0755"
        with_items:
          - "vera_repository"
        when: ve_exists.stat.exists == False

标签: ansible

解决方案


试试下面的代码,它将检查目录的存在。

  • 主机:本地主机任务:

    • 名称:Ansible 检查目录。统计:路径:/project/devops/test 寄存器:my_folder

    • 名称:“如果目录已存在则回显”调试:味精:“测试目录已存在”何时:my_folder.stat.exists

    • 名称:“Ansible 创建目录,如果不存在”文件:路径:/project/devops/test 状态:目录模式:0755 组:root 所有者:root 时间:my_folder.stat.exists == false


推荐阅读