Contents
ansibleのiptablesモジュールをちょっと触ったのでメモっておきます。
iptablesモジュールドキュメント
http://docs.ansible.com/ansible/latest/iptables_module.html
検証環境
| 項目 | バージョン | 
|---|---|
| RHEL | 7.4 | 
| ansible | 2.4.20 | 
iptablesモジュールを使ってみる
iptables設定を入れる
まずはテスト用でYAMLを作ってみました。各チェインDROPが無いのはひとまず動きがみたかったからです。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23  | 
						--- - hosts: localhost   become: yes   tasks:     - name: iptables INPUT ssh rule.       iptables:         comment: ssh input rule         chain: INPUT         source: 192.168.0.200         destination: 192.168.0.233         protocol: tcp         destination_port: 22         jump: ACCEPT     - name: iptables OUTPUT ssh rule.       iptables:         comment: ssh output rule         chain: OUTPUT         source: 192.168.0.233         destination: 192.168.0.200         protocol: tcp         source_port: 22         jump: ACCEPT  | 
					
これを実行してみます。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13  | 
						[root@localhost ~]# ansible-playbook -i localhost iptables_cnf.yml [root@localhost ~]# iptables -nL Chain INPUT (policy ACCEPT) target     prot opt source               destination ACCEPT     tcp  --  192.168.0.200        192.168.0.233        tcp dpt:22 /* ssh input rule */ Chain FORWARD (policy ACCEPT) target     prot opt source               destination Chain OUTPUT (policy ACCEPT) target     prot opt source               destination ACCEPT     tcp  --  192.168.0.233        192.168.0.200        tcp spt:22 /* ssh output rule */  | 
					
設定は問題なく入っています。
iptables設定を変更する
それでは、次にiptablesの設定を変更したYAMLを作ります。
変更部分は source のみです。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23  | 
						--- - hosts: localhost   become: yes   tasks:     - name: iptables INPUT ssh rule.       iptables:         comment: ssh input rule         chain: INPUT         source: 192.168.0.199         destination: 192.168.0.233         protocol: tcp         destination_port: 22         jump: ACCEPT     - name: iptables OUTPUT ssh rule.       iptables:         comment: ssh output rule         chain: OUTPUT         source: 192.168.0.233         destination: 192.168.0.199         protocol: tcp         source_port: 22         jump: ACCEPT  | 
					
これを実行してみます。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  | 
						[root@localhost ~]# ansible-playbook -i localhost iptables_cnf.yml [root@localhost ~]# iptables -nL Chain INPUT (policy ACCEPT) target     prot opt source               destination ACCEPT     tcp  --  192.168.0.200        192.168.0.233        tcp dpt:22 /* ssh input rule */ ACCEPT     tcp  --  192.168.0.199        192.168.0.233        tcp dpt:22 /* ssh input rule */ Chain FORWARD (policy ACCEPT) target     prot opt source               destination Chain OUTPUT (policy ACCEPT) target     prot opt source               destination ACCEPT     tcp  --  192.168.0.233        192.168.0.200        tcp spt:22 /* ssh output rule */ ACCEPT     tcp  --  192.168.0.233        192.168.0.199        tcp spt:22 /* ssh output rule */  | 
					
既存設定は変更されず、新しい設定が追加されました。
実は期待していたことは既存設定の変更をしてくれるかな?と思っていたのですがそうでは無いようです。
iptables設定の入れ直し
設定を変更する場合は、一旦flushして再度入れ直しになる感じでしょうか。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26  | 
						--- - hosts: localhost   become: yes   tasks:     - name: iptables flush       iptables:         flush: true     - name: iptables INPUT ssh rule.       iptables:         comment: ssh input rule         chain: INPUT         source: 192.168.0.199         destination: 192.168.0.233         protocol: tcp         destination_port: 22         jump: ACCEPT     - name: iptables OUTPUT ssh rule.       iptables:         comment: ssh output rule         chain: OUTPUT         source: 192.168.0.233         destination: 192.168.0.199         protocol: tcp         source_port: 22         jump: ACCEPT  | 
					
flushして再度同じ設定を入れ直せば期待していた動きになりました。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13  | 
						[root@localhost ~]# ansible-playbook -i localhost iptables_cnf.yml [root@localhost ~]# iptables -nL Chain INPUT (policy ACCEPT) target     prot opt source               destination ACCEPT     tcp  --  192.168.0.199        192.168.0.233        tcp dpt:22 /* ssh input rule */ Chain FORWARD (policy ACCEPT) target     prot opt source               destination Chain OUTPUT (policy ACCEPT) target     prot opt source               destination ACCEPT     tcp  --  192.168.0.233        192.168.0.199        tcp spt:22 /* ssh output rule */  | 
					
ただし、瞬断する感じにはなってしまそうです。
flushしてACCEPT状態でルール入れて最後にチェインをDROPポリシーにすればいいだけのような気がする。(入れる時は全ACCEPTになってしまうけど)