如何使ios_config任务幂等? [英] How can I make my ios_config task idempotent?

查看:193
本文介绍了如何使ios_config任务幂等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在cisco路由器中创建一个幂等剧本.我在网上阅读了示例,我认为问题出在参数"with_item".

I'm trying to create an idempotent playbook in a cisco router. I read the examples on the web and I think the problem is with the parameter "with_item".

这是戏:

- name: Configurar subinterfaces para las VLAN 10,20 y 30
     ios_config:
        provider: "{{ cli }}"
        parents:
          - interface GigabitEthernet0/0
        lines:
          - interface GigabitEthernet0/0.{{ item.subinterface }}
        after:
          - description {{ item.name }}
          - encapsulation dot1q {{ item.subinterface }}
          - ip address {{ item.ip_add }} 255.255.255.0
      with_items:
        - { subinterface: 10, name: Ventas, ip_add: 192.168.10.1 }
        - { subinterface: 20, name: RRHH, ip_add: 192.168.20.1 }
        - { subinterface: 30, name: Contabilidad, ip_add: 192.168.30.1 }

这是输出:

PLAY [Configurar Router 1 Cisco] ***********************************************

TASK [config_r1_cisco : Configurar subinterfaces para las VLAN 10,20 y 30] *****
changed: [R1] => (item={u'subinterface': 10, u'ip_add': u'192.168.10.1', u'name': u'Ventas'})
changed: [R1] => (item={u'subinterface': 20, u'ip_add': u'192.168.20.1', u'name': u'RRHH'})
changed: [R1] => (item={u'subinterface': 30, u'ip_add': u'192.168.30.1', u'name': u'Contabilidad'})

PLAY RECAP *********************************************************************
R1                         : ok=1    changed=1    unreachable=0    failed=0

当我一次又一次地运行剧本时,输出总是改变而不是确定.可能是因为在"with_items"参数中使用变量时,幂等性没有实现吗?

When I run the playbook again and again, the output always change instead of ok. Could it be because when using variables in the "with_items" parameter the idempotence is not carried out?

推荐答案

有许多原因使您可能会得到幂等命令,但它们与with_items选项无关.

There are many reasons why you might be getting an idempotent command, but they are not related to the with_items options.

一个可能的问题是使用缩写命令(例如,用shut代替shutdown).在为什么config模块总是返回true" 的"Ansible Network FAQ"中的

A possible issue would be using abbreviated commands (shut instead of shutdown for example). On the section "Why do the config modules always return true" of the "Ansible Network FAQ" there it this snippet:

Ansible Network * _config模块将您在行中指定的命令文本与配置中的文本进行比较.如果您在任务的行"部分中使用了shutdown,并且配置显示为shutdown,则即使配置已经正确,该模块也会返回changed = true.您的任务将在每次运行时更新配置.

Ansible Network *_config modules compare the text of the commands you specify in lines to the text in the configuration. If you use shut in the lines section of your task, and the configuration reads shutdown, the module returns changed=true even though the configuration is already correct. Your task will update the configuration every time it runs.

我不相信这是您的情况.

I don't believe this is your case.

另一个原因可能是您没有使用与running_config上相同的命令.因此,即使它们产生的输出相同,行之间的检查也不完全匹配.

Another reason could be that you are not using the same commands as those found on the running_config. And so the checks between the lines are not matching exactly, even though they produce the same output.

虽然应该检查,但也可能不是您的问题.

Also probably not your issue, though you should check.

模块还有一种将更新的行与running_config进行比较的方式.默认情况下,该模块检查每一行,但还有其他选项. 文档说:

There is also the way the module compares the updated lines with the running_config. By default, the module checks each line, but there are other options. The documentation says:

指导模块执行命令集与当前设备配置的匹配.如果将match设置为line,则将逐行匹配命令.如果将match设置为strict,则命令行将根据位置进行匹配.如果将match设置为完全匹配,则命令行必须相等.最后,如果将match设置为none,则模块将不会尝试将源配置与远程设备上正在运行的配置进行比较.

Instructs the module on the way to perform the matching of the set of commands against the current device config. If match is set to line, commands are matched line by line. If match is set to strict, command lines are matched with respect to position. If match is set to exact, command lines must be an equal match. Finally, if match is set to none, the module will not attempt to compare the source configuration with the running configuration on the remote device.

那可能是你的问题.

对于可能会影响模块工作方式的因素,我也有自己的见解,事实上,您正在使用after选项将更改应用于接口.它的实际用法是:

I also have a personal opinion as to what may be affecting the way the module works and is the fact that you are using the after option to apply the changes to the interfaces. Its actual usage is:

如果需要进行更改,则有序的命令集将追加到命令堆栈的末尾.就像之前一样,这允许剧本设计者在命令集之后附加一组要执行的命令.

The ordered set of commands to append to the end of the command stack if a change needs to be made. Just like with before this allows the playbook designer to append a set of commands to be executed after the command set.

连同before选项一起,它们用于在进行实际更改之前和之后应用命令.例如,在五分钟内设置一次重置以避免由于配置问题而导致断开连接,或者将更改写入ROM(尽管您可能通过save_when选项执行此操作).我没有证据表明是这种情况,但是您应该尝试一下.只需将after选项中的行移动到lines选项.

Along with the before option, they are used to apply commands before and after the actual changes are made. For example, setting a reset in five minutes to avoid a disconnection because of a configuration issue, or writing the changes to the ROM (though, you probably do this through the save_when option). I have no evidence that this is the case, but you should try it. Just move the lines in the after option to the lines option.

最后,您可以通过运行带有--check --diff标志的playbook来查看正在发生的变化.

Finally, you might see what is changing by running the playbook with the --check --diff flag.

希望对您有帮助.

这篇关于如何使ios_config任务幂等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆