最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ansible - How to skip a handler if another handler has been run? - Stack Overflow

programmeradmin0浏览0评论

I have a playbook with two handlers:

  • Restart the Chrony service
  • Reload the Chrony sources

It is not necessary to run the second handler, after the first has been executed. In that case, how can I tell Ansible to skip the second handler?

Example:

- name: Configure NTP
  hosts: jumphost
  become: true
  tasks:
    - name: "Uninstall unwanted timesyncer"
      apt:
        state: absent
        name:
          - ntp
          - systemd-timesyncd
    - name: "Install Chrony"
      apt: state=present name=chrony
    - name: "Disable Debian pool servers"
      lineinfile:
        path: /etc/chrony/chrony.conf
        regexp: '^pool.*'
        backrefs: true
        line: '#\g<0>'
      notify: Restart Chrony service
    - name: "Configure example ntp server"
      copy:
        dest: /etc/chrony/sources.d/example.sources
        owner: root
        group: root
        mode: 0644
        content: |
          server ntp.example.de iburst
      notify: Reload Chrony sources
  handlers:
    - name: Restart Chrony service
      service: name=chronyd state=restarted
    - name: Reload Chrony sources
      command: chronyc reload sources

I have a playbook with two handlers:

  • Restart the Chrony service
  • Reload the Chrony sources

It is not necessary to run the second handler, after the first has been executed. In that case, how can I tell Ansible to skip the second handler?

Example:

- name: Configure NTP
  hosts: jumphost
  become: true
  tasks:
    - name: "Uninstall unwanted timesyncer"
      apt:
        state: absent
        name:
          - ntp
          - systemd-timesyncd
    - name: "Install Chrony"
      apt: state=present name=chrony
    - name: "Disable Debian pool servers"
      lineinfile:
        path: /etc/chrony/chrony.conf
        regexp: '^pool.*'
        backrefs: true
        line: '#\g<0>'
      notify: Restart Chrony service
    - name: "Configure example ntp server"
      copy:
        dest: /etc/chrony/sources.d/example.sources
        owner: root
        group: root
        mode: 0644
        content: |
          server ntp.example.de iburst
      notify: Reload Chrony sources
  handlers:
    - name: Restart Chrony service
      service: name=chronyd state=restarted
    - name: Reload Chrony sources
      command: chronyc reload sources
Share Improve this question edited Jan 22 at 9:45 NateDhaliwal 1431 silver badge12 bronze badges asked Jan 21 at 11:29 cevingceving 23.9k15 gold badges117 silver badges196 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default -2

I think I got it. It is possible to register in a handler.

- name: Configure NTP
  hosts: jumphost
  become: true
  tasks:
    - name: "Uninstall unwanted timesyncer"
      apt:
        state: absent
        name:
          - ntp
          - systemd-timesyncd
    - name: "Install Chrony"
      apt: state=present name=chrony
    - name: "Disable Debian pool servers"
      lineinfile:
        path: /etc/chrony/chrony.conf
        regexp: '^pool.*'
        backrefs: true
        line: '#\g<0>'
      notify: Restart Chrony service
    - name: "Configure example ntp server"
      copy:
        dest: /etc/chrony/sources.d/example.sources
        owner: root
        group: root
        mode: 0644
        content: |
          server ntp.example.de iburst
      notify: Reload Chrony sources
  handlers:
    - name: Restart Chrony service
      service: name=chronyd state=restarted
      register: restart
    - name: Reload Chrony sources
      command: chronyc reload sources
      when: "restart is undefined or not restart.changed or restart.failed"
发布评论

评论列表(0)

  1. 暂无评论