ansible 循环语句with_items



这个示例实现了一个批量创建系统用户的功能,with_items会自动循环执行上面的语句user: name={{ item }} state=present groups=wheel,
循环次数为with_items的元素个数,这里有两个元素,分别为testuser1,testuser2会分别替换{{ item }}项


[root@node110 yml]# cat circle.yml 
---
- hosts: 192.168.198.120
  tasks:
    - name: add several users
      user: name={{ item }} state=present groups=wheel
      with_items:
        - testuser1
        - testuser2
客户端:
cat /etc/passwd
testuser1:x:560:560::/home/testuser1:/bin/bash
testuser2:x:561:561::/home/testuser2:/bin/bash


循环也支持列表(list)的形式 :通过with_flattened语句来实现:
#file: roles/foo/vars/main.yml
packages_base:
- [ 'foo-package','bar-package' ]
packages_apps:
- [['one-package'],['two-package']]
  - [['red-package'],['blue-package']]


circle.yml
---
- hosts: 192.168.198.120
  tasks:
- name: flattened loop demo
 yum: name={{ item }} state=installed
 with_flattened:
- packages_base
- packages_apps

分割线
感谢打赏
江西数库信息技术有限公司
YWSOS.COM 平台代运维解决方案
 评论
 发表评论
姓   名:

Powered by AKCMS