将一个变量分配给Ansible中当前列表/文件中的另一个变量

Assigning a variable to another variable in the current list / file in Ansible

我有一个 playbook.yml 文件可以读取清单文件,例如 service_1.yml 和 service_2.yml。 我需要重新分配 ram_min 变量并添加一些数字。

playbook.yml
---
- hosts: 127.0.0.1
  connection: local
  gather_facts: false
  tasks:
  - name: "Include var"
    set_fact:
      ram_list: "{{ ram_list | default([]) + [ lookup('file',item) | from_yaml ]}}"
    with_items:
      - service_1.yml
      - service_2.yml

  - debug:
      var: ram_list
...

service_1.yml
---
name: service_1
ram_min: 1024
ram_max: "{{ ( ram_min + 256 ) | int }}"
...

service_2.yml
---
name: service_2
ram_min: 2048
ram_max: "{{ ( ram_min + 256 ) | int }}"
...

结果,我得到:

TASK [debug] *********************************************
ok: [127.0.0.1] => {
    "ram_list": [
        {
            "name": "service_1",
            "ram_max": "{{ ( ram_min + 256 ) | int }}",
            "ram_min": 1024
        },
        {
            "name": "service_2",
            "ram_max": "{{ ( ram_min + 256 ) | int }}",
            "ram_min": 2048
        }
    ]
}

我想看:

TASK [debug] *********************************************
ok: [127.0.0.1] => {
    "ram_list": [
        {
            "name": "service_1",
            "ram_max": 1280,
            "ram_min": 1024
        },
        {
            "name": "service_2",
            "ram_max": 2304,
            "ram_min": 2048
        }
    ]
}

告诉我,我该如何解决这个问题?

PS。我确实需要导入库存文件,我通过 - "lookup ('file' .."

更新

playbook.yml
---
- hosts: 127.0.0.1
  connection: local
  gather_facts: false
  tasks:
  - name: "Include var"
    include_tasks:
      file: include_variables.yml
    with_filetree:
      - "{{ workspace_temp_dir }}"
    when:
      - app_list_temp.state == 'file'
      - app_list_temp.path.split('/')[0] in app | default(app_list_temp.path.split('/')[0])
      - not app_list_temp.path.split('/')[0] is match(exclude) | default([])
      - app_list_temp.path.split('/')[-1] == 'main.yml'
    loop_control:
      loop_var: app_list_temp
...

include_variables.yml
---
- name: "Include variable files to /"
  include_vars:
    file: "{{ app_list_temp.src }}"
- name: "Include variable files to /temp_list"
  include_vars:
    file: "{{ app_list_temp.src }}"
    name: temp_list
- name: "Combine variables to list"
  set_fact:
    app_list_combine: "{{ app_list_combine | default([]) + [ temp_list ] }}"
...

问题是lookup('file',item)没有展开内容。需要 include_vars。因此需要 include_vars + set_fact 的循环,但不可能循环一个块。 IMO 唯一的解决方案是循环 include_tasks。下面的戏

- name: "Include var"
  include_tasks: create-ramlist.yml
  loop:
    - service_1.yml
    - service_2.yml
- debug:
    var: ram_list

create-ramlist.yml
---
- include_vars: "{{ item }}"
- set_fact:    
    ram_list: "{{ ram_list | default([]) + [ {'name': name,
                                              'ram_max': ram_max,
                                              'ram_min': ram_min} ] }}"
...

给出:

"ram_list": [
    {
        "name": "service_1", 
        "ram_max": "1280", 
        "ram_min": 1024
    }, 
    {
        "name": "service_2", 
        "ram_max": "2304", 
        "ram_min": 2048
    }
]