循环语句
列表迭代
展开列表
代码语言:txt复制- hosts: all
remote_user: root
tasks:
- name: install {{ item }}
yum: name={{ item }} state=latest
#安装下列所有包
with_items:
- mariadb-server
- mariadb
- php-fpm
- httpd
改写方法1
代码语言:txt复制with_items: [mariadb-server, mariadb, php-fpm, httpd]
改写方法2
代码语言:txt复制with_items:
- [mariadb-server, mariadb, php-fpm, httpd]
改写方法3
代码语言:txt复制vars:
users:
- lala
- yaya
- haha
with_items: "{{ users }}"
不展开列表
代码语言:txt复制[student@workstation ansible]$ cat loop.yml
- hosts: all
gather_facts: no
tasks:
- debug:
msg: "{{ item[0] }}"
with_list:
- ["haha","lala","xixi"]
- ["miemie","wawa","yoyo" ]
元素叠加
代码语言:txt复制[student@workstation ansible]$ cat loop.yml
- hosts: servera
gather_facts: no
tasks:
- debug:
msg: "{{ item[0] }}-{{ item[1] }}"
with_together
- ["蓝","橙","红"]
- ["颜色","精灵" ]
列表上下元素进行叠加
代码语言:txt复制ok: [servera] => (item=[u'u84dd', u'u989cu8272']) => {
"item": [
"蓝",
"颜色"
],
"msg": "蓝-颜色"
}
ok: [servera] => (item=[u'u6a59', u'u7cbeu7075']) => {
"item": [
"橙",
"精灵"
],
"msg": "橙-精灵"
}
ok: [servera] => (item=[u'u7ea2', None]) => {
"item": [
"红",
null
],
"msg": "红-"
}
笛卡尔积
代码语言:txt复制[student@workstation ansible]$ cat loop.yml
- hosts: servera
gather_facts: no
tasks:
- debug:
msg: "{{ item[0] }}-{{ item[1] }}"
with_nested:
- ["蓝","橙"]
- ["颜色","精灵" ]
执行结果
代码语言:txt复制ok: [servera] => (item=[u'u84dd', u'u989cu8272']) => {
"item": [
"蓝",
"颜色"
],
"msg": "蓝-颜色"
}
ok: [servera] => (item=[u'u6a59', u'u7cbeu7075']) => {
"item": [
"橙",
"精灵"
],
"msg": "橙-精灵"
}
ok: [servera] => (item=[u'u6a59', u'u989cu8272']) => {
"item": [
"橙",
"颜色"
],
"msg": "橙-颜色"
}
ok: [servera] => (item=[u'u84dd', u'u7cbeu7075']) => {
"item": [
"蓝",
"精灵"
],
"msg": "蓝-精灵"
}
索引
带索引的循环,item的第0个就是元素的索引,起始从0开始
代码语言:txt复制[student@workstation ansible]$ cat loop.yml
- hosts: servera
gather_facts: no
tasks:
- debug:
msg: "{{ item[0] }}-{{ item[1] }}"
with_indexed_items:
- ["卡卡","C罗","梅西"]
执行结果
代码语言:txt复制ok: [servera] => (item=(0, u'u5361u5361')) => {
"item": [
0,
"卡卡"
],
"msg": "0-卡卡"
}
ok: [servera] => (item=(2, u'u6885u897f')) => {
"item": [
2,
"梅西"
],
"msg": "2-梅西"
}
ok: [servera] => (item=(1, u'Cu7f57')) => {
"item": [
1,
"C罗"
],
"msg": "1-C罗"
}
步长
与python中range一样,可以指定开始、结束、步长
代码语言:txt复制[student@workstation ansible]$ cat loop.yml
- hosts: servera
gather_facts: no
tasks:
- debug:
msg: "{{ item }}"
with_sequence: start=1 end=3 stride=1
执行结果
代码语言:txt复制ok: [servera] => (item=1) => {
"item": "1",
"msg": "1"
}
ok: [servera] => (item=3) => {
"item": "3",
"msg": "3"
}
ok: [servera] => (item=2) => {
"item": "2",
"msg": "2"
}
其它写法:
标准yaml写法
代码语言:txt复制with_sequence:
start=1
end=3
stride=1
与上面等同
代码语言:txt复制with_sequence: count=3
自定义起始结束步长
代码语言:txt复制with_sequence: start=3 end=9 stride=3
倒数
代码语言:txt复制with_sequence: start=6 end=2 stride=-1
保留2位小数
代码语言:txt复制with_sequence: start=2 end=6 stride=2 format="number is %0.2f"
随机
代码语言:txt复制[student@workstation ansible]$ cat loop.yml
- hosts: servera
gather_facts: no
tasks:
- debug:
msg: "{{ item }}"
with_random_choice:
- 123
- 456
- 789
- 111
- 222
- 333
字典迭代
代码语言:txt复制[student@workstation ansible]$ dic_iter.yaml
- hosts: myhosts
remote_user: root
tasks:
- name: add some user
user: name={{ item.name }} group= {{ item.group }} state=present
with_items:
- { name: 'shenqi1',group: 'wheel' }
- { name: 'shenqi2',group: 'wheel' }
- { name: 'shenqi3',group: 'wheel' }
变量迭代
代码语言:txt复制[student@workstation ansible]$ var_iter.yaml
- hosts: all
remote_user: root
tasks:
- name: loop
block:
- debug:
msg: "{{ hostvars[item]['ansible_default_ipv4']['address'] }}"
with_items: "{{ groups['all'] }}"
- debug:
msg: "{{ hostvars[item]['ansible_hostname']}}"
with_items: "{{ groups['all'] }}"
- debug:
msg: "{{ hostvars[item]['ansible_fqdn']}}"
with_items: "{{ groups['all'] }}"
文件迭代
文件内容
使用with_file查看文件的内容
代码语言:txt复制- hosts: servera
gather_facts: no
tasks:
- debug:
msg: "{{ item }}"
with_file:
- /etc/issue
- /etc/hostname
执行查看结果
代码语言:txt复制ok: [servera] => (item=S
Kernel r on an m) => {
"item": "\SnKernel \r on an \m",
"msg": "\SnKernel \r on an \m"
}
ok: [servera] => (item=workstation.lab.example.com) => {
"item": "workstation.lab.example.com",
"msg": "workstation.lab.example.com"
}
目录列表
代码语言:txt复制[student@workstation ansible]$ direc_iter.yaml
- hosts: servera
gather_facts: no
tasks:
- debug:
msg: "{{ item }}"
with_fileglob:
- /etc/host*
执行查看结果
代码语言:txt复制ok: [servera] => (item=/etc/host.conf) => {
"item": "/etc/host.conf",
"msg": "/etc/host.conf"
}
ok: [servera] => (item=/etc/hosts) => {
"item": "/etc/hosts",
"msg": "/etc/hosts"
}
ok: [servera] => (item=/etc/hostname) => {
"item": "/etc/hostname",
"msg": "/etc/hostname"
}
ok: [servera] => (item=/etc/hosts.deny) => {
"item": "/etc/hosts.deny",
"msg": "/etc/hosts.deny"
}
ok: [servera] => (item=/etc/hosts.allow) => {
"item": "/etc/hosts.allow",
"msg": "/etc/hosts.allow"
}