【问题标题】:Ansible playbook block ALL IP exclude one or more IPAnsible playbook block ALL IP 排除一个或多个 IP
【发布时间】:2018-03-07 09:41:45
【问题描述】:

我开始使用 Ansible 开发一个在系统 iptables 上执行一些操作的剧本。 我有一台服务器,我想阻止除一个或多个 IP 之外的所有 IP。

我真的不知道如何使用 ansible 模块编写 iptables 规则。我需要:

  1. 丢弃所有传入流量 (iptables -P INPUT DROP)
  2. 丢弃所有传入流量 (iptables -P INPUT DROP)
  3. 丢弃所有转发的流量 (iptables -P FORWARD DROP)
  4. 允许所有传出流量 (iptables -P OUTPUT ACCEPT)
  5. iptables -A INPUT -p tcp -m tcp -s ipaddress --dport 22 -j ACCEPT

到目前为止,我已经创建了这个剧本:

---

  - hosts: localhost
    remote_user: sysadmin
    become: true

    vars:
      host_name: localhost

    tasks:

  # Drop all incoming traffic
  # iptables -P INPUT DROP
     - iptables:
         chain: INPUT
         protocol: all
         jump: DROP
       become: yes


  # Drop all forwarded traffic
  # iptables -P FORWARD DROP
     - iptables:
         chain: FORWARD
         source: all
         jump: DROP
       become: yes

  # Allow all outgoing traffic
  #iptables -P OUTPUT ACCEPT
     - iptables:
         chain: OUTPUT
         source: all
         jump: ACCEPT
       become: yes

  # Allow all outgoing traffic
  # iptables -A INPUT -p tcp -m tcp -s xx.xx.xx.xx/32 --dport 22 -j ACCEPT
     - iptables:
         action: append
         chain: INPUT
         protocol: tcp
         source: ip_address
         destination_port: 22
         jump: ACCEPT
       become: yes  

【问题讨论】:

  • 我认为您需要更改任务顺序,因为首先您需要允许您想要的 ip 然后全部丢弃,否则它将丢弃所有内容并且不会尊重您的允许规则。
  • 我认为您需要更改任务顺序,因为首先您需要允许您想要的 ip 然后全部丢弃,否则它将丢弃所有内容并且不会尊重您的允许规则。
  • 即使我使用 -append 选项?

标签: module ansible iptables


【解决方案1】:

我解决了采取不同的步骤:

  1. iptables -A INPUT -s 2.228.104.210 -j ACCEPT
  2. iptables -A OUTPUT -d 2.228.104.210 -j ACCEPT
  3. iptables -P INPUT DROP
  4. iptables -P 输出丢弃

还有工作手册:

---

  - hosts: localhost
    remote_user: sysadmin
    become: true

    vars:
      host_name: localhost

    tasks:

     - iptables:
         chain: INPUT
         source: 192.168.1.1
         jump: ACCEPT
       become: yes


     - iptables:
         chain: OUTPUT
         destination: 192.168.1.1
         jump: ACCEPT
       become: yes


     - iptables:
         chain: INPUT
         policy: DROP
       become: yes


     - iptables:
         chain: OUTPUT
         policy: DROP
       become: yes

【讨论】:

    猜你喜欢
    • 2020-10-11
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    • 2022-06-28
    相关资源
    最近更新 更多