Ansible 脚本使用循环方法将票证分配给用户列表

问题描述 投票:0回答:1

我有两个清单:

inc_numbers: [A, B, C, D, E, F, G] 

users:
- username: u1
  status: active
  
- username: u2
  status: active
  
- username: u3
  status: active

- username: u4
  status: active

假设第一个列表是票证,第二个列表是用户。 我如何循环这个?如何使用 Ansible 以循环方式分配这些票证?

我尝试了以下方法

  - name: with_together -> loop
    set_fact:
      inc_user_map: "{{ inc_user_map|default([]) + [{ 'inc_numbers':item.0, 'users':item.1.username }] }}"
      with_together:
      - "{{ inc_numbers }}"
      - "{{ users }}"`

  - name: Nested loop
    set_fact:
        inc_sel: "{{ inc_sel|d([]) + [item|combine({'selection': selection})] }}"
    loop: "{{ users }}"
    vars:
      username: "{{ item.username }}"
      selection: "{{ inc_numbers|
                     zip(username)|
                     rejectattr('0', 'eq', '0') }}"

这不会给出所需的输出。用户单循环完成后结束

所需输出

A : u1 
B : u2 
C : u3 
D : u1 
E : u2
F : u3 
G : u1
ansible scripting nested-loops
1个回答
0
投票

以下不言自明且完整的剧本满足您的要求:

---
- hosts: localhost
  gather_facts: false

  vars:
    inc_numbers: [ A, B, C, D, E, F, G ]

    users:
      - username: u1
        status: active
      - username: u2
        status: active
      - username: u3
        status: active
      - username: u4
        status: active

    user_rounds: "{{ (inc_numbers | length / users | length) | round(0, 'ceil') }}"

    repeated_users_names_only: "{{ users | map(attribute='username') * user_rounds | int  }}"

    zipped_tickets_to_user: "{{ inc_numbers | zip(repeated_users_names_only) }}"

    ticket_to_user_dict: "{{ dict(zipped_tickets_to_user) }}"


  tasks:
    - name: "Count how many times you need to repeat your full user list
        to cover the number of tickets"
      ansible.builtin.debug:
        var: user_rounds

    - name: "Extract the user names and repeat them as needed"
      ansible.builtin.debug:
        var: repeated_users_names_only

    - name: "Zip together the elements from tickets and user names"
      ansible.builtin.debug:
        var: zipped_tickets_to_user

    - name: "Use the zipped list as key/value pairs to create a dict"
      ansible.builtin.debug:
        var: ticket_to_user_dict

运行上面的剧本会给出:

PLAY [localhost] *********************************************************************************************************************************************************************************************************************

TASK [Count how many times you need to repeat your full user list to cover the number of tickets] ************************************************************************************************************************************
ok: [localhost] => {
    "user_rounds": "2.0"
}

TASK [Extract the user names and repeat them as needed] ******************************************************************************************************************************************************************************
ok: [localhost] => {
    "repeated_users_names_only": [
        "u1",
        "u2",
        "u3",
        "u4",
        "u1",
        "u2",
        "u3",
        "u4"
    ]
}

TASK [Zip together the elements from tickets and user names] *************************************************************************************************************************************************************************
ok: [localhost] => {
    "zipped_tickets_to_user": [
        [
            "A",
            "u1"
        ],
        [
            "B",
            "u2"
        ],
        [
            "C",
            "u3"
        ],
        [
            "D",
            "u4"
        ],
        [
            "E",
            "u1"
        ],
        [
            "F",
            "u2"
        ],
        [
            "G",
            "u3"
        ]
    ]
}

TASK [Use the zipped list as key/value pairs to create a dict] ***********************************************************************************************************************************************************************
ok: [localhost] => {
    "ticket_to_user_dict": {
        "A": "u1",
        "B": "u2",
        "C": "u3",
        "D": "u4",
        "E": "u1",
        "F": "u2",
        "G": "u3"
    }
}

PLAY RECAP ***************************************************************************************************************************************************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
© www.soinside.com 2019 - 2024. All rights reserved.