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

时间:2019-03-20 16:17:08

标签: variables ansible ansible-inventory

我有一个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 ] }}"
...

1 个答案:

答案 0 :(得分:0)

问题在于 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
    }
]