Ansible way to stop a service only if it needs to be upgraded

后端 未结 2 884
滥情空心
滥情空心 2021-01-19 17:17

In an ansible playbook I want to stop MariaDB if an upgrade is needed (restart from the RPM package does not always work in my situation). I\'m quite new to ansible.

相关标签:
2条回答
  • 2021-01-19 17:56

    You can hide warning with:

    - name: "Check if MariaDB needs to be upgraded"
      shell: "yum check-update MariaDB-server|grep MariaDB|wc -l"
      args:
        warn: false
      register: needs_update
    

    Or you can trick Ansible to execute yum task in check_mode:

    - name: "Check if MariaDB needs to be upgraded (CHECK MODE!)"
      yum:
        name: MariaDB-server
        state: latest
      check_mode: yes
      register: needs_update_check
    
    - name: "Stop mysql service"
      service:
        name: mysql
        state: stopped
      when: needs_update_check | changed
    

    Please, test this code before use.

    0 讨论(0)
  • 2021-01-19 17:57

    The best way to handle this is use a "handler" eg something along the lines of

    tasks:
      - name: Update db
        yum: name=MariaDB-server state=latest
        notify:
          - stop db
    
    handlers:
      -  name: stop db  
         service: name=MariaDB-server state=stopped
    

    You can specify multiple handlers if you need to do multiple things, but if you just want to restart the db, use restarted instead of stopped

    http://docs.ansible.com/ansible/playbooks_best_practices.html

    0 讨论(0)
提交回复
热议问题