ansible:找到重复的字典键(提示)

时间:2020-02-24 12:51:16

标签: ansible command prompt

我正在尝试使用ansible将cisco交换机配置备份到tftp。

我是一个很生气的新手,但是却管理了一个简单的班轮以在开关中获取和设置选项。备份到tftp命令暂时不起作用。

显然,命令参数存储在python词典中,当我尝试使用提示符:和答案:在一个命令中两次选择选项时,我得到一个错误。

[WARNING]: While constructing a mapping from backup-cisco-tftp.yml, line 11, column 9, found a duplicate dict key (prompt). Using last defined value only.

任务说明:

fatal: [SW3]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "msg": "argument commands is of type <type 'dict'> and we were unable to convert to list: <type 'dict'> cannot be converted to a list"}

代码是:

      gather_facts: false
      connection: network_cli
      tasks:
      - name: backup to tftp
        ios_command:
          commands:
            command: "copy running-config tftp:"
            prompt: "remote host"
            answer: "1.5.1.2"
            prompt: "filename"
            answer: "backup-{{ inventory_hostname }}.txt"

我想必须有一种等待文本并添加两次响应的方法吗?

预先感谢

Wouter

2 个答案:

答案 0 :(得分:0)

请尝试以下操作

    - name: play
      hosts: effe
      gather_facts: false
      connection: network_cli
      tasks:
      - name: backup to tftp
        cli_command:
         command: "copy running-config tftp:"
         check_all: True
         prompt: 
          - "remote host"
          - "filename"
         answer: 
          - "1.5.1.2"
          - "backup-{{ inventory_hostname }}.txt"

答案 1 :(得分:0)

对我而言唯一有效的方法是使用位于[]中的提示部分。我的错误是我指定了完整的提示符,例如“主机名或IP地址(Ctrl-c中止):[2.2.2.2]?”。而我只能选择“ [2.2.2.2]”。

---
 - name: run commands that require answering a prompt
   hosts: ios_xr_routers:1.1.1.1
   gather_facts: no
   connection: network_cli
   vars:
     ansible_network_os: ios


   tasks:
   - name: backup
     cli_command:
      command: "copy running-config tftp://2.2.2.2/test vrf MGMT"
      check_all: True
      prompt:
       - "[2.2.2.2;MGMT]"
       - "[/test]"
      answer:
       - "2.2.2.2"
       - "test"