模板
基于模板的方式生成一个文件复制到远程主机。基于ansible的templates模块实现。
将模板文件中的变量替换成变量的值后复制到远程主机。
模板:templates
文本文件,嵌套有脚本(使用模板编程语言编写)
Jinja2:
字面量:
字符串:使用单引号或双引号;
数字:整数,浮点数;
列表:[item1, item2, ...]
元组:(item1, item2, ...)
字典:{key1:value1, key2:value2, ...}
布尔型:true/false
算术运算:
+, -, *, /, //, %, **
比较操作:
==, !=, >, >=, <, <=
逻辑运算:
and, or, not
示例:
- hosts: websrvs
remote_user: root
tasks:
- name: install nginx
yum: name=nginx state=present
- name: install conf file
template: src=files/nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx
tags: instconf
- name: start nginx service
service: name=nginx state=started
handlers:
- name: restart nginx
service: name=nginx state=restarted
模板配置文件 :nginx.conf.j2
worker_processes {{ ansible_processor_vcpus }};
listen {{ http_port }};
例子:现在我们需要把每一台服务器的一般的内存分配给redis,但是每台服务器的
内存大小都不相同。
首先,我们在源配置文件里面修改
[ root@node1 ~ ]# cat redis.conf.j2
# maxmemory <bytes>
maxmemory {{ ansible_memtotal_mb /2 }}mb
然后编辑一个yaml文件
[ root@node1 ~ ]# cat redis.conf.yaml
- hosts: dbserver
remote_user: root
tasks:
- name: install redis conf
template: src=/root/redis.conf.j2 dest=/etc/redis.conf owner=redis group=root mode=644
[ root@node1 ~ ]# ansible-playbook redis.conf.yaml
======================================================
条件测试:
when语句:在task中使用,jinja2的语法格式
tasks:
- name: install conf file to centos7
template: src=files/nginx.conf.c7.j2
when: ansible_distribution_major_version == "7"
- name: install conf file to centos6
template: src=files/nginx.conf.c6.j2
when: ansible_distribution_major_version == "6"
循环:迭代,需要重复执行的任务;
对迭代项的引用,固定变量名为”item“
而后,要在task中使用with_items给定要迭代的元素列表;
列表方法:
字符串
字典
例子:字符串迭代的方法
[ root@node1 ~ ]# cat tomcat.yaml
- hosts: webserver
remote_user: root
vars:
- jdk_version: 1.8.0
tasks:
- name: install {{ item }} package
yum: name={{ item }} state=latest
with_items:
- nginx
- java-{{ jdk_version }}-openjdk
- tomcat
- tomcat-webapps
- tomcat-docs-webapp
- tomcat-admin-webapps
例子:字典
[ root@node1 ~ ]# cat tomcat.yaml
- hosts: webserver
remote_user: root
tasks:
- name: install {{ item }} package
yum: name={{ item.name }}-{{ item.version }} state=latest
with_items:
- { name: 'nginx', version: '1.10.2' }
- { name: 'tomcat', version: '7.0.69' }
例子:
[ root@node1 ~ ]# cat tom.yaml
- hosts: webserver
remote_user: root
vars:
- jdk_version: 1.8.0
tasks:
- name: install {{ item }} package
yum: name={{ item }} state=latest
with_items:
- nginx
- java-{{ jdk_version }}-openjdk
- tomcat
- tomcat-webapps
- tomcat-docs-webapp
- tomcat-admin-webapps
- name: install conf file
copy: src={{ item.file }} dest={{ item.conf }}
with_items:
- { file: '/root/tomcat-users.xml', conf: '/etc/tomcat/tomcat-users.xml' }
- { file: '/root/server.xml', conf: '/etc/tomcat/server.xml' }
案例:
- name: install some packages
yum: name={{ item }} state=present
with_items:
- nginx
- memcached
- php-fpm
- name: add some groups
group: name={{ item }} state=present
with_items:
- group11
- group12
- group13
- name: add some users
user: name={{ item.name }} group={{ item.group }} state=present
with_items:
- { name: 'user11', group: 'group11' }
- { name: 'user12', group: 'group12' }
- { name: 'user13', group: 'group13' }