ansible中template简单使用

一、模板(template)简介

  • 文件文件,嵌套有脚本(使用模板编程语言编写);
  • jinja2语言,使用字面量,有以下形式:
    • 字符串:使用单引号或双引号;
    • 数字:整数,浮点数;
    • 列表:[ item1,item2,……]
    • 元组:(item1,item2,……)
    • 字典:
    • 布尔型:true/false
  • 算术运算:+,-,*,/,//,%,**
  • 比较操作:==,!=,>,>=,<,<=
  • 逻辑运算:and,or,not
  • 流表达式:for,if,when

二、使用template部署nginx

$ ls //建议,安装nginx的yaml和templates目录在同一目录下
install_nginx.yaml templates
$ cat install_nginx.yaml
---
- hosts: webservers
remote_user: root
vars: #创建变量信息
- http_port: 8888
tasks:
- name: install package
yum: name=nginx
- name: template copy
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf #由于模板文件在tmplate下,所以src后的路径就可以只写配置文件名称
notify: restart service #定义notify便于修改文件重启服务
- name: start service
service: name=nginx state=started enabled=yes
handlers: #定义重启服务策略
- name: restart service
service: name=nginx state=restarted
$ ls templates/ #注意模板的文件名称有一定的要求
nginx.conf.j2
$ cat templates/nginx.conf.j2 #该文件就是nginx的配置文件复制而成的
………… #省略部分内容
worker_processes {{ ansible_processor_vcpus**2 }}; #使用变量是cpu核心数的2次方
listen {{ http_port }} default_server;
listen [::]:{{ http_port }} default_server;
#使用playbook中定义的变量信息
$ ansible-playbook install_nginx.yaml
#运行playbook文件
$ ansible webservers -m shell -a 'rpm -q nginx'
#nginx确实已经安装成功
$ ansible webservers -m shell -a 'ss -lnt | grep 8888'
#端口已经正常开启
$ ansible webservers -m shell -a 'ps aux | grep nginx'
#确认工作进程数是预期设定的值

至此template批量部署nginx已经实现!

三、playbook中when简单使用

when:可以简单理解为一个条件判断,类似于shell脚本中的if语句!

因为ansible管理的主机可能不是一个系统版本的,那么就需要区别部署了!

$ ansible all -m setup -a 'filter=*distribution*'
#查看ansible默认支持的变量
$ cat install_nginx.yaml
---
- hosts: all #针对所有主机
remote_user: root
vars:
- http_port: 8888
tasks:
- name: install package
yum: name=nginx
- name: template copy for centos7
template: src=nginx.conf7.j2 dest=/etc/nginx/nginx.conf
when: ansible_distribution_major_version == "7" #当检测到系统版本为7才执行本模块的操作
notify: restart service
- name: template copy for centos6
template: src=nginx.conf6.j2 dest=/etc/nginx/nginx.conf
when: ansible_distribution_major_version == "6"
notify: restart service
- name: start service
service: name=nginx state=started enabled=yes
handlers:
- name: restart service
service: name=nginx state=restarted
$ ls templates/ #注意一个是nginx6的配置文件,一个nginx7的配置文件
nginx.conf6.j2 nginx.conf7.j2
$ ansible-playbook install_nginx.yaml
#执行playbook文件
$ ansible all -m shell -a 'ss -lntp | grep nginx'
#确认centos 6系统的nginx已经启动

四、playbook中with_items简单使用

4.1 迭代:with_items

迭代:with_items:当有需要重复性执行任务是,可以使用迭代机制!

  • 带迭代项的引用,固定变量为“item”;
  • 在task中使用with_items定义需要迭代的元素列表;
  • 列表格式:
    • 字符串;
    • 字典;
$ cat test.yaml
---
- hosts: all
remote_user: root
tasks:
- name: touch some file
file: name=/data/{{ item }} state=touch #文件名定义为列表元素
when: ansible_distribution_major_version == "7"
with_items: #定义列表元素
- file1
- file2
- file3
$ ansible-playbook test.yaml
$ ansible all -m shell -a 'ls -l /data'
#当满足条件的主机都创建了文件

4.2 迭代嵌套子变量

$ cat test1.yaml
---
- hosts: all
remote_user: root
tasks:
- name: create some group
group: name={{ item }}
with_items:
- g1
- g2
- g3
- name: create some users
user: name={{ item.name }} group={{ item.group }}
with_items:
- { name: 'user1', group: 'g1'}
- { name: 'user2', group: 'g2'}
- { name: 'user3', group: 'g3'}
$ ansible-playbook test1.yaml
$ ansible all -m shell -a 'getent group'
$ ansible all -m shell -a 'getent passwd'
$ ansible all -m shell -a 'id user1'
#进行验证

五、template循环示例

5.1 第一种写法

$ cat test2.yaml
---
- hosts: webservers
remote_user: root
vars:
ports:
- 81
- 82
- 83
tasks:
- name: copy conf
template: src=for2.conf.j2 dest=/data/for2.conf
$ cat templates/for2.conf.j2
{% for port in ports %}
server {
listen {{ port }}
}
{% endfor %}
$ ansible-playbook test2.yaml
$ ansible webservers -m shell -a 'cat /data/for2.conf'
#进行验证

5.2 第二种写法

$ cat test3.yaml
---
- hosts: webservers
remote_user: root
vars:
ports:
- listen_port: 81
- listen_port: 82
- listen_port: 83
tasks:
- name: copy conf
template: src=for3.conf.j2 dest=/data/for3.conf
$ cat templates/for3.conf.j2
{% for port in ports %}
server {
listen {{ port.listen_port }}
}
{% endfor %}
$ ansible-playbook test3.yaml
$ ansible webservers -m shell -a 'cat /data/for3.conf'
#进行验证

5.3 第三种写法

$ cat test4.yaml
---
- hosts: webservers
remote_user: root
vars:
ports:
- web1:
port: 81
name: web1.lzj.com
rootdir: /data/web1
- web2:
port: 82
name: web2.lzj.com
rootdir: /data/web2
- web1:
port: 83
name: web3.lzj.com
rootdir: /data/web3
tasks:
- name: copy conf
template: src=for4.conf.j2 dest=/data/for4.conf
$ cat templates/for4.conf.j2
{% for p in ports %}
server {
listen {{ p.port }}
servername {{ p.name }}
documentroot {{ p.rootdir }}
}
{% endfor %}
$ ansible-playbook test4.yaml
$ ansible webservers -m shell -a 'cat /data/for4.conf'
#进行验证

六、playbook中if简单使用

$ cat test5.yaml
---
- hosts: webservers
remote_user: root
vars:
ports:
- web1:
port: 81
rootdir: /data/web1
- web2:
port: 82
name: web2.lzj.com
rootdir: /data/web2
- web1:
port: 83
rootdir: /data/web3
tasks:
- name: copy conf
template: src=for5.conf.j2 dest=/data/for5.conf
$ cat templates/for5.conf.j2
{% for p in ports %}
server {
listen {{ p.port }}
{% if p.name is defined%} #如果名称被定义了才给名字赋值
servername {{ p.name }}
{% endif %}
documentroot {{ p.rootdir }}
}
{% endfor %}
$ ansible-playbook test5.yaml
$ ansible webservers -m shell -a 'cat /data/for5.conf'
#进行验证
posted @   吕振江  阅读(2465)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
浏览器标题切换
浏览器标题切换end
点击右上角即可分享
微信分享提示

目录导航