【问题标题】:How to extract the output from stdout.lines in ansible如何在ansible中从stdout.lines中提取输出
【发布时间】:2019-07-26 05:58:59
【问题描述】:
---
- name: Mikrotik info
  hosts: mikrotik
  connection: network_cli
  remote_user: root
  gather_facts: false
  tasks:
  - name: show info
    routeros_command:
     commands: /system routerboard print
    register: rb_info

  - name: Debug info
    debug:
      msg: "{{ rb_info.stdout_lines }}"

输出:

 routerboard: yes
             model: 751G-2HnD
     serial-number: 3A6502B2A2E7
     firmware-type: ar7240
  factory-firmware: 3.0
  current-firmware: 6.42.3
  upgrade-firmware: 6.43.4

我需要将其过滤为“升级固件”字符串并获得如下输出:

upgrade-firmware: 6.43.4

我应该使用 regex_replace 吗?或者我可以使用 grep 或类似的东西?

非常感谢任何想法。

谢谢

【问题讨论】:

    标签: ansible mikrotik ansible-template


    【解决方案1】:

    (更新)

    使用 from_yamlcombine 字典。例如

        - set_fact:
            minfo: "{{ minfo|default({})|combine(item|from_yaml) }}"
          loop: "{{ rb_info.stdout_lines }}"
        - debug:
            var: minfo['upgrade-firmware']
    

      minfo['upgrade-firmware']: 6.43.4
    

    (备案)

    1. 稳健的解决方案是将数据写入 templateinclude_vars。以下任务
        - tempfile:
          register: tempfile
        - template:
            src: minfo.j2
            dest: "{{ tempfile.path }}"
        - include_vars:
            file: "{{ tempfile.path }}"
            name: minfo
        - debug:
            var: minfo
    

    使用模板

        shell> cat minfo.j2 
        {% for item in rb_info.stdout_lines %}
        {{ item }}
        {% endfor %}
    

    应该给

        "minfo": {
            "current-firmware": "6.42.3", 
            "factory-firmware": 3.0, 
            "firmware-type": "ar7240", 
            "model": "751G-2HnD", 
            "routerboard": true, 
            "serial-number": "3A6502B2A2E7", 
            "upgrade-firmware": "6.43.4"
        }
    
    1. 以下任务创建变量upgrade_firmware
        - set_fact:
            upgrade_firmware: "{{ item.split(':').1|trim }}"
          loop: "{{ rb_info.stdout_lines|map('quote')|map('trim')|list }}"
          when: item is search('^upgrade-firmware')
        - debug:
            var: upgrade_firmware
    
    1. 可以将所有参数放入字典中
        - set_fact:
            minfo: "{{ minfo|default({})|
                       combine({item.split(':').0: item.split(':').1|trim}) }}"
          loop: "{{ rb_info.stdout_lines|map('quote')|map('trim')|list }}"
        - debug:
            var: minfo['upgrade-firmware']
    

    【讨论】:

    • 谢谢!但我有一个错误fatal: [10.1.1.1]: FAILED! => {"msg": "The conditional check 'item is search('^upgrade-firmware')' failed. The error was: Unexpected templating type error occurred on ({% if item is search('^upgrade-firmware') %} True {% else %} False {% endif %}): expected string or buffer\n\nThe error appears to be in '/home/user/scripts/mikrotik.update/mikrotik-info.yml': line 12, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n register: rb_info\n - set_fact:\n ^ here\n"}
    • 试试item|quote is search('^upgrade-firmware')。你必须测试它。抱歉,我没有 rb_info。
    • 是的,我变了。现在输出是:TASK [set_fact] * skipping: [10.1.1.1] => (item=[u'routerboard: yes', u' model: 750', u' serial-number: 3B0202EEEFB4', u' firmware-type: ar7240', u' factory-firmware: 2.38', u' current-firmware: 3.19', u' upgrade-firmware: 6.43.4']) TASK [debug] ***********************************************ok: [10.1.1.1] => { "upgrade_firmware": "VARIABLE IS NOT DEFINED!" } 我应该在另一个游戏中定义变量吗?是否可以隐藏“TASK [set_fact] 部分?
    • 问题是字符串开头的多余空格。添加过滤器map('trim')。查看更新的答案。
    猜你喜欢
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多