温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - Take action on negative exit from where
ansible python

python - 对从此处退出的负面出口采取行动

发布于 2020-04-08 00:08:26

我有一本手册,其中检查了我是否可以安装该代理,我想做什么以及以下情况:当我的某个地方存在被忽略的代理时,例如,我在其中检查是否存在/opt/Agent/目录,否则我会想创建此目录,该怎么做?

---
- hosts: all
  tasks:

  - name: 'Ensure that free space on /opt/Agent/ is grater than 1.5GB'
    assert:
      that: item.size_available >= 1500000000
      msg: 'disk space has reached 1.5GB threshold'
    when: item.mount == mountname
    with_items: '{{ ansible_mounts }}'

  - name: Ansible check agent is present.
    stat:
      path: /etc/init.d/agent
    register: file_details

  - debug:
      msg: "Agent is present"
    when: file_details.stat.exists

  - name: Ansible check directory Agent exists.
    stat:
      path: /opt/Agent/
    register: files_to_delete

  - debug:
      msg: "Directory Agent exists"
    when: files_to_delete.stat.exists and files_to_delete.stat.isdir

  vars:
    mountname: '/opt/Agent/'

查看更多

提问者
Luis Henrique
被浏览
85
Sebastián Greco 2020-02-01 04:50

您可以只使用“文件”模块来确保目录存在,否则可以创建目录。

---
- name: over
  hosts: localhost

  tasks:
   - name: makes sure directory exists or creates it
     file:
       state: directory
       path: /opt/Agent

如果使用此任务,则可以避免所有仅用于“确保”路径存在的验证。