gpt4 book ai didi

来自站点 yaml 或模板的 Ansible 剧本循环?

转载 作者:行者123 更新时间:2023-12-05 05:43:58 25 4
gpt4 key购买 nike

我正在尝试使用我的 Ansible 剧本来调用站点 YAML 引用来创建一个文件名,该文件名可以为多个开关递增。我究竟做错了什么?我相信剧本是从主机 YAML 中提取的吗?

格式:<switch>-<site>-<floor><stackid>.txt

例如:有两个开关:

  • swi-lon-101.txt
  • swi-lon-202.txt

host_vars/host.yaml

project_name: test
device_name: swi
site_abbrev: lon
device_type: switch
switch_stacks:
- id: 01
installation_floor: 1
- id: 02
installation_floor: 2

templates/switch-template.j2

{% for stack in switch_stacks %}
set system host-name {{ device_name }}-{{ site_abbrev }}-{{ stack.installation_floor }}{{ stack.id }}
{% endfor %}

问题所在的剧本,我如何获取主机名以便为 2 个交换机中的每一个正确创建?

我的剧本:

- name: Create Folder Structure
hosts: junos
gather_facts: false

tasks:
- name: Create Site Specific Folder
file:
path: /home/usr/complete_config/{{ project_name }}
state: directory
mode: 0755

- name: Set Destination Directory & Filename for Switch Configurations
set_fact:
dest_dir: /home/usr/complete_config/{{ project_name }}
full_device_name: "{{ device_name|lower }}-{{ site_abbrev|lower }}-{{ switch_stacks.installation_floor }}{{ switch_stacks.id }}.txt"
when: device_type == 'switch'

Ansible 错误,正在运行:

ansible-playbook playbooks/site-playbook.yaml
TASK [Set Destination Directory & Filename for Switch Configurations] **************************************************
fatal: [site-switch]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'installation_floor'\n\nThe error appears to be in '/home/usr/playbooks/switch-playbook.yaml': line 19, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Set Destination Directory & Filename for Switch Configurations\n ^ here\n"}

最佳答案

因此,您确实需要一个 loop为了设置这个事实,否则,您正在尝试访问列表中的 installation_floor,这是不可能的。

您还将面临 switch_stacks 中项目的 id 问题,因为 01 是一个整数,最终将显示为1,很简单。所以你要么需要将它们声明为字符串,要么用 format filter 填充它们.

所以,你最终完成了这个任务:

- set_fact:
full_device_name: >-
{{
full_device_name
| default([])
+ [
device_name | lower ~ '-' ~
site_abbrev | lower ~ '-' ~
item.installation_floor ~
"%02d" | format(item.id) ~ '.txt'
]
}}
loop: "{{ switch_stacks }}"
when: device_type == 'switch'

这将创建一个列表:

full_device_name:
- swi-lon-101.txt
- swi-lon-202.txt

给定剧本:

- hosts: localhost
gather_facts: false

tasks:
- set_fact:
full_device_name: >-
{{
full_device_name
| default([])
+ [
device_name | lower ~ '-' ~
site_abbrev | lower ~ '-' ~
item.installation_floor ~
"%02d" | format(item.id) ~ '.txt'
]
}}
loop: "{{ switch_stacks }}"
when: device_type == 'switch'
vars:
device_name: swi
site_abbrev: lon
device_type: switch
switch_stacks:
- id: 01
installation_floor: 1
- id: 02
installation_floor: 2

- debug:
var: full_device_name

这会产生:

TASK [set_fact] ************************************************************
ok: [localhost] => (item={'id': 1, 'installation_floor': 1})
ok: [localhost] => (item={'id': 2, 'installation_floor': 2})

TASK [debug] ***************************************************************
ok: [localhost] =>
full_device_name:
- swi-lon-101.txt
- swi-lon-202.txt

关于来自站点 yaml 或模板的 Ansible 剧本循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71696913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com