1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Ansible×NW自動化】blockinfileモジュールで「ANSIBLE MANAGED BLOCK」を表示させない方法

Posted at

はじめに

<バージョン>
ansible 2.9.1

以下のようなplaybookでvMXからshowコマンドの結果を取得し、
blockinfileモジュールで結果をテキストに出力しようと思ったのですが、
出力結果に「ANSIBLE MANAGED BLOCK」という文字列が出てきてしまい、
個人的に納得いかない出力結果となりました。

blockinfile_test1.yml
---
- name: blockinfile TEST
  hosts: junos
  gather_facts: no
  vars:
    ansible_python_interpreter: /usr/bin/python3
    show_commands:
      - show version
      - show interfaces terse
  tasks:
    - name: junos show command
      junos_command:
        commands: "{{ item }}"
      register: junos_results
      with_items: "{{ show_commands }}"

    - name: save command
      blockinfile:
        block: "{{ item.stdout[0] }}"
        path: "result/{{ inventory_hostname }}.log"
        marker_begin: "=================={{ item.item }}=================="
        marker_end: ""
        create: yes
      with_items: "{{ junos_results.results }}"
      loop_control:
        label: "{{ item.item }}"

出力結果1

各ループの始まりと終わりに「ANSIBLE MANAGED BLOCK」という文字列が出てきてしまいます。

出力1
# ==================show version================== ANSIBLE MANAGED BLOCK
Hostname: ip-<addr>
Model: vmx
Junos: 18.4R1.8
~以下、略~
#  ANSIBLE MANAGED BLOCK
# ==================show interfaces terse================== ANSIBLE MANAGED BLOCK
Interface               Admin Link Proto    Local                 Remote
cbp0                    up    up
demux0                  up    up
~以下、略~
#  ANSIBLE MANAGED BLOCK

解決策

markerを空にして、blockの書き方を工夫します。

ポイント
(1)「marker: ""」 と書いて、「ANSIBLE MANAGED BLOCK」が出ないようにする
(2)block内で |(パイプ)を使うと、内容を複数行に渡って書くことが出来る

blockinfile_test2yml
---
- name: blockinfile TEST
  hosts: junos
  gather_facts: no
  vars:
    ansible_python_interpreter: /usr/bin/python3
    show_commands:
      - show version
      - show interfaces terse
  tasks:
    - name: junos show command
      junos_command:
        commands: "{{ item }}"
      register: junos_results
      with_items: "{{ show_commands }}"

    - name: save command
      blockinfile:
        block: |
               =================={{ item.item }}==================
               {{ item.stdout[0] }}
        path: "result/{{ inventory_hostname }}.log"
        marker: ""
        create: yes
      with_items: "{{ junos_results.results }}"
      loop_control:
        label: "{{ item.item }}"

出力結果2

余計なマーカーが入らず、スマートな出力になりました。

出力2
==================show version==================
Hostname: ip-<addr>
Model: vmx
Junos: 18.4R1.8
~以下、略~

==================show interfaces terse==================
Interface               Admin Link Proto    Local                 Remote
cbp0                    up    up
demux0                  up    up
~以下、略~

ちなみに

以下のようにmarkerを使うと、「ANSIBLE MANAGED BLOCK」は出てこないのですが、
各ループの前後にマーカーが記載されます。
自分としては、ループの始めだけにマーカーを出したかったです。

blockinfile_test3.yml
---
- name: blockinfile TEST
  hosts: junos
  gather_facts: no
  vars:
    ansible_python_interpreter: /usr/bin/python3
    show_commands:
      - show version
      - show interfaces terse
  tasks:
    - name: junos show command
      junos_command:
        commands: "{{ item }}"
      register: junos_results
      with_items: "{{ show_commands }}"

    - name: save command
      blockinfile:
        block: "{{ item.stdout[0] }}"
        path: "result/{{ inventory_hostname }}.log"
        marker: "=================={{ item.item }}=================="
        create: yes
      with_items: "{{ junos_results.results }}"
      loop_control:
        label: "{{ item.item }}"

出力結果3

各ループの前後にマーカーが記載されます。この出力方法が好みの方もいらっしゃると思います。

出力3
==================show version==================
Hostname: ip-<addr>
Model: vmx
Junos: 18.4R1.8
~以下、略~
==================show version==================
==================show interfaces terse==================
Interface               Admin Link Proto    Local                 Remote
cbp0                    up    up
demux0                  up    up
~以下、略~
==================show interfaces terse==================
1
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?