本文将结合例子介绍模块,条件测试和循环语句的使用
2 模板
模块和模板的区别,template模块是将模板文件里的变量以实际的值重新生成后文件后,复制到远程主机上
模块:template
基于模板(以后缀.j2结尾的文件)方式生成一个文件复制到远程主机,调用ansible的收集到的内建变量,变量的嵌入代码的标志是双花括号
*src= *dest= owner= group= mode=
模板:templates
文本文件,嵌套有脚本(使用模板编程语言Jinja2编写)
字面量: 字符串:使用单引号或双引号; 数字:整数,浮点数; 列表:[item1,item2,...] 元组:(item1,...) 字典:{key1:value1,key2:value2,...} 布尔型:true/false 算术运算: +,-,*,/,//,%,** 比较操作: ==,!=,>,>=,<,<= 逻辑运算: and,or,not
例一:
安装redis软件,配置redis的配置文件为模板
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 的值
例二:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
worker_processes {{ ansible_processor_vcpus }}; server_name {{ ansible_fqdn }};
3 条件测试
如被管控的主机系统不一样,执行的语句可能会不一样,需要做条件的判定
when语句:在task中使用,jinja2的语法格式
例子:
在centos6和centos7上安装Nginx,判断对应主机系统版本,实现不一样的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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,拷贝不同的文件到对应的主机
1
2
3
4
5
6
7
8
9
10
vim cpfile.yml
- hosts: Nginx
remote_user: root
tasks:
执行
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