How to register a var in either one or another task

后端 未结 2 1592
灰色年华
灰色年华 2021-01-26 08:19

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 in         


        
2条回答
  •  梦毁少年i
    2021-01-26 09:22

    There are only workarounds. Skipped tasks will always overwrite registered variable.

    One possible way:

    - 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)
    

提交回复
热议问题