如何在一个或另一个任务中注册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
),然后在安装时相应地采取了行动(放置配置文件),并保持使配置文件分发独立的任务.
我发现的是,如果第一个任务实际上返回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.
对此有什么好主意吗?
推荐答案
只有解决方法.跳过的任务将始终覆盖注册变量.
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屋!