我想要执行这个状态:
backup_log_readable:
acl.present:
- name: /var/log/backup
- acl_type: user
- acl_name: monitor
- perms: rx
- recurse: True
但这仅应在/var/log/backup
存在的情况下发生。
如何在盐中实现这样的条件?
答案1
除了此onlyif: test -d /var/log/backup
方法(可能是最好的方法)之外,您还可以:
1)如果出现以下情况,请用 jinja 包裹所有内容:
{%- if salt['file.directory_exists' ]('/var/log/backup') %}
backup_log_readable:
acl.present:
- name: /var/log/backup
- acl_type: user
- acl_name: monitor
- perms: rx
- recurse: True
{%- endif %}
2)以file.exists
状态为必要条件:
backup_log_exists:
file.exists:
- name: /var/log/backup
backup_log_readable:
acl.present:
- name: /var/log/backup
- acl_type: user
- acl_name: monitor
- perms: rx
- recurse: True
- require:
- file: backup_log_exists
这样,您将在摘要中看到失败状态,因此它不会像 onlyif 和 jinja 方法那样完全透明
3)运行acl状态之前创建目录:
backup_log_dir:
file.directory:
- name: /var/log/backup
backup_log_readable:
acl.present:
- name: /var/log/backup
- acl_type: user
- acl_name: monitor
- perms: rx
- recurse: True
- require:
- file: backup_log_dir
答案2
Onlyif 应该起作用:
- onlyif:
- test -d /var/log/backup
看: https://docs.saltstack.com/en/latest/ref/states/requisites.html#onlyif