zoukankan      html  css  js  c++  java
  • ansible模块介绍之ios_command

    一、模块简介

    ios_command此模块将任意命令发送到ios节点并返回设备读取的结果
    此模块不支持在配置模式下使用,即只支持在用户模式>和特权模式#下使用

     官方文档地址:https://docs.ansible.com/ansible/latest/modules/ios_command_module.html#ios-command-module

    二、模块参数

    auth_pass   #进入特权模式使用的密码,如果authorize=no,则不检索此特权密码;
                #如果authorize=yes,而auth_pass不填,则使用环境变量ANSIBLE_NET_AUTH_PASS的值作为特权密码进行验证
    authorize   #no or yes,是否进去特权模式,默认使用环境变量ANSIBLE_NET_AUTHORIZE的值,默认在用户模式下
    command     #【必填项】需要发送到远端执行的命令列表,如果遇到需要回答提示,则在命令后使用参数prompt:提示关键字,使用参数answer:'y'回答或者answer:'
    ',或者answer:'n'
    interval    #重试之间的等待间隔时间,默认是1s
    wait_for    #在继续前进之前,任务会等待条件成立,如果不成立,则任务失败。
    match       #与wait_for配合使用,默认是all,可选参数all或any,match=all表示必须满足wait_for里的所有条件;match=any表示只要满足wait_for里的任一条件即可
    retries     #重试次数,默认10次
    provider    #包含如下dict对象
        auth_pass   #进入特权模式的密码,与上面auth_pass效果一样
        authorize   #no or yes,默认no,与上面authorize效果一样
        host        #【必填项】填写域名或者ip地址,需要连接的设备地址,{{ansible_host}}代表遍历/etc/ansible/hosts文件里每行中带有ansible_host的主机
        username    #远程登录用户名,默认使用环境变量ANSIBLE_NET_USERNAME
        password    #远程登录密码,默认使用环境变量ANSIBLE_NET_PASSWORD
        port        #默认22端口
        ssh_keyfile #ssh密钥文件位置,默认使用环境变量ANSIBLE_NET_SSH_KEYFILE
        timeout     #超时时间,默认10s

    三、模块使用注意事项

       暂无

      
    四、模块使用实例

      4.1 查看ios组里的主机的版本号

        a.编辑/etc/ansible/hosts文件,新增如下内容

    [ios]
    r5 ansible_host=192.168.xx.45
    r6 ansible_host=192.168.xx.46
    r7 ansible_host=192.168.xx.47
    r8 ansible_host=192.168.xx.48

        b.编写ansible playbook剧本nano ios_example01.yaml,严格按照如下格式

    ---
    - name: ios command module example
      hosts: ios
      connection: local
      gather_facts: no
    
      tasks:
        - name: check ios version
          ios_command:
            commands: show version
            provider:
              username: cisco
              authorize: yes
              auth_pass: cisco
              host: "{{ansible_host}}"
              password: cisco
          register: show_version_output
    
        - name: print the show version
          debug:
            msg: "{{show_version_output.stdout_lines}}"

        c.如果第一次登录设备,需要修改nano /etc/ansible/ansible.cfg文件里的host_key_checking = False,即关闭主机密钥检测

        d.执行剧本:ansible-playbook iso_example01.yaml,如下部分截图

       4.2 清除ios组里的所有主机的ether 0/0接口信息计数,并自动根据提示,回复y执行

        步骤与3.1的例子一模一样,差别在于b点的playbook剧本内容,剧本内容如下:

    ---
      - name: ios command module example
        hosts: ios
        connection: local
        gather_facts: no
    
        tasks:
          - name: run show version and check to see if output contains IOS
            ios_command:
              commands:
                - command: 'clear counters Ethernet0/0 '
                  prompt: 'Clear "show interface" counters on this interface [confirm]'
                  answer: 'y'
                - command: 'clear counters Ethernet0/1'
                  prompt: '[confirm]'
                  answer: "
    "
              
              provider:
                host: "{{ansible_host}}"
                username: cisco
                password: cisco
                authorize: yes
                auth_pass: cisco
            register: show_version_output
    
          - name: print the show version
            debug:
              msg: "{{show_version_output.stdout_lines}}"

     五、模块返回内容字段介绍

    failed_conditions   #失败的条件列表,返回列表形式
    stdout              #远程执行命令的返回结果,返回列表形式
    stdout_lines        #多条远程执行命令,将每个命令的返回结果列表1在一一存入列表2中
  • 相关阅读:
    【前端面试】前端知识点总结_前端大厂车轮战_转自mahmud(小麦)大神
    【网优】浅谈LTE无线网络优化
    Java单体应用
    Java单体应用
    Java单体应用
    Java单体应用
    Java单体应用
    Java单体应用
    Java单体应用
    Java单体应用
  • 原文地址:https://www.cnblogs.com/lisenlin/p/10860581.html
Copyright © 2011-2022 走看看