本文将结合例子介绍模块,条件测试和循环语句的使用
2 模板
模块和模板的区别,template模块是将模板文件里的变量以实际的值重新生成后文件后,复制到远程主机上
模块:template
基于模板(以后缀.j2结尾的文件)方式生成一个文件复制到远程主机,调用ansible的收集到的内建变量,变量的嵌入代码的标志是双花括号
*src=
*dest=
owner=
group=
mode=
模板:templates
文本文件,嵌套有脚本(使用模板编程语言Jinja2编写)
字面量:
字符串:使用单引号或双引号;
数字:整数,浮点数;
列表:[item1, item2, ...]
元组:(item1, item2, ...)
字典:{key1:value1, key2:value2, ...}
布尔型:true/false
算术运算:
+, -, *, /, //, %, **
比较操作:
==, !=, >, >=, <, <=
逻辑运算:
and, or, not
例一:
安装redis软件,配置redis的配置文件为模板
修改配置模板文件,以.j2结尾
vim /root/redis.conf.j2
maxmemory {{ ansible_memtotal_mb / 2 }}mb
注意,template不能再命令行执行,即不能在命令里用-m指定模块为template执行,需要定义在脚本里,用playbook执行脚本
vim /root/ansible/installvar.yml
- hosts: websrvs
remote_user: root
vars:- pkgname: redis
tasks: - name: install package
yum: name={{ pkgname }} state=latest - name: install redis conf
template: src=/root/redis.conf.j2 dest=/etc/redis.conf owner=redis group=root mode=644
- pkgname: redis
执行脚本如下
ansible-playbook installvar.yml
则websrvs组里的主机安装redis后配置文件配置的maxmemory为ansible_memtotal_mb / 2 的值
例二:
安装nginx,把nginx配置文件配置为模板
vim /root/ansible/instnginx.yml
- hosts: websrvs
remote_user: root
tasks: - name: install nginx
yum: name=nginx state=present - name: install conf file
template: src=/root/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 }};
server_name {{ ansible_fqdn }};
3 条件测试
如被管控的主机系统不一样,执行的语句可能会不一样,需要做条件的判定
when语句:在task中使用,jinja2的语法格式
例子:
在centos6和centos7上安装nginx,判断对应主机系统版本,实现不一样的配置
vim instnginx.yml
- hosts: nginx
remote_user: root
tasks:- name: install nginx package
yum: name=nginx state=latest
tags: instpkg - name: install conf file6
template: src=/root/nginx.conf.c6.j2 dest=/etc/nginx/nginx.conf
when: ansible_distribution_major_version == "6"
notify: restart nginx - name: install conf file7
template: src=/root/nginx.conf.c7.j2 dest=/etc/nginx/nginx.conf
when: ansible_distribution_major_version == "7"
notify: restart nginx - name: start nginx service
service: name=nginx enabled=true state=started
handlers: - name: restart nginx
service: name=nginx state=restarted
- name: install nginx package
运行
1
ansible-playbook instnginx.yml
例子:
根据主机的ip,拷贝不同的文件到对应的主机
vim cpfile.yml
- hosts: nginx
remote_user: root
tasks:- name: copy file6
copy: src=/root/nginx.conf.c6.j2 dest=/tmp/
when: ansible_default_ipv4['address'] == '172.18.50.63' - name: copy file7
copy: src=/root/nginx.conf.c7.j2 dest=/tmp/
when: ansible_default_ipv4['address'] == '172.18.50.75'
- name: copy file6
执行
1
ansible-playbook cpfile.yml
4 循环
迭代,需要重复执行的任务;
1 对迭代项的引用,固定变量名为”item“
2 然后在task中使用with_items给定要迭代的元素列表;
列表方法:
字符串
字典:元素可以是字典,复制多个文件时,可以直接使用copy模块
例子:
通过迭代的方法安装服务包和创建用户
vim iteration.yml
- hosts: dbsrvs
remote_user: root
vars:- jdk_version: 1.8.0
tasks: - name: install {{ item }} package
yum: name={{ item.name }}-{{ item.version }} state=latest
with_items: - { name: 'nginx',version: '1.10.2' }
- { name: 'tree',version: '1.5.3' }
- name: copy nginx conf
copy: src={{ item.file }} dest={{ item.conf }}
with_items: - { file: '/root/nginx.conf.os6',conf: '/etc/nginx.conf' }
- name: install {{ item }} package
yum: name={{ item }} state=latest
with_items: - java-{{ jdk_version }}-openjdk
- tomcat
- tomcat-webapps
- tomcat-docs-webapp
- tomcat-admin-webapps
- name: add some groups
group: name={{ item }} state=present
with_items: - ansigrp1
- ansigrp2
- name: add some users
user: name={{ item.name }} group={{ item.group }} state=present
with_items: - { name: 'ansiuser1', group: 'ansigrp1' }
- { name: 'ansiuser2', group: 'ansigrp2' }
- jdk_version: 1.8.0