基于文件名的主机特定目标
Specific Targeting of Hosts Based on File Name
我正在使用 ansible 将配置“.set”文件发送到使用 "junos_install_config" 模块的 Junos OS 设备。我想根据名称将特定文件发送到特定主机。
例如。我想发送文件 "vMX1.set" 到主机 vMX1,文件 "vMX2.set" 到主机 vMX2 等等。
目前我是这样做的:
---
name: Configure Junos Devices
hosts: all
roles:
- Juniper.junos
connection: local
gather_facts: no
tasks:
- name: Send to Device 1
when: ansible_hostname == vMX1
junos_install_config:
host={{ inventory_hostname }}
file=/home/usr/resources/vMX1.set
overwrite=false
- name: Send to Device 2
when: ansible_hostname == vMX2
junos_install_config:
host={{ inventory_hostname }}
file=/home/usr/resources/vMX2.set
overwrite=false
但是这个方法非常耗时而且不合逻辑。例如,如果我有 50 个配置文件和 50 个设备,我将不得不编写 50 个不同的任务。有没有什么方法可以自动执行此操作,以便剧本检查任务的名称并为文件分配相应的名称?
主机文件如下所示
[vMXrouters]
vMX1 ansible_ssh_host=10.249.89.22
vMX2 ansible_ssh_host=10.249.89.190
Q: "Is there any way to automate this so that the playbook checks the name of the task and assigns the file with the corresponding name?"
答:下面的剧本应该可以完成工作
- name: Configure Junos Devices
hosts: all
vars:
list_of_devices: ['vMX1', 'vMX2']
tasks:
- name: "Send to {{ inventory_hostname }}"
junos_install_config:
host="{{ inventory_hostname }}"
file="/home/usr/resources/{{ inventory_hostname }}.set"
overwrite=false
when: inventory_hostname in list_of_devices
delegate_to: localhost
如果定义了主机组,剧本会更简单
- name: Configure Junos Devices
hosts: vMX_devices
tasks:
- name: "Send to {{ inventory_hostname }}"
junos_install_config:
host="{{ inventory_hostname }}"
file="/home/usr/resources/{{ inventory_hostname }}.set"
overwrite=false
delegate_to: localhost
(未测试)
我正在使用 ansible 将配置“.set”文件发送到使用 "junos_install_config" 模块的 Junos OS 设备。我想根据名称将特定文件发送到特定主机。
例如。我想发送文件 "vMX1.set" 到主机 vMX1,文件 "vMX2.set" 到主机 vMX2 等等。
目前我是这样做的:
---
name: Configure Junos Devices
hosts: all
roles:
- Juniper.junos
connection: local
gather_facts: no
tasks:
- name: Send to Device 1
when: ansible_hostname == vMX1
junos_install_config:
host={{ inventory_hostname }}
file=/home/usr/resources/vMX1.set
overwrite=false
- name: Send to Device 2
when: ansible_hostname == vMX2
junos_install_config:
host={{ inventory_hostname }}
file=/home/usr/resources/vMX2.set
overwrite=false
但是这个方法非常耗时而且不合逻辑。例如,如果我有 50 个配置文件和 50 个设备,我将不得不编写 50 个不同的任务。有没有什么方法可以自动执行此操作,以便剧本检查任务的名称并为文件分配相应的名称?
主机文件如下所示
[vMXrouters]
vMX1 ansible_ssh_host=10.249.89.22
vMX2 ansible_ssh_host=10.249.89.190
Q: "Is there any way to automate this so that the playbook checks the name of the task and assigns the file with the corresponding name?"
答:下面的剧本应该可以完成工作
- name: Configure Junos Devices
hosts: all
vars:
list_of_devices: ['vMX1', 'vMX2']
tasks:
- name: "Send to {{ inventory_hostname }}"
junos_install_config:
host="{{ inventory_hostname }}"
file="/home/usr/resources/{{ inventory_hostname }}.set"
overwrite=false
when: inventory_hostname in list_of_devices
delegate_to: localhost
如果定义了主机组,剧本会更简单
- name: Configure Junos Devices
hosts: vMX_devices
tasks:
- name: "Send to {{ inventory_hostname }}"
junos_install_config:
host="{{ inventory_hostname }}"
file="/home/usr/resources/{{ inventory_hostname }}.set"
overwrite=false
delegate_to: localhost
(未测试)