gpt4 book ai didi

ansible - 如果启用了 ufw,如何运行 ansible 任务?

转载 作者:行者123 更新时间:2023-12-02 19:41:15 27 4
gpt4 key购买 nike

仅当 Ubuntu 服务器上启用了 ufw 时,我才需要运行一些任务。

伪代码如下:

 - name: detect if ufw is enabled
magical_module: ???
register: ufw_is_enabled

- name: my task
when: ufw_is_enabled

我只看到这个不优雅的解决方案:

  • 远程执行ufs状态
  • 解析输出:
root@test:~# ufw status
Status: inactive

也许 ufw 启用时有一些文件?在这种情况下,可以使用 stat 模块。还有其他解决方案吗?

最佳答案

大致如下:

    - name: check whether ufw status is active
shell: ufw status
changed_when: false
register: ufw_check

- debug:
msg: "{{ ufw_check }}"

- name: do something if ufw is enabled
shell: echo
when: "'inactive' not in ufw_check.stdout"

确保使用become: true,因为需要 root 权限才能成功执行该命令。

要使用 UFW 模块管理系统,see this Ansible module

例如,您可以在想要禁用 UFW 的系统上禁用 UFW:

    - name: Disable UFW on hosts
ufw:
state: disabled # unloads firewall and disables firewall on boot.

关于ansible - 如果启用了 ufw,如何运行 ansible 任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60095403/

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