首页 > 解决方案 > Ansible playbook 可在缺少时在 IOS 设备上添加本地帐户

问题描述

我正在尝试为 Cisco IOS 设备创建 Ansible 剧本,以检查设备上是否存在本地用户帐户。如果它存在什么都不做,但如果它不存在,我想添加它。

以下是我所拥有的,但我需要条件检查方面的帮助,即检查寄存器值的最佳方法是什么,如果 rsitadmin 存在于其中,则不执行任何操作,否则将此命令发送到设备用户名 {{user}} secret {{经过}}。

如果缺少任务添加 RSITADMIN 不起作用,但这是我要检查的摘要。

我也使用 src 参数尝试了这个 jinja 模板,但没有帮助。

{% if 'rsitadmin' in ACCOUNT %}
{% else %}
 username {{user}} secret {{pass}}
{% endif %}
---
- name: Add RSITADMIN Account if missing
  hosts: LAB
  vars_files:
    - rsitadmin.yml
  gather_facts: false
  connection: local

  tasks:
    - name: Check if RSITADMIN account exist
      ios_command:
        commands: sh run | s username rsitadmin
      register: ACCOUNT

    - name: Print RSITADMIN account details
      debug:
          var=ACCOUNT.stdout

    - name: Add RSITADMIN if missing
      ios_config:
        lines: username {{user}} secret {{pass}}
      when: "'rsitadmin' in ACCOUNT"

标签: ansible

解决方案


这对我有用:

    - name: Configure RSITADMIN Account on devices its missing
      ios_config:
        lines:
          - username {{user}} secret {{pass}}
        save_when: changed
      when: ACCOUNT.stdout[0] is not search ("username rsitadmin")

推荐阅读