1. 程式人生 > 實用技巧 >playbook迴圈語句

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 }