【问题标题】:How to run an ansible task if ufw is enabled?如果启用了 ufw,如何运行 ansible 任务?
【发布时间】:2020-02-06 12:49:53
【问题描述】:

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

伪代码如下:

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

 - name: my task
   when: ufw_is_enabled

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

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

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

【问题讨论】:

    标签: ansible ufw


    【解决方案1】:

    类似的东西:

        - name: check whether ufw status is active
          shell: ufw status
          changed_when: False
          ignore_errors: True
          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,因为这是必需的。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 2021-01-08
      • 2018-12-21
      • 1970-01-01
      • 2014-07-19
      相关资源
      最近更新 更多