前提ansible安装完毕, /etc/ansible/hosts 配置完毕 ; ssh-keygen //产生私钥和公钥配置完毕!
Centos6 防火墙部署
[root@linux7 ~]# cd /etc/ansible/roles/
[root@linux7 roles]# mkdir iptables/{files,templates,tasks} -pv
[root@linux7 roles]# vim iptables/templates/iptables.j2
Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 10050 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
[root@linux7 roles]# vim iptables/tasks/main.yaml
- name: copy iptables
template: src=iptables.j2 dest=/etc/sysconfig/iptables backup=yes owner=root group=root mode=0600
handlers: -
name: Start iptables
service: name=iptables state=restarted enabled=true
/root目录
[root@linux7 ~]# vim iptables.yaml - hosts: linux-server
remote_user: root
roles:-
iptables
Hosts 写的是 /etc/ansible/hosts 写的组名
roles定义的是使用哪个目录(iptables或者zabbix-agent)
接下来我们
首先check
ansible-playbook iptables.yaml --check
ansible-playbook iptables.yaml --check -vvv (此命令是查看check过程的详细信息,如果有报错,请用此条查看详细报错)
check无报错,接下来执行
ansible-playbook iptables.yaml
去部署的机器查看下是否部署成功
发现部署已生效
不同的东西最好分开部署,这样报错也好找
-
Centos7 防火墙部署
copy一份centos7的防火墙文件
vim /etc/firewalld/zones/public.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
<short>Public</short>
<description>For use in public areas. You do not trust the other computers on networks to not harm your computer. O
nly selected incoming connections are accepted.</description>
<service name="dhcpv6-client"/>
<service name="ssh"/>
<port protocol="tcp" port="10050"/>
</zone>
vim public.xml.j2
放在templates 目录下
然后修改task目录下的main.yaml文件
centos7
- name: copy public.xml
template: src=public.xml.j2 dest=/etc/firewalld/zones/public.xml backup=yes owner=root group=root mode=0644
handlers: - name: Restart firewalld
service: state=restarted name=firewalld enabled=true
此文件中有两份模版,我是为了方便所以放到一起
然后找一台centos7的服务器
修改hosts文件
vim /etc/ansible/hosts
回到根目录
先检查文件
ansible-playbook iptables.yaml --check
如果没有报错,那就直接执行
登录192.168.30.38服务查看
cat /etc/firewalld/zones/public.xml
已经添加上