Ansible 创建用户并根据 vars 将他们分配给特定的组

Ansible to create users and assign them to a particular group based on vars

Gurus,我正在学习 ansible 并试图了解如何使用变量,我遇到过下面的剧本,但我不理解如何根据我的变量将特定组分配给特定用户已在 vars

下为用户和组定义

我在下面播放我想创建用户 anikarigved 并想将他们分配到 docker 组,同时 ayusheshant 应该分配 test 组。

到目前为止,我还没有实现这一目标的想法。但是截至目前,运行 它会创建用户并将这两个组分配给所有用户。

$ cat nested_playbook-1.yml
---
- name: testing nested play
  hosts: localhost
  vars:
    users:
      - anika
      - rigved
      - ayush
      - eshant
    grps:
      - docker
      - test
  tasks:
    - name: make users members of groups
      user:
        name: "{{ item[0] }}"
        state: present
        groups: "{{ item[0] }}"
      with_nested:
        - "{{ users }}"
        - "{{ grps }}"

您构建数据的方式没有显示用户和组之间的任何关系。一种选择是像这样构建它:

grps:
  - name: docker
    users:
      - anika
      - rigved
  - name: test
    users:
      - ayush
      - eshant

使用此结构,您可以使用 subelements 过滤器遍历 grps,如下所示:

---
- name: testing nested play
  gather_facts: false
  hosts: localhost
  vars:
    grps:
      - name: docker
        users:
          - anika
          - rigved
      - name: test
        users:
          - ayush
          - eshant
  tasks:
    - debug:
        msg:
          user:
            name: "{{ item.1 }}"
            state: present
            groups: "{{ item.0.name }}"
      loop: "{{ grps|subelements('users') }}"
      loop_control:
        label: "{{ item.1 }}"

subelements 过滤器是一种创建 "nested loop" 的方法:它为 grps 中的每个组迭代 'users' 键的每个成员。在每次循环迭代期间,item 是一个二元组,其中第一项是来自 grps 的对应元素,第二项迭代该元素的 users 键。换句话说,你最终迭代了这个列表:

  • [{'name': 'docker', 'users': ['anika', 'rigved']}, anika}
  • [{'name': 'docker', 'users': ['anika', 'rigved']}, rigved}
  • [{'name': 'test', 'users': ['ayush', 'eshant']}, ayush}
  • [{'name': 'test', 'users': ['ayush', 'eshant']}, eshant}

所以在循环中,item.0 指的是组条目本身(因此 item.0.name 是组名),item.1 指的是用户。

PLAY [testing nested play] *******************************************************************

TASK [debug] *********************************************************************************
ok: [localhost] => (item=anika) => {
    "msg": {
        "user": {
            "groups": "docker",
            "name": "anika",
            "state": "present"
        }
    }
}
ok: [localhost] => (item=rigved) => {
    "msg": {
        "user": {
            "groups": "docker",
            "name": "rigved",
            "state": "present"
        }
    }
}
ok: [localhost] => (item=ayush) => {
    "msg": {
        "user": {
            "groups": "test",
            "name": "ayush",
            "state": "present"
        }
    }
}
ok: [localhost] => (item=eshant) => {
    "msg": {
        "user": {
            "groups": "test",
            "name": "eshant",
            "state": "present"
        }
    }
}

PLAY RECAP ***********************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

或者,您可以像这样构建数据:

users:
  - name: anika
    group: docker
  - name: rigved
    group: docker
  - name: ayush
    group: docker
  - name: eshant
    group: docker

你可以只用一个简单的循环来处理这个结构:

---
- name: testing nested play
  gather_facts: false
  hosts: localhost
  vars:
    users:
      - name: anika
        group: docker
      - name: rigved
        group: docker
      - name: ayush
        group: docker
      - name: eshant
        group: docker
  tasks:
    - debug:
        msg:
          user:
            name: "{{ item.name }}"
            state: present
            groups: "{{ item.group }}"
      loop: "{{ users }}"
      loop_control:
        label: "{{ item.name }}"


请注意,在上述两个示例中,我在任务上使用 loop_control 只是为了设置循环标签并使输出看起来更好。您可以从上述任务中删除 loop_control 而不会影响它们的运行方式。