我想暂停我的 ansible 剧本,直到某个目录存在。 我知道我可以检查文件是否存在
- name: Wait until the file /home/myfile.txt is present before continuing
wait_for:
path: /home/myfile.txt
state: present
但这似乎不适用于目录。
我也知道我可以用这个示例代码检查一个目录
# Determine if a path exists and is a directory. Note that we need to test
# both that p.stat.isdir actually exists, and also that it's set to true.
- name: Get stats of the FS object
ansible.builtin.stat:
path: /path/to/something
register: p
- name: Print a debug message
ansible.builtin.debug:
msg: "Path exists and is a directory"
when: p.stat.isdir is defined and p.stat.isdir
是否可以将这两种方法结合起来?
一个最小的示例剧本
---
- hosts: test
become: false
gather_facts: false
tasks:
- name: Wait for trigger directory
ansible.builtin.wait_for:
path: "/home/{{ ansible_user }}/trigger/"
state: present
register: result
- name: Show result
debug:
var: result
跑步
TASK [Wait for trigger directory] *********************************************
ok: [test.example.com]
Thursday 02 March 2023 09:09:25 +0100 (0:00:08.185) 0:00:08.223 ********
并在远程节点上创建名为
trigger
的目录后
user@test:~$ pwd
/home/user
user@test:~$ mkdir trigger
将导致正确的预期行为
TASK [Show result] ************************************************************
ok: [test.example.com] =>
result:
changed: false
elapsed: 7
failed: false
gid: 1234567890
group: ansible_users
match_groupdict: {}
match_groups: []
mode: '0755'
owner: user
path: /home/user/trigger/
port: null
search_regex: null
secontext: unconfined_u:object_r:user_home_t:s0
size: 4096
state: directory
uid: 123456789
PLAY RECAP *********************************************************************
test.example.com : ok=2 changed=0 unreachable=0 failed=0 ...
Playbook run took 0 days, 0 hours, 0 minutes, 8 seconds
Thursday 02 March 2023 09:09:25 +0100 (0:00:00.107) 0:00:08.330 *********
================================================================================
Wait for trigger directory ----------------------------------------------- 8.19s
Show result -------------------------------------------------------------- 0.11s
更多文档
wait_for
模块 - 在继续之前等待条件 - 返回值
等待时经过的秒数elapsed