Warm tip: This article is reproduced from stackoverflow.com, please click
ansible python

Take action on negative exit from where

发布于 2020-04-07 23:20:04

I have a manual in which I check if I can install the agent, what I would like to do and the following, when there is an ignored one in mine where, for example, where I check if there is a /opt/Agent/ directory, otherwise I would like to create this directory, how to do this?

---
- 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/'
Questioner
Luis Henrique
Viewed
64
Sebastián Greco 2020-02-01 04:50

You could just use the "file" module to make sure the directory exists, or otherwise, create it.

---
- name: over
  hosts: localhost

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

If you use this task, then you can avoid all those verifications that only work to "make sure" the path exists.