LoginSignup
0
2

More than 5 years have passed since last update.

Ansibleで複数のCisco機器に同じコマンドを打つ

Last updated at Posted at 2016-10-19

多くのCisco機器で同じコマンド(主にshow 系)を実行したくなったので
Ansibleで実行させてみる。

shell でやるときとの大きな違いは
Inventoryをうまく書くと、実行先を柔軟に選択できるというところ。
hostsファイルをたくさん抱えるよりはinventoryファイルのほうが修正するときのことを考えると良いかと。
Inventoryファイルのアクセス権には注意するか、credentialな情報を含めるのをやめましょう。

作ったファイル

playbook

one.yml

 - hosts: "{{ host }}"
   gather_facts: no
   connection: local

   tasks:
     - name: one-shot
       register: result
       ios_command:
          provider: "{{ cli }}"
          commands: "{{ command }}"

     - debug: var=result.stdout_lines
       when: result|success

   vars:
     cli:
       host: "{{ inventory_hostname }}"
       username: "{{ ansible_user }}"
       password: "{{ ansible_password }}"
       authorize: true
       auth_pass: "{{ ansible_net_auth }}"

inventory file

hosts

[group1]
host1
host2

[group2]
host3
host4

[all:children]
group1
group2

[all:vars]
ansible_user=login-username
ansible_password=login-password
ansible_net_auth=enable-password

shell

one.sh

#!/bin/bash
if [ $# -lt 2 ]; then
    echo "usage $0 hostgroup command [command]..."
    exit 1
fi

host="$1"
shift

cmd="["
for c in "$@"; do
    cmd="${cmd}'${c}',"
done
cmd="${cmd%,}]"

ansible-playbook -i $(cd $(dirname $0) && pwd)/hosts $(cd $(dirname $0) && pwd)/one.yml -e "{\"command\":\"${cmd}\",\"host\":\"${host}\"}"

使い方

./one.sh "host group name" "some command1" "some command2" "some command3" ...

もしくは

ansible-playbook -i hosts one.yml -e '{"host":"host group name","command":"some command"}'

ansible-playbook -i hosts one.yml -e '{"host":"host group name","command":"['some command','some command2','some command3'...]"}'

0
2
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
0
2