如何在一项或另一项任务中注册 var [英] How to register a var in either one or another task
问题描述
这个任务集合不像我希望的那样工作:
This task collection doesn't work as I hoped it would:
- name: Find out whether syslog-ng is installed (yum)
tags: syslog_forwarding
command: yum -q list installed syslog-ng
register: syslog_ng_check
failed_when: False
changed_when: False
when: ansible_pkg_mgr == 'yum'
- name: Find out whether syslog-ng is installed (apt)
tags: syslog_forwarding
command: dpkg -s syslog-ng
register: syslog_ng_check
failed_when: False
changed_when: False
when: ansible_pkg_mgr == 'apt'
- name: Configure syslog-ng to forward all logs to syslog servers (apt)
tags: syslog_forwarding
template:
src: syslog_ng_forward_all.conf.j2
dest: /etc/syslog-ng/conf.d/syslog_forward.conf
notify: restart syslog_ng
when: syslog_ng_check is defined and syslog_ng_check.rc == 0
- name: Configure syslog-ng to forward all logs to syslog servers (yum)
tags: syslog_forwarding
template:
src: syslog_ng_forward_all.conf.j2
dest: /etc/syslog-ng/conf.d/syslog_forward.conf
notify: restart syslog_ng
when: syslog_ng_check is defined and syslog_ng_check.rc == 0
我想首先查明系统上是否安装了 syslog-ng
(系统可能是 CentOS
或 Debian/Ubuntu
code>),然后相应地采取行动(放置配置文件),并保持独立放置配置文件分发的任务.我发现如果第一个任务实际上返回 0 并将 syslog-ng-check.rc
设置为 0,那么第二个任务使它再次未定义(或至少为空),即使任务被跳过(因为没有系统可以同时使用 yum
和 apt
,至少不是我的).
I wanted to first find out whether syslog-ng
was installed on the system (the system might either be a CentOS
or a Debian/Ubuntu
), and then act accordingly (place a config file) when so, and keep the task to place the config file distribution independent.
What I have found out is that if the first task actually returns 0 and sets syslog-ng-check.rc
to 0, then the second task makes it be undefined again (or at least, empty), even though the task is skipped (as no system can be using yum
and apt
at the same time, at least not mine).
当然,我可以为每个 yum
和 apt
检查注册不同的变量,但是我需要在我想要避免的 when: 子句中使用更多逻辑.
Of course, I could register different variables for each yum
and apt
checks, but then I would need more logic in my when: clause which I wanted to avoid.
对此有什么好的想法吗?
Any good ideas on this one?
推荐答案
只有解决方法.跳过的任务将始终覆盖已注册的变量.
There are only workarounds. Skipped tasks will always overwrite registered variable.
一种可能的方法:
- name: Find out whether syslog-ng is installed (yum)
tags: syslog_forwarding
command: yum -q list installed syslog-ng
register: syslog_ng_check
failed_when: False
changed_when: False
when: ansible_pkg_mgr == 'yum'
- set_fact:
syslog_ng_flag: True
when: syslog_ng_check.rc == 0
- name: Find out whether syslog-ng is installed (apt)
tags: syslog_forwarding
command: dpkg -s syslog-ng
register: syslog_ng_check
failed_when: False
changed_when: False
when: ansible_pkg_mgr == 'apt'
- set_fact:
syslog_ng_flag: True
when: syslog_ng_check.rc == 0
- name: Configure syslog-ng to forward all logs to syslog servers (apt)
tags: syslog_forwarding
template:
src: syslog_ng_forward_all.conf.j2
dest: /etc/syslog-ng/conf.d/syslog_forward.conf
notify: restart syslog_ng
when: syslog_ng_flag | default(false)
这篇关于如何在一项或另一项任务中注册 var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!