gpt4 book ai didi

仅在需要升级时停止服务的 Ansible 方法

转载 作者:行者123 更新时间:2023-12-05 00:52:31 24 4
gpt4 key购买 nike

在 ansible playbook 中,如果需要升级,我想停止 MariaDB(在我的情况下,从 RPM 包重新启动并不总是有效)。我对ansible很陌生。

我想出了这个:

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

- name: "Stop mysql service"
service:
name: mysql
state: stopped
when: needs_update.stdout == "1"

有没有比执行 shell 命令更好的方法呢?运行它时,我收到警告:
TASK [mariadb_galera : Check if MariaDB needs to be upgraded]    ******************
changed: [139.162.220.42] => {"changed": true, "cmd": "yum check-update MariaDB-server|grep MariaDB|wc -l", "delta": "0:00:00.540862", "end": "2017-03-01 13:03:34.415272", "rc": 0, "start": "2017-03-01 13:03:33.874410", "stderr": "", "stdout": "0", "stdout_lines": ["0"], "warnings": ["Consider using yum module rather than running yum"]}
[WARNING]: Consider using yum module rather than running yum

谢谢!

最佳答案

您可以通过以下方式隐藏警告:

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

或者你可以欺骗 Ansible 执行 yum任务在 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

请在使用前测试此代码。

关于仅在需要升级时停止服务的 Ansible 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42532659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com