- 变量
- 算术运算:+ - / % // *
- 比较操作:== != > <
- 逻辑运算:and or not
- 循环
- 条件判断
- 循环迭代
- 基于字符串列表
tasks:
- name: create rsyncd file
copy: src={{ item }} dest=/tmp/{{ item }}
with_items:
- a
- b
- c
- d
with_itmes 嵌套子变量
#基于字典列表
- hosts: eagleslab
remote_user: root
tasks:
- name: add several users
user: name={{ item.name }} state=present groups={{ item.groups }}
with_items:
- { name: ‘testuser1’ , groups: ‘wheel’}
- { name: ‘testuser2’ , groups: ‘root’ }
with_items: [a,b,c,d]
变量
- 字符串:使用单引号或者双引号
- 数字:整数、浮点数、。。
- 列表:[item1,item2,item3…]
- 元组:(item1,item2,item3,…)
- 字典:{k1:v1,k2:v2,k3:v3…}
- 布尔值:true/false
算术运算:+ - / % // *
比较操作:== != > <
逻辑运算:and or not
循环
{% %}
……
{% endfor %}
示例:
[root@node1 templates]# cat nginxconf.j2.bak
{% for vhost in nginx_vhosts %}
server {
listen {{ vhost }}
listen {{ vhost.listen }}
}
{% endfor %}
worker_processes {{ ansible_processor_vcpus }};
[root@node1 tmp]# cat nginxtemp.yam
- hosts: mageduweb
remote_user: root
vars:
nginx_vhosts: // nginx_vhosts:[web1,web2,web3]
- web1
listen: 80
- web2
listen: 81
- web3
listen: 82
# nginx_vhosts:
# - listen: 8080
tasks:
- name: template config to remote hosts
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
list1 = [1,2,3,4,5]
for i in list1;
echo $i条件判断
- name: restart Nginx
service: name=nginx state=restarted
when: ansible_distribution_major_version == “6”循环迭代
基于字符串列表
tasks:
- name: create rsyncd file
copy: src={{ item }} dest=/tmp/{{ item }}
with_items:
- a
- b
- c
- d
with_itmes 嵌套子变量
#基于字典列表
- hosts: eagleslab
remote_user: root
tasks:
- name: add several users
user: name={{ item.name }} state=present groups={{ item.groups }}
with_items:
- { name: ‘testuser1’ , groups: ‘wheel’}
- { name: ‘testuser2’ , groups: ‘root’ }
with_items: [a,b,c,d]