使用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模块

ansible的firewalld和template模块_源文件

测试facts变量

  • 新建一个index.html文件
# 将变量写在文件中
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 ...