How to register a var in either one or another task

后端 未结 2 1593
灰色年华
灰色年华 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条回答
  • 2021-01-26 09:07

    Why not just slightly reorder your tasks to make your "configure" task immediately follow the test ("find out..") as a pair, i.e.:

    - name: Find out whether syslog-ng is installed (yum)
    - name: Configure syslog-ng to forward all logs to syslog servers (yum)
    - name: Find out whether syslog-ng is installed (yum)
    - name: Configure syslog-ng to forward all logs to syslog servers (apt)
    

    That way even if the other test resets syslog_ng_check you don't care because the you've already used it.

    Putting the find/configure pair for each of the two pkg managers in a block guarded by when: ansible_pkg_mgr == ... would be another way, and would make it more obvious that the ordering/grouping was important.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题