ansible系列(24)--ansible的loop循环语句
1. loop循环语句
在写 playbook
的时候发现了很多 task 都要重复引用某个相同的模块,比如一次启动10个服务,或者一次拷贝10个文件,如果按照传统的写法最少要写10次,这样会显得 playbook
很臃肿。如果使用循环的方式来编写 playbook
,这样可以减少重复编写 task
带来的臃肿。
引用格式:
- 对迭代项的引用,固定变量名为
"item"
; - 要在
task
中使用with_items
或loop
关键字给定要迭代的元素列表;
1.1 使用循环批量安装软件
-
方式一:
[root@xuzhichao playbook]# cat loop_install.yml - hosts: NginxWebs remote_user: root tasks: - name: Install Packages yum: name: "{{ item }}" state: present loop: - httpd - mariadb-server
-
运行
playbook
:root@xuzhichao playbook]# ansible-playbook loop_install.yml PLAY [NginxWebs] ********************************************************************************************************************************************** TASK [Install Packages] *************************************************************************************************************************************** ok: [192.168.20.23] => (item=httpd) ok: [192.168.20.22] => (item=httpd) changed: [192.168.20.23] => (item=mariadb-server) changed: [192.168.20.22] => (item=mariadb-server) PLAY RECAP **************************************************************************************************************************************************** 192.168.20.22 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.20.23 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
-
方式二:
[root@xuzhichao playbook]# cat loop_install1.yml - hosts: NginxWebs remote_user: root vars: packages: - httpd - mariadb-server tasks: - name: Install Packages yum: name: "{{ packages }}" state: present
-
运行
playbook
:[root@xuzhichao playbook]# ansible-playbook loop_install1.yml PLAY [NginxWebs] ********************************************************************************************************************************************** TASK [Install Packages] *************************************************************************************************************************************** changed: [192.168.20.23] changed: [192.168.20.22] PLAY RECAP **************************************************************************************************************************************************** 192.168.20.22 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.20.23 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
1.2 使用循环批量启动服务
playbook文件如下:
[root@xuzhichao playbook]# cat loop_service.yml
- hosts: NginxWebs
remote_user: root
tasks:
- name: Start Service
service:
name: "{{ item }}"
state: started
loop:
- httpd
- mariadb-server
运行playbook:
[root@xuzhichao playbook]# ansible-playbook -C loop_service.yml
PLAY [NginxWebs] **********************************************************************************************************************************************
TASK [Start Service] ******************************************************************************************************************************************
changed: [192.168.20.23] => (item=httpd)
changed: [192.168.20.22] => (item=httpd)
changed: [192.168.20.23] => (item=mariadb-server)
changed: [192.168.20.22] => (item=mariadb-server)
PLAY RECAP ****************************************************************************************************************************************************
192.168.20.22 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.20.23 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
1.3 使用循环批量创建用户
批量创建用户,使用 key values
字典的方式。
[root@xuzhichao playbook]# cat loop_createuser.yml
- hosts: NginxWebs
tasks:
- name: Create Groups
group:
name: "{{ item }}"
state: present
loop:
- group1
- group2
- group3
- name: Create Users
user:
name: "{{ item.user }}"
group: "{{ item.group }}"
uid: "{{ item.uid }}"
state: present
create_home: yes
loop:
- { user: user1, group: group1, uid: 2001 }
- { user: user2, group: group2, uid: 2002 }
- { user: user3, group: group3, uid: 2003 }
运行playbook
:
[root@xuzhichao playbook]# ansible-playbook -C loop_createuser.yml
PLAY [NginxWebs] **********************************************************************************************************************************************
TASK [Create Groups] ******************************************************************************************************************************************
ok: [192.168.20.23] => (item=group1)
ok: [192.168.20.22] => (item=group1)
ok: [192.168.20.23] => (item=group2)
ok: [192.168.20.22] => (item=group2)
changed: [192.168.20.23] => (item=group3)
changed: [192.168.20.22] => (item=group3)
TASK [Create Users] *******************************************************************************************************************************************
changed: [192.168.20.22] => (item={u'group': u'group1', u'user': u'user1', u'uid': 2001})
changed: [192.168.20.23] => (item={u'group': u'group1', u'user': u'user1', u'uid': 2001})
changed: [192.168.20.22] => (item={u'group': u'group2', u'user': u'user2', u'uid': 2002})
changed: [192.168.20.23] => (item={u'group': u'group2', u'user': u'user2', u'uid': 2002})
changed: [192.168.20.23] => (item={u'group': u'group3', u'user': u'user3', u'uid': 2003})
changed: [192.168.20.22] => (item={u'group': u'group3', u'user': u'user3', u'uid': 2003})
PLAY RECAP ****************************************************************************************************************************************************
192.168.20.22 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.20.23 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
1.4 使用循环批量拷贝文件
在需要拷贝多个文件,这些文件又有不同的属主属组权限等属性时,而且拷贝到的目标目录不同时,可以使用循环来拷贝文件。
示例,playbook
文件如下:
[root@xuzhichao playbook]# cat loop_copyfile.yml
- hosts: NginxWebs
tasks:
- name: Copy Configuer File
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
owner: "{{ item.owner }}"
group: "{{ item.group }}"
omode: "{{ item.mode }}"
loop:
- { src: "conf/nginx.conf.j2", dest: "/etc/nginx/nginx.conf", owner: "root", group: "root", mode: "0644" }
- { src: "conf/example.com.conf.j2", dest: "/etc/nginx/conf.d/example.con.conf", owner: "root", group: "root", mode: "0644" }
- { src: "file/index.html", dest: "/data/nginx/example/index.html", owner: "nginx", group: "nginx", mode: 0644 }
创建相关模板文件:
[root@xuzhichao playbook]# touch conf/nginx.conf.j2
[root@xuzhichao playbook]# touch conf/example.com.conf.j2
[root@xuzhichao playbook]# touch file/index.html
运行playbook
:
[root@xuzhichao playbook]# ansible-playbook -C loop_copyfile.yml
PLAY [NginxWebs] ****************************************************************************************************************************
TASK [Copy Configuer File] *****************************************************************************************************************************
changed: [192.168.20.23] => (item={u'dest': u'/etc/nginx/nginx.conf', u'src': u'conf/nginx.conf.j2', u'group': u'root', u'mode': u'0644', u'owner': u'root'})
changed: [192.168.20.22] => (item={u'dest': u'/etc/nginx/nginx.conf', u'src': u'conf/nginx.conf.j2', u'group': u'root', u'mode': u'0644', u'owner': u'root'})
changed: [192.168.20.22] => (item={u'dest': u'/etc/nginx/conf.d/example.con.conf', u'src': u'conf/example.com.conf.j2', u'group': u'root', u'mode': u'0644', u'owner': u'root'})
changed: [192.168.20.23] => (item={u'dest': u'/etc/nginx/conf.d/example.con.conf', u'src': u'conf/example.com.conf.j2', u'group': u'root', u'mode': u'0644', u'owner': u'root'})
changed: [192.168.20.23] => (item={u'dest': u'/data/nginx/example/index.html', u'src': u'file/index.html', u'group': u'nginx', u'mode': 420, u'owner': u'nginx'})
changed: [192.168.20.22] => (item={u'dest': u'/data/nginx/example/index.html', u'src': u'file/index.html', u'group': u'nginx', u'mode': 420, u'owner': u'nginx'})