Ansible之Playbook的Template模板和tags标签

文章目录

一、Template模块

  • Jinja是基于Python的模块引擎。Template类是Jinja的一个重要组件,可以看做是一个编译过的模板文件,用来产生目标文本,传递Python的变量给模板去替换模板中的标记。
  • 是一种创建配置文件的工具,在配置文件中,有一些数据会有动态的改变(如:ip、主机名、端口、用户、页面路径等)需要使用变量来表示
  • template模块就是将变量和动态文件结合起来,最终生成一个配置文件(需要动态改变)
  • 文件配置使用jinjia2模板语言编写,一定是以.j2结尾

1、准备template模板文件

  • 先准备一个以.j2为后缀的template模板文件,设置引用的变量
  • 模板文件使用test1曾用的httpd.conf配置文件
bash 复制代码
cp nginx.conf nginx.conf.j2
#准备以.j2结尾的配置文件

#编辑配置文件
vim nginx.conf.j2
listen      {{nginx_port}};
#39行,修改监听端口自定义变量
server_name {{server_name}};
#41行,修改主机名自定义变量
root        {{root_dir}};
#42行,修改页面路径自定义变量

2、修改主机清单文件

  • 修改主机清单文件,使用主机变量定义一个变量名相同,而值不同的变量
bash 复制代码
#修改主机清单配置文件
vim /etc/ansible/hosts 
 [webservers]
192.168.10.12 nginx_port=192.168.10.12:80 server_name=www.liu.com:80 root_dir=/etc/nginx/htdocs
#指定主机访问端口,域名和页面路径

 [dbservers]
192.168.10.13 nginx_port=192.168.10.13:80 server_name=www.yan.com:80 root_dir=/etc/nginx/htdocs
#指定主机访问端口,域名和页面路径
bash 复制代码
#远程主机添加主机映射
vim /etc/hosts
192.168.10.12 www.liu.com
192.168.10.13 www.yan.com

3、编写playbook

bash 复制代码
#编辑yaml剧本
vim deam05.yaml
---
- name: install nginx
  hosts: all
  remote_user: root
  vars:
  #定义调用变量
    - pck: nginx
    - ser: nginx
  tasks:
    - name: install nginx pck
      yum: name={{pck}} state=latest
      #调用变量
    - name: install configure file
      template: src=/opt/nginx.conf.j2 dest=/etc/nginx/nginx.conf
      #使用template模板
      notify:
        - restart nginx
    - name: create root dir
      file: path=/etc/nginx/htdocs state=directory
    - name: start nginx server
      service: name={{ser}} enabled=true state=started
  handlers:
    - name: restart nginx
      service: name={{ser}} state=restarted

4、执行playbook

bash 复制代码
ansible-playbook deam05.yaml
#执行剧本

5、准备测试网页

bash 复制代码
ansible 192.168.10.12 -m shell -a "echo 'this is template liu' > /etc/nginx/htdocs/index.html"
#准备访问页面

ansible 192.168.10.13 -m shell -a "echo 'this is template yan' > /etc/nginx/htdocs/index.html"
#准备访问页面

6、访问测试

bash 复制代码
curl 192.168.10.12
curl 192.168.10.13
#访问测试

curl www.liu.com
curl www.yan.com
#使用域名访问测试时,需要先添加主机映射

二、tags模块

  • 可以在一个playbook中为某个或某些任务定义"标签",在执行此playbook时通过ansible-playbook命令使用--tags选项能实现仅运行指定的tasks。
  • playbook还提供了一个特殊的tags为always。作用就是当使用always当tags的task时,无论执行哪一个tags时,定义有always的tags都会执行。

1、编写脚本

bash 复制代码
#使用tags模块编辑yaml脚本
vim deam06.yaml
---
- name: tags
  hosts: webservers
  remote_user: root
  tasks:
    - name: mkdir directory
      file: path=/opt/liuyanfen/ state=directory
      tags:
        - always
    - name: touch file
      file: path=/opt/liuyanfen/test state=touch
      tags:
        - xx01
    - name: copy hosts file
      copy: src=/etc/hosts dest=/opt/hosts
      tags:
        - xx02

2、执行tags="xx01"

bash 复制代码
ansible-playbook deam06.yaml --tags="xx01"
#执行标签xx01
  • 验证
bash 复制代码
ansible webservers -a "ls -l /opt/liuyanfen"
#查看创建的目录文件

3、执行tags="xx02"

  • 先删除文件夹
bash 复制代码
ansible webservers -m file -a "path=/opt/liuyanfen/ state=absent"
#先移除创建的文件夹

ansible webservers -a "ls -l /opt/liuyanfen"
#查看文件,显示不存在
  • 执行tags=xx02
bash 复制代码
ansible-playbook deam06.yaml --tags="xx02"
#执行tags=xx02
  • 验证
bash 复制代码
ansible webservers -a "ls -l /opt/liuyanfen"
#查看创建的目录,tags=always

ansible webservers -a "ls -l /opt/hosts"
#查看复制的文件,tags=xx02
相关推荐
一切皆文件linux14 小时前
网站集群批量管理-密钥认证与Ansible模块
ansible
让美好继续发生2 天前
ansible学习
学习·ansible
有谁看见我的剑了?2 天前
ansible学习之 Facts
ansible
peanutfish2 天前
Chapter 4 RH294 RHEL Automation with Ansible
linux·ansible·yaml
henan程序媛3 天前
jenkins项目发布基础
运维·gitlab·ansible·jenkins
pyliumy4 天前
ansible 配置
大数据·ansible
运维小白。。4 天前
自动化运维工具 Ansible
运维·自动化·ansible
weixin_438197385 天前
ansible之playbook\shell\script模块远程自动安装nginx
linux·服务器·ansible
peanutfish5 天前
Chapter 5 RH294 RHEL Automation with Ansible
linux·ansible·yaml
人类群星闪耀时5 天前
自动化运维的利器:Ansible、Puppet和Chef详解
运维·自动化·ansible