首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析ansible playbook输出

解析ansible playbook输出
EN

Stack Overflow用户
提问于 2020-07-05 00:56:56
回答 1查看 538关注 0票数 0

我的策略如下

代码语言:javascript
复制
  - name: Juniper SRX Compliance checks
    hosts: juniper
    gather_facts: false
    tasks:
    - name: Syslog server check
      junos_config:
         src: /home/gefelas/ansible_junos/files/syslog_config.txt
      diff: true
      register: junos_output
    - debug:
        msg: Syslog server check - This check has failed with the following output({{ junos_output.diff.prepared }})
       when: junos_output.changed
    - debug:
        msg: Syslog server check - This check has passed with the following output({{ junos_output.diff.prepared }})
      when: not junos_output.changed

它产生输出(在ansible.cfg中使用stdout_callback = community.general.yaml )

代码语言:javascript
复制
msg: |-
 Syslog server check - This check has failed with the following output([edit system syslog host 192.168.100.70]
+     interactive-commands any;
[edit system syslog host 192.168.100.70]
+    facility-override local1;
+    log-prefix firewall;
+    source-address 172.16.203.121;
+    explicit-priority;
[edit system syslog]
+    file messages {
+        any critical;
+        authorization info;
+    }
+    file default-log-messages {
+        structured-data;
+    }
+    file sessions {
+        user info;
+    }
+    file interactive-commands {
+        interactive-commands error;
+    })

哪个regex模块适合生成以下输出

代码语言:javascript
复制
msg: |-
   Syslog server check - This check has failed with the following output

    set system syslog archive size 300000
    set system syslog archive files 3
    set system syslog archive world-readable
    set system syslog user * any emergency
    set system syslog host 192.168.100.70 any any
    set system syslog host 192.168.100.70 interactive-commands any
    set system syslog host 192.168.100.70 facility-override local1
    set system syslog host 192.168.100.70 log-prefix firewall
    set system syslog host 192.168.100.70 source-address "172.16.203.121"
    set system syslog host 192.168.100.70 explicit-priority
    set system syslog file messages any critical
    set system syslog file messages authorization info
    set system syslog file default-log-messages structured-data
    set system syslog file sessions user info
    set system syslog file interactive-commands interactive-commands error

在剧本中添加一些类似的东西会有什么不同吗?

代码语言:javascript
复制
  - set_fact:  
     junos_output: |  
      {{ junos_output |  
      map('regex_replace','.*\\s+( )\\s+.*','\\g<ip>') |   
  list }}   
EN

回答 1

Stack Overflow用户

发布于 2020-07-06 23:10:35

我会给你我最好的技巧,在Ansible中以明文打印路由器的“set”内容。它是肮脏的,它打破了抽象,但它如此方便,以至于我仍然喜欢它。

代码语言:javascript
复制
- name: Print configuration
  delegate_to: localhost
  shell: "echo '{{ srx_config|join(cr) }}' > /dev/tty"
  when: ansible_verbosity > 0
  changed_when: false
  vars:
    cr: '{{ "\n" }}'

这里的主要技巧是使用/dev/tty进行输出。如果你运行带有-v选项的playbook,你会在屏幕上的plantext中得到srx_config中的所有行,没有引号,等等。所有你要做的就是调试它,只需登录到控制台并将它们粘贴到“配置”模式。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62731942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档