ansible的firewalld和template模块
原创
©著作权归作者所有:来自51CTO博客作者omaidb的原创作品,请联系作者获取转载授权,否则将追究法律责任
使用firewalld模块配置防火墙策略
---
- name:
hosts: all
tasks:
- name: 安装防火墙
yum:
name: firewalld
- name: 开启防火墙服务
service:
name: firewalld
state: started
enabled: yes
- name: 设置防火墙规则
firewalld:
zone: public # 设置zone
port: 80/tcp
permanent: yes # 永久生效
immediate: yes # 现在就生效
state: enabled # 启用策略
[c8 root ~]# firewall-cmd --list-ports --permanent
80/tcp
[c8 root ~]# firewall-cmd --list-ports
80/tcp
[c8 root ~]#
template模块
测试facts变量
# 将变量写在文件中
welcome to {{ ansible_hostname }} on {{ ansible_ztuzex7lqa.ipv4.address }}
---
- name: template模块演示
hosts: all
tasks:
- name: 将index.html文件复制到节点,并将其中的变量替换为对应的值
template:
src: ~/play/template/index.html
dest: /var/www/html/index.html
[c8 root /var/www/html]# cat index.html
welcome to c8 on 10.147.17.65
[rhel7-gjb root /var/www/html]# cat index.html
welcome to rhel7-gjb on 10.147.17.40
Jinja2模板调用剧本中定义的变量
# 创建j2文件
touch source.j2
# 源文件中使用变量
{{ welcome }} {{ iname }} ...
---
- name: 演示剧本中定义变量使用template模板
hosts: all
vars:
welcome: 'hello'
iname: 'omaidb'
tasks:
- name: template模板自动使用vars变量
template:
src: ~/play/template/source.j2 # 源文件
dest: /tmp/ # 节点的目标目录
[c8 root /tmp]# cat source.j2
hello omaidb ...
[rhel7-gjb root /tmp]# cat source.j2
hello omaidb ...