iptables模块
基本函数方法:
salt.modules.iptables.append(table=u‘filter‘, chain=None, rule=None, family=u‘ipv4‘)
salt.modules.iptables.insert(table=u‘filter‘, chain=None, position=None, rule=None, family=u‘ipv4‘)
salt.modules.iptables.delete(table, chain=None, position=None, rule=None, family=u‘ipv4‘)
salt.modules.iptables.save(filename=None, family=u‘ipv4‘)
salt.modules.iptables.get_rules(family=u‘ipv4‘)
功能说明:
实现对被控主机的iptables基本配置。用于对iptables进行新增,插入,删除,保存配置,查看等功能。
更多请查看官方文档:
https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.iptables.html
常用参数说明:
table:iptables表,如,filter表
chain:iptables链,如,INPUT链
position:iptables规则编号
rule:针对某个表某个链的规则配置
family:一般为ipv4和ipv6
1、命令方式配置方法:
追加80端口的规则
salt ‘*‘ iptables.append filter INPUT rule=‘-p tcp --dport 80 -j ACCEPT‘
插入3306端口的规则
salt ‘*‘ iptables.insert filter INPUT position=3 rule=‘-p tcp --dport 3306 -j ACCEPT‘
删除指定链编号为3(position=3)的规则
salt ‘*‘ iptables.delete filter INPUT position=3
删除指定规则
salt ‘*‘ iptables.delete filter INPUT rule=‘-p tcp --dport 80 -j ACCEPT‘
保存至配置文件iptables
salt ‘*‘ iptables.save /etc/sysconfig/iptables
2、API调用配置方法:
API原理是通过调用master client模块,实例化一个LocalClient对象,再调用cmd()方法来实现的。
import salt.client
client = salt.client.LocalClient()
ret = client.cmd(‘‘,‘test.ping)
#cmd内格式:‘<操作目标>‘,‘<模块>‘,‘[参数]‘。例:‘‘,‘cmd.run‘,[‘df -h‘]
print ret
iptables模块的API调用方法:
#!/usr/bin/env python
import salt.client
client = salt.client.LocalClient()
追加指定规则
ret = client.cmd(‘*‘,‘iptables.append‘,[‘filter‘,‘INPUT‘,‘rule=\‘-p tcp --dport 80 -j ACCEPT\‘‘])
print ret
插入编号为1的位置指定规则
ret = client.cmd(‘*‘,‘iptables.insert‘,[‘filter‘,‘INPUT‘,‘position=1‘,‘rule=\‘-p tcp --dport 80 -j ACCEPT\‘‘])
删除指定链编号为3的规则
ret =client.cmd(‘*‘, ‘iptables.delete‘,[‘filter‘,‘INPUT‘,‘position=3‘])
删除指定规则
ret = client.cmd(‘*‘,‘iptables.delete‘,[‘filter‘,‘INPUT‘,‘rule=\‘-p tcp --dport 1202 -j ACCEPT\‘‘])
保存到配置文件
ret = client.cmd(‘*‘,‘iptables.save‘,[‘/etc/sysconfig/iptables‘])
结果查看([主机],[表],[链],[规则])
print ret[‘lvs_master‘][‘filter‘][‘INPUT‘][‘rules‘]
3、状态管理配置方法:
常用iptables规则的追加、删除、插入配置方法:
salt.states.iptables.append(name, table=u‘filter‘, family=u‘ipv4‘, kwargs)
salt.states.iptables.delete(name, table=u‘filter‘, family=u‘ipv4‘, kwargs)
salt.states.iptables.insert(name, table=u‘filter‘, family=u‘ipv4‘, kwargs)
更多请查看官方文档:
https://docs.saltstack.com/en/latest/ref/states/all/salt.states.iptables.html**
追加规则
cat /srv/salt/iptables/append.sls
httpd: #ID 注意唯一性
iptables.append:
- table: filter
- chain: INPUT
- jump: ACCEPT
- match: state
- connstate: NEW
- dport: 80
- protocol: tcp
- save: True
插入规则
cat /srv/salt/iptables/insert.sls
连续型端口配置
httpd:
iptables.insert:
- position: 1
- table: filter
- chain: INPUT
- jump: ACCEPT
- match: state
- connstate: NEW
- dport: 80
- protocol: tcp
- save: True
## 离散型端口配置
firewall multiport:
iptables.insert:
- position: 1
- table: filter
- family: ipv4
- chain: INPUT
- jump: ACCEPT
- source: 192.168.81.190
- match: multiport
- dports: 9999,9994
- proto: tcp
- save: True
删除规则
cat /srv/salt/iptables/delete.sls
httpd:
iptables.delete:
- table: filter
- chain: INPUT
- jump: ACCEPT
- match: state
- connstate: NEW
- dport: 80
- protocol: tcp
- save: True
应用配置
cd /srv/salt
salt ‘*‘ state.sls iptables/insert
原文地址:http://blog.51cto.com/bjdongpengfei/2161069