【发布时间】:2018-10-01 14:16:04
【问题描述】:
我正在尝试创建交换机备份。我想根据配置输出和切换主机名创建动态文件。 例如,switch1的配置应该保存在文件名hostname1中,switch2的配置应该保存在文件名hostname2中,以此类推。 我从文件中的交换机获取主机名。
我的问题是,switch1 的配置保存在文件 hostname1、hostname2 等中。 如何正确循环变量以在正确的文件中获取正确的配置?
我目前的剧本是这样的:
---
- hosts: cisco
connection: local
gather_facts: false
vars:
backup_path: /etc/ansible/tests
cli:
host: "{{ inventory_hostname }}"
username: test
password: test
tasks:
- name: show run on switches
ios_command:
commands: show running-config
provider: "{{ cli }}"
register: config
- name: creating folder
file:
path: "{{ backup_path }}"
state: directory
run_once: yes
- name: get hostnames
become: yes
shell: cat /etc/ansible/tests/hostname_ios.txt
register: hostnames
- name: copy config
copy:
content: "{{ config.stdout[0] }}"
dest: "{{ backup_path }}/{{ item }}.txt"
with_together: "{{ hostnames.stdout_lines }}"
...
【问题讨论】:
标签: ansible