[英]unable to create dictionary from variables with ansible and group_vars
將ansible設置為group_vars很好。 在此上下文中,每個服務器都有兩個雙端口NIC(Mellanox)。 網絡配置是通過netplan的group_vars完成的。 雖然network:bond0:interfaces
就像一個超級network:ethernets:
一樣沒有:
## group_vars/my-server-group
Mellanox_1: enp3s0
Mellanox_2: enp5s0
bond0_interfaces:
- "{{ Mellanox_1 }}"
- "{{ Mellanox_1 }}d1"
- "{{ Mellanox_2 }}"
- "{{ Mellanox_2 }}d1"
interface_no_dhcp:
dhcp4: no
interface_array: [ '{{ Mellanox_1 }}', '{{ Mellanox_1 }}d1', '{{ Mellanox_2 }}', '{{ Mellanox_2 }}d1' ]
# ^^^ works, but is useless
interface_array: { '{{ Mellanox_1 }}', '{{ Mellanox_1 }}d1', '{{ Mellanox_2 }}', '{{ Mellanox_2 }}d1' }
# ^^^ doesn't work
netplan_config_file: /etc/netplan/netplan_ansible.yaml
netplan_configuration:
network:
ethernets:
"{{ interface_array }}"
bonds:
bond0:
interfaces: "{{ bond0_interfaces }}"
parameters:
mode: 802.3ad
結果是:
## /etc/netplan/netplan.yaml (excerpt)
netplan_configuration:
network:
ethernets:
"{{ Mellanox_1 }}": <-- instead of 'enp3s0:'
"{{ Mellanox_1 }}d1":
"{{ Mellanox_2 }}":
"{{ Mellanox_2 }}d1":
另外,以下操作無效,並導致錯誤: recursive loop detected in template string
:
## group_vars/my-server-group
ethernet_interfaces: |
"{% for interface in bond0_interfaces %}"
"{{ ethernet_interfaces|combine({interface: interface_no_dhcp}) }}"
"{% endfor %}"
對於數組(例如bond0_interfaces),它可以按預期與字典一起工作,但會失敗。 據我所知,為了遵循netplan配置指南( 在此進行說明 ),我需要一個字典而不是一個數組。 最后應該是這樣的:
## /etc/netplan/netplan.yaml (excerpt)
network:
ethernets:
enp3s0:
dhcp4: no
enp3s0d1:
dhcp4: no
enp5s0:
dhcp4: no
enp5s0d1:
dhcp4: no
bonds:
bond0:
interfaces:
- enp3s0
- enp3s0d1
- enp5s0
- enp5s0d1
parameters:
mode: 802.3ad
在這個線程中,用戶holdenweb說:
“動態變量名幾乎總是一個糟糕的主意”
在這種情況下,這是絕對合理的;-)
我還檢查了:
我有解決辦法。 我可以建議使用我的Ansible角色linux_postinstall嗎? 任務netplan可以滿足您的需求。 訣竅是在模板中過濾item.conf
{{ item.conf | from_yaml | to_nice_json }}
以下是在/ scratch中創建以太網和綁定的測試方法。 我尚未使用這些配置文件測試過netplan。 因人而異。 該示例也適用於group_vars 。
>貓netplan.yml
- hosts: localhost
become: yes
become_method: sudo
become_user: root
vars:
Mellanox_1: enp3s0
Mellanox_2: enp5s0
lp_netplan_root: "/scratch"
lp_netplan: True
lp_netplan_renderer: "networkd"
lp_netplan_conf:
- file: "91-ethernet.yaml"
category: "ethernets"
conf: |
{{ Mellanox_1 }}:
dhcp4: no
{{ Mellanox_1 }}d1:
dhcp4: no
{{ Mellanox_2 }}:
dhcp4: no
{{ Mellanox_2 }}d1:
dhcp4: no
- file: "92-bonds.yaml"
category: "bonds"
conf: |
bond0:
interfaces:
- "{{ Mellanox_1 }}"
- "{{ Mellanox_1 }}d1"
- "{{ Mellanox_2 }}"
- "{{ Mellanox_2 }}d1"
parameters:
mode: 802.3ad
roles:
- vbotka.linux_postinstall
> ansible-playbook netplan.yml -t lp_netplan
>貓/scratch/91-ethernet.yaml
# Ansible managed
network:
version: 2
renderer: networkd
ethernets:
{
"enp3s0": {
"dhcp4": false
},
"enp3s0d1": {
"dhcp4": false
},
"enp5s0": {
"dhcp4": false
},
"enp5s0d1": {
"dhcp4": false
}
}
>貓/scratch/92-bonds.yaml
# Ansible managed
network:
version: 2
renderer: networkd
bonds:
{
"bond0": {
"interfaces": [
"enp3s0",
"enp3s0d1",
"enp5s0",
"enp5s0d1"
],
"parameters": {
"mode": "802.3ad"
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.