Ansible系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.htmlhtml
ansible中的循环都是借助迭代来实现的。基本都是以"with_"开头。如下是常见的几种循环。shell
ansibel支持迭代功能。例如,有一大堆要输出的命令、一大堆要安装的软件包、一大堆要copy的文件等等。centos
例如,要安装一堆软件包。数组
---
- hosts: localhost
tasks:
- yum: name="{{item}}" state=installed
with_items:
- pkg1
- pkg2
- pkg3
它会一个一个迭代到特殊变量"{{item}}"处。oop
再例如,指定一堆文件列表,而后使用grep搜索出给定文件列表中包含"www.example.com"字符串的文件:测试
---
- hosts: localhost
tasks:
- shell: grep -Rl "www\.example\.com" "{{item}}"
with_items:
- file1
- file2
- file3
register: match_file
- debug: msg="{% for i in match_file.results %} {{i.stdout}} {% endfor %}"
注意,将with_items迭代后的结果注册为变量时,其注册结果也是列表式的,且其key为"results"。具体的结果比较长,可使用debug模块的var或msg参数观察match_file变量的结果。ui
在上面,是使用for循环进行引用的。若是不使用for循环,那么就须要使用数组格式。例如,引用match_file中的第一和第二个结果。this
- debug: var=match_file.results[0].stdout
- debug: var=match_file.results[1].stdout
显然,不如循环引用更好,由于不知道match_file中到底有几个匹配文件,也就不能肯定match_file中的列表数量。url
每一个列表项中可能都包含一个或多个字典,既然with_items迭代的是列表项,那么确定也能迭代列表中的各字典。spa
例如:
tasks:
- command: echo {{ item }}
with_items: [ 0, 2, 4, 6, 8, 10 ]
register: num
- debug: msg="{% for i in num.results %} {{i.stdout}} {% endfor %}"
再例如:
---
- hosts: localhost
tasks:
- shell: echo "name={{item.name}},age={{item.age}}"
with_items:
- {name: zhangsan,age: 32}
- {name: lisi,age: 33}
- {name: wangwu,age: 35}
register: who
- debug: msg="{% for i in who.results %} {{i.stdout}} {% endfor %}"
使用"with_dict"能够迭代字典项。迭代时,使用"item.key"表示字典的key,"item.value"表示字典的值。
例如:
---
- hosts: localhost tasks: - debug: msg="{{item.key}} & {{item.value}}" with_dict: { address: 1,netmask: 2,gateway: 3 }
另外一种状况,字典是已存储好的。例如ansible facts中的ansible_eth0.ipv4,其内容以下:
"ipv4": {
"address": "192.168.100.65",
"netmask": "255.255.255.0",
"gateway": "192.168.100.2"
}
这种状况下,with_dict处能够直接指定该字典的key。即:
---
- hosts: localhost tasks: - debug: msg="{{item.key}} & {{item.value}}" with_dict: ansible_eth0.ipv4
再例如,直接引用playbook中定义的vars。
---
- hosts: 192.168.100.65
gather_facts: False
vars:
user:
longshuai_key:
name: longshuai
gender: Male
xiaofang_key:
name: xiaofang
gender: Female
tasks:
- name: print hash loop var
debug: msg="{{ item.key }} & {{ item.value.name }} & {{ item.value.gender }}"
with_dict: "{{ user }}"
例如,拷贝一堆用通配符匹配出来的文件到各远程主机上。
---
- hosts: centos
tasks:
- copy: src="{{item}}" dest=/tmp/
with_fileglob:
- /tmp/*.sh
- /tmp/*.py
注意,通配符没法匹配"/",所以没法递归到子目录中,也就没法迭代子目录中的文件。
with_lines很好用,能够将命令行的输出结果按行迭代。
例如,find一堆文件出来,copy走。
---
- hosts: localhost
tasks:
- copy: src="{{item}}" dest=/tmp/yaml
with_lines:
- find /tmp -type f -name "*.yml"
嵌套迭代是指屡次迭代列表项。例如:
---
- hosts: localhost tasks: - debug: msg="{{item[0]}} & {{item[1]}}" with_nested: - [a,b] - [1,2,3]
结果将获得"a & 1"、"a & 2"、"a & 3"、"b & 1"、"b & 2"和"b & 3"共6个结果。
在ansible中,只有when能够实现条件判断。
tasks:
- name: config the yum repo for centos 6
yum_repository:
name: epel
description: epel
baseurl: http://mirrors.aliyun.com/epel/6/$basearch/
gpgcheck: no
when: ansible_distribution_major_version == "6"
注意两点:
此外,还支持各类逻辑组合。
tasks:
# 逻辑或
- command: /sbin/shutdown -h now
when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or
(ansible_distribution == "Debian" and ansible_distribution_major_version == "7")
# 逻辑与
- command: /sbin/shutdown -t now
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "6"
# 取反
- command: /sbin/shutdown -t now
when: not ansible_distribution == "CentOS"
还能够直接直接引用布尔值的变量。
---
- hosts: localhost
vars:
epic: False
tasks:
- debug: msg="This certainly is epic!"
when: not epic
此外,可使用jinja2的defined来测试变量是否已定义,使用undefined能够取反表示未定义。例如:
tasks:
- shell: echo "I've got '{{ foo }}' and am not afraid to use it!"
when: foo is defined
- fail: msg="Bailing out. this play requires 'bar'"
when: bar is undefined