Ansible 剧本循环只调用最终循环
Ansible playbook loop only calling final loop
我正在尝试创建一个根据设备数量循环播放的剧本,为每个设备创建一个不同的文件,然后每个文件都获得一个与文件名相匹配的主机名。
看起来循环缓存了 2 个文件名,但没有创建,后来忘记了循环中的第一个文件名,记住了它的属性但使用了循环中第二个文件名的预期文件名。
已经有一段时间了,我是否遗漏了一些明显的东西?
playbooks/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 }}{{ "%02d" | 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
您使用 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 }}{{ "%02d" | 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 }}"
我正在尝试创建一个根据设备数量循环播放的剧本,为每个设备创建一个不同的文件,然后每个文件都获得一个与文件名相匹配的主机名。
看起来循环缓存了 2 个文件名,但没有创建,后来忘记了循环中的第一个文件名,记住了它的属性但使用了循环中第二个文件名的预期文件名。
已经有一段时间了,我是否遗漏了一些明显的东西?
playbooks/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 }}{{ "%02d" | 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
您使用 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 }}{{ "%02d" | 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 }}"