2017-09-26 68 views
0

我开始使用Ansible开发进行了系统iptables的一些动作的剧本。 我有一台服务器,我想阻止除一个或多个IP以外的所有IP。Ansible剧本阻止所有IP排除一个或多个IP

我真的不知道该怎么写使用ansible模块的iptables规则。我需要:

  1. 丢弃所有传入流量 (iptables的-P INPUT DROP)
  2. 丢弃所有传入流量(iptables的-P INPUT DROP)
  3. 丢弃所有转发的流量(iptables的-P正向压降)
  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 
+0

我想你需要更改任务顺序,因为首先你需要让你想要的IPS然后放下一切,否则会放下所有的事情,也不会尊重你的允许规则。 –

+0

我认为你需要更改的任务顺序,因为首先你需要让你想要的IPS然后放下一切,否则会放下所有的事情,也不会尊重你的允许规则。 –

+0

即使我使用-append选项? – Dandelion

回答

0

我解决了采取不同的措施:

  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 OUTPUT DROP

和工作的剧本:

--- 

    - 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