我正在嘗試創建一個將根據設備數量回圈的劇本,為每個設備創建一個不同的檔案,然后每個都在其中獲得一個與檔案名匹配的主機名。
看起來回圈快取了兩個檔案名,但沒有創建,然后忘記了回圈中的第一個,記住了它的屬性,但使用了回圈中第二個的預期檔案名。
已經有一段時間了,我是否遺漏了一些明顯的東西?
劇本/test.yaml
- name: Create Folder Structure
hosts: switches
gather_facts: false
vars:
project_name: test
switch_stacks:
- id: 01
installation_floor: 1
- id: 02
installation_floor: 2
device_name: swi
site_abbrev: lon
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 }}
switch_device_name: '{{ device_name|lower }}-{{ site_abbrev|lower }}-{{ item.installation_floor }}{{ "d" | format(item.id) }}'
loop: "{{ switch_stacks }}"
- name: Create Switch Configurations
hosts: switches
gather_facts: false
tasks:
- name: Generate Switch Configurations
template:
src: /home/usr/templates/switch-template.j2
dest: "{{ dest_dir }}/{{ switch_device_name }}"
lstrip_blocks: yes
delegate_to: localhost
看之前的檔案夾結構:
usr@ans:~$ tree complete_config/test
complete_config/test [error opening dir]
0 directories, 0 files
劇本的運行:
usr@ans:~$ ansible-playbook playbooks/test.yaml
PLAY [Create Folder Structure] **************************************************************************************
TASK [Create Site Specific Folder] **********************************************************************************
changed: [switch]
TASK [Set Destination Directory & Filename for Switch Configurations] ***********************************************
ok: [switch] => (item={'id': 1, 'installation_floor': 1})
ok: [switch] => (item={'id': 2, 'installation_floor': 2})
PLAY [Create Switch Configurations] *********************************************************************************
TASK [Generate Switch Configurations] *******************************************************************************
changed: [switch -> localhost]
PLAY RECAP **********************************************************************************************************
switch : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
之后的檔案夾結構:
usr@ans:~$ tree complete_config/test/
complete_config/test/
└── swi-lon-202
0 directories, 1 file
uj5u.com熱心網友回復:
你用set_fact錯了。由于您覆寫了您在每次迭代中設定的變數,因此只保留最后一個。你想做的是:
- 擺脫你
set_fact在這里沒用的東西 - 擺脫第二個游戲并在第一個游戲結束時移動任務,像這樣更改它(現場撰寫且未經測驗)
- name: Generate Switch Configurations
vars:
dest_dir: /home/usr/complete_config/{{ project_name }}
switch_device_name: '{{ device_name|lower }}-{{ site_abbrev|lower }}-{{ item.installation_floor }}{{ "d" | format(item.id) }}'
template:
src: /home/usr/templates/switch-template.j2
dest: "{{ dest_dir }}/{{ switch_device_name }}"
lstrip_blocks: yes
delegate_to: localhost
loop: "{{ switch_stacks }}"
uj5u.com熱心網友回復:
謝謝Zeitounator!那么什么時候應該使用 set_fact 呢?我應該更頻繁地使用 vars 嗎?
以下內容已根據您的建議進行了更改,并經過測驗,生成了兩個檔案,我需要處理與檔案名匹配的主機名條目,但我會弄清楚,這是投入數小時后向前邁出的一大步,謝謝!
- name: Create Folder Structure
hosts: switches
gather_facts: false
vars:
project_name: test
switch_stacks:
- id: 01
installation_floor: 1
- id: 02
installation_floor: 2
device_name: swi
site_abbrev: lon
tasks:
- name: Create Site Specific Folder
file:
path: /home/usr/complete_config/{{ project_name }}
state: directory
mode: 0755
- name: Set Destination Directory the Filename and Generate Switch Configurations
vars:
dest_dir: /home/usr/complete_config/{{ project_name }}
switch_device_name: '{{ device_name|lower }}-{{ site_abbrev|lower }}-{{ item.installation_floor }}{{ "d" | format(item.id) }}'
template:
src: /home/usr/templates/switch-template.j2
dest: "{{ dest_dir }}/{{ switch_device_name }}"
lstrip_blocks: yes
delegate_to: localhost
loop: "{{ switch_stacks }}"
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/454528.html
標籤:python-3.x 循环 可靠的 神社2 剧本
