ansible - 如何使用 Ansible 重新加载 Firewalld 服务?

标签 ansible centos daemon firewalld

我在centos 7中用ansible向firewalld添加了一些规则。但我必须重新加载 firewalld 守护程序,以便服务正常工作。有什么想法吗?

这是我的ansible代码:

- name: Add port to firewalld
  firewalld:
    port: "{{ item }}"
    permanent: yes
    state: enabled
  when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux'
  loop:
    - 8080/tcp
    - 8000/tcp
    - 8090/tcp
    - 8040/tcp

最佳答案

首先使用with_items端口列表如下:

- name: Add port to firewalld
  firewalld:
    port: "{{ item }}"
    permanent: yes
    state: enabled
  when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux'
  loop:
    - 8080/tcp
    - 8000/tcp
    - 8090/tcp
    - 8040/tcp

如果端口不固定,您还可以使用以下代码输入端口并将其用作变量:
- hosts: localhost
  gather_facts: no
  vars_prompt:
    - name: ports
      prompt: "Enter port(s) number"
      private: no
  tasks:
    - name: add port
      firewalld:
            service: "{{ item }}"
            permanent: yes
            immediate: yes
            state: enabled
      with_items: "{{ ports.split(',') }}"

关于重新加载firewalld,它提到了here我们不能使用状态参数重新加载firewalld所以使用systemd模块如下:
- name: reload service firewalld
  systemd:
    name: firewalld
    state: reloaded

关于ansible - 如何使用 Ansible 重新加载 Firewalld 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60487848/

相关文章:

ansible - 将主机添加到 Ansible 主机文件

jenkins - 我在 CentOS7 上的 Jenkins 无法访问...为什么?

wordpress - 无法在 Wordpress 的 wp-content/uploads 中创建目录

没有 Rails 的 Ruby 的 `.present?`?

solr - 在 Ansible 中从主机构建动态命令

Ansible 检查作为变量提到的安装的磁盘空间

c++ - 我如何开始在类 Unix 操作系统(如 Linux)中编写守护进程?

c - 为什么我的守护进程在重新启动后没有打印到控制台?

asynchronous - 如何使用 ansible 中的包含来并行化循环

unix - 查找连接到服务器的所有 IP