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/'
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.
got it, thanks for the answer, a doubt, if you want to close the playbook when a condition has not been met, how to do?
You could use a task with the "meta" module with the "end_play" in combination with a "when" statement.
ok, thanks a lot !