playbook循环语句

1.定义变量安装多服务

[root@m01 ~]# vim install.yml 
- hosts: nfs
  tasks:
    - name: Install Server
      yum: 
        name: "{{ package }}"
        state: present
      vars:
        package:
          - httpd
          - nfs-utils
          - mariadb-server

2.定义变量启动多服务

[root@m01 ~]# cat install.yml 
- hosts: nfs
  vars:
    package:
      - httpd
      - nfs-utils
      - mariadb-server
  tasks:
    - name: Install Server
      yum: 
        name: "{{ package }}"
        state: present
  
    - name: Start Server
      systemd:
        name: "{{ item }}"
        state: started
        enabled: yes
      with_items:
        - httpd
        - nfs-utils
        - mariadb

3.字典定义变量

1)创建多个用户组

 - name: Create www Group
      group:
        name: "{{ item.name }}"
        gid: "{{ item.gid }}"
      with_items:
        - { name: www, gid: 666 }
        - { name: lhd, gid: 777 }
        - { name: dsb, gid: 888 }

2)创建多个用户

    - name: Create www User
      user:
        name: "{{ item.name }}"
        uid: "{{ item.uid }}"
        group: "{{ item.name }}"
        shell: "{{ item.shell }}"
        create_home: "{{ item.create_home }}"
      with_items:
        - { name: www, uid: 666, shell: "/sbin/nologin", create_home: no }
        - { name: lhd, uid: 777, shell: "/bin/bash", create_home: no }
        - { name: dsb, uid: 888, shell: "/bin/bash", create_home: yes }
原文地址:https://www.cnblogs.com/chenlifan/p/13777419.html