LoginSignup
3
3

More than 1 year has passed since last update.

Ansibleで複数のコマンド実行結果とノード毎にファイルを作る方法

Last updated at Posted at 2019-01-12

AnsibleでCatalystのconfig取ってみた(telnet版)」の続き記事です。

1. 複数のコマンド実行結果をファイル保存する方法

複数のコマンド実行結果を取得する方法です。
配列使ったプログラミングをやったことがある人なら容易に思い至る方法なので、この先は読む価値はありません。

さて、前のポストで、command:配下に入力するコマンドを入れてますが、

command

      command:
       - terminal length 0
       - enable
       - cisco
       - show run
      changed_when: False
      register: command_result

この4行の結果は、一つの変数名の中のそれぞれ違う領域に入ります。
(配列相当で、可変長なchar?)

なので、

Text出力

  - name: log export
    local_action:
      module: copy
      owner: hayashi
      group: hayashi
      mode: 0644
      dest: "/home/hayashi/show_run.log"
      content: "{{ command_result.output[3] }}"
    changed_when: False

content: "{{ command_result.output[3] }}"と書くと、4行目のshow runの結果が採れるわけです。

もうお分かりかと思いますが、

command

      command:
       - terminal length 0
       - enable
       - cisco
       - show run
       - show ver
      changed_when: False
      register: command_result

と5行目に足して、
別ファイルで出力する適当な- name:をコピペで足して、
[3]を[4]にインクリメントしてあげれば、

Text出力

  - name: show ver
    local_action:
      module: copy
      owner: hayashi
      group: hayashi
      mode: 0644
      dest: "/home/hayashi/show_ver.log"
      content: "{{ command_result.output[4] }}"
    changed_when: False

show verの結果がファイルに出力されます。

2.ノード毎にファイル保存する方法

次に、ノード毎にファイル名を作る方法ですが、inventory_hostnameという変数名を使います。

ノード名出力
      dest: "/home/hayashi/{{ inventory_hostname }}show_ver.log"

でも、hostsファイルはIPアドレスで作っていると後々困りそうなので、ノード名に変えていくつもりです。LocalDNS建てないと、、、正引きのファイルどうやって自動で作ろう、、、というのはAnsible外のお話ですね。

3
3
0

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
3
3