Ansible playbook 编程( 三 )

 
使用 with_items 方式
其中 playbook 文件中仅把 loop 变为了 with_items 。
1 [yun@ansi-manager object04]$ pwd 2 /app/ansible_info/object04 3 [yun@ansi-manager object04]$ ll 4 total 16 5 drwxrwxr-x 2 yun yun56 Oct 26 16:03 file 6 -rw-rw-r-- 1 yun yun 1205 Oct 26 16:02 test_with_items_hash.yml 7 [yun@ansi-manager object04]$ cat file/config_test.conf.j28 111 9 [yun@ansi-manager object04]$ cat file/yml_test_j2.yml 10 22211 [yun@ansi-manager object04]$ cat test_with_items_hash.yml 12 ---13 # 使用循环字典创建多个用户,创建多个文件,拷贝多个文件14 - hosts: proxyservers15 16tasks:17- name: "Create multiple user"18user:19name: "{{ item.user }}"20groups: "{{ item.groups }}"21with_items:22- { user: "testuser1", groups: "root" }23- { user: "testuser2", groups: "root,yun" }24 25- name: "Create multiple file or dir"26file:27path: "{{ item.path }}"28owner: "{{ item.owner }}"29group: "{{ item.group }}"30mode: "{{ item.mode }}"31state: "{{ item.state }}"32with_items:33- { path: "/tmp/with_items_testdir", owner: "yun", group: "root", mode: "755", state: "directory" }34- { path: "/tmp/with_items_testfile", owner: "bin", group: "bin", mode: "644", state: "touch" }35 36- name: "copy multiple file"37copy:38src: "{{ item.src }}"39dest: "{{ item.dest }}"40owner: "{{ item.owner }}"41group: "{{ item.group }}"42with_items:43- { src: "./file/config_test.conf.j2", dest: "/tmp/with_items_testdir/", owner: "yun", group: "root" }44- { src: "./file/yml_test_j2.yml", dest: "/tmp/yml_test.yml", owner: "yun", group: "yun" }45 46 [yun@ansi-manager object04]$ ansible-playbook -b -i ../hosts_key --syntax-check test_with_items_hash.yml# 语法检测47 [yun@ansi-manager object04]$ ansible-playbook -b -i ../hosts_key -C test_with_items_hash.yml# 预执行,测试执行48 [yun@ansi-manager object04]$ ansible-playbook -b -i ../hosts_key test_with_items_hash.yml# 执行 
遍历字典示例:
使用 loop 方式【推荐】
1 [yun@ansi-manager object04]$ pwd 2 /app/ansible_info/object04 3 [yun@ansi-manager object04]$ ll 4 total 28 5 -rw-rw-r-- 1 yun yun452 Oct 26 16:46 test_loop_dict.yml 6 [yun@ansi-manager object04]$ cat test_loop_dict.yml7 --- 8 # 打印信息 9 - hosts: manageservers10vars:11users:12alice:13name: Alice Appleworth14telephone: 123-456-789015bob:16name: Bob Bananarama17telephone: 987-654-321018 19tasks:20- name: "print user info"21debug:22msg: "User {{ item.key }}, userfullname: {{ item.value.name }} ({{ item.value.telephone }})"23# 将字典转换为适合循环的项表第一种方式推荐24loop: "{{ users|dict2items }}"25#loop: "{{ lookup('dict', users) }}"26 27 [yun@ansi-manager object04]$ ansible-playbook -b -i ../hosts_key --syntax-check test_loop_dict.yml# 语法检测28 [yun@ansi-manager object04]$ ansible-playbook -b -i ../hosts_key -C test_loop_dict.yml# 预执行,测试执行29 [yun@ansi-manager object04]$ ansible-playbook -b -i ../hosts_key test_loop_dict.yml# 执行 
使用 with_items 方式
1 [yun@ansi-manager object04]$ pwd 2 /app/ansible_info/object04 3 [yun@ansi-manager object04]$ ll 4 total 28 5 -rw-rw-r-- 1 yun yun458 Oct 26 16:47 test_with_items_dict.yml 6 [yun@ansi-manager object04]$ cat test_with_items_dict.yml7 --- 8 # 打印信息 9 - hosts: manageservers10vars:11users:12alice:13name: Alice Appleworth14telephone: 123-456-789015bob:16name: Bob Bananarama17telephone: 987-654-321018 19tasks:20- name: "print user info"21debug:22msg: "User {{ item.key }}, userfullname: {{ item.value.name }} ({{ item.value.telephone }})"23# with_dict 会直接解析字典24with_dict: "{{ users }}"25 26 [yun@ansi-manager object04]$ ansible-playbook -b -i ../hosts_key --syntax-check test_with_items_dict.yml# 语法检测27 [yun@ansi-manager object04]$ ansible-playbook -b -i ../hosts_key -C test_with_items_dict.yml# 预执行,测试执行28 [yun@ansi-manager object04]$ ansible-playbook -b -i ../hosts_key test_with_items_dict.yml# 执行 
变量循环-vars针对yum 安装多个包很有用,其他则会报出警告 。
1 [yun@ansi-manager object04]$ pwd 2 /app/ansible_info/object04 3 [yun@ansi-manager object04]$ ll 4 total 36 5 -rw-rw-r-- 1 yun yun252 Oct 26 17:46 test_cycle_vars.yml 6 [yun@ansi-manager object04]$ cat test_cycle_vars.yml7 --- 8 # 批量包安装 9 - hosts: proxyservers10 11tasks:12- name: "Install multiple packages"13yum:14name: "{{ multi_package }}"15state: present16vars:17multi_package:18- tree19- nc20- tcpdump21 22 [yun@ansi-manager object04]$ ansible-playbook -b -i ../hosts_key --syntax-check test_cycle_vars.yml# 语法检测23 [yun@ansi-manager object04]$ ansible-playbook -b -i ../hosts_key -C test_cycle_vars.yml# 预执行,测试执行24 [yun@ansi-manager object04]$ ansible-playbook -b -i ../hosts_key test_cycle_vars.yml# 执行


推荐阅读