1.为不同用户定义不同组
--- - hosts: web2 remote_user: root tasks: - user: name: "{{item.name}}" group: "{{item.group}}" password: "{{‘123456‘|password_hash(‘sha512‘)}}" with_items: - {name: "aa", group: "users"} - {name: "bb", group: "mail" } - {name: "cc", group: "wheel"} - {name: "dd", group: "root" }
运行
# ansible-playbook add.yml
注意item.name 中间是点,组是自己存在的
2.嵌套循环,循环添加多用户
--- - hosts: web2 remote_user: root vars: un: [a, b, c] id: [1, 2, 3] tasks: - name: add users shell: echo {{item}} with_nested: - "{{un}}" - "{{id}}"
原文:https://www.cnblogs.com/lsgo/p/10422959.html