LoginSignup
1
0

More than 5 years have passed since last update.

シンプルにコマンド出力をとってくるAnsible Role(Windows版)

Last updated at Posted at 2016-10-29

その名の通り、ターゲットとなるサーバ上で実行した標準出力をリダイレクトしたファイルをとってくるRole "fetch-wcommand-out" を作ってみました。ターゲットサーバがWindows用のRoleです。
通常少なからずAnsibleのコードを書かなければなりませんが、取得に必要なファイルが決まってさえいれば直ぐに使えるようにしています。

fetch-wcommand-out Role

Ansible Galaxy に上げています。
https://galaxy.ansible.com/tksarah/fetch-wcommand-out/

準備

  • Ansible 2.X 以上の入ったホスト(v2.1.2でテスト済み)
  • とってきたいファイルのリスト
sample_comlist.txt
1,"Get-Date -displayhint tim","timeout.txt"
2,"$PSVersionTable","psversion.txt"

(注意)ダブルクォーテーションを使い、不要なスペースは削除

使い方例

対象となるサーバのインベントリファイルを用意します。

(参考)AnsibleでWindows Server 2012を構成する

Ansible Galaxy からロールをダウンロードします。

# ansible-galaxy install --roles-path ./roles tksarah.fetch-wcommand-out

プレイブックを用意します。(以下そのまま使っていただくと便利です。)

site.yml
- name: Playbook for fetching files
  hosts: "{{ target }}"
  gather_facts: no

  vars_prompt:
    - { name: "target" , prompt: "Input target host" ,  default: all , private: no }
    - { name: "inputcommand" , prompt: "Input your command list" , default: sample_comlist.txt , private: no }
    - { name: "remtemp" , prompt: "Input remote temp directory " , default: "C:\\ansible_remtemp" , private: no }
    - { name: "savedir" , prompt: "Input a save directory" , default: fetched , private: no }

  roles:
    - tksarah.fetch-wcommand-out

実行します。

# ansible-playbook -i hosts site.yml
Input target host [all]:(ターゲットホストを指定します。デフォルトはall)
Input your command list [sample_comlist.txt]:(取得したいコマンドと保存ファイル名が書かれているファイルを指定します。)
Input remote temp directory  [C:\\ansible_remtemp]:(ターゲットサーバ上で一時的に保存するディレクトリを指定します。)
Input a save directory [fetched]:(取得したファイルを保存するディレクトリを指定します。デフォルトはカレントのfetched)
PLAY [Playbook for fetching files] *********************************************
                           :
                           :
                           :
PLAY RECAP *********************************************************************
192.168.0.1            : ok=19   changed=6    unreachable=0    failed=0
192.168.0.2            : ok=17   changed=6    unreachable=0    failed=0

上記サンプルのプレイブックを使った時、とってきたファイルはカレントの「fetched」ディレクトリに格納されます。

# tree fetched
fetched
|-- 192.168.0.1
|   |-- C:
|   |   `-- ansible_rmtemp
|   |       |-- psversion.txt
|   |       |-- timeout.txt
|-- 192.168.0.2
|   |-- C:

以下も合わせて使うと効果的です。
シンプルにファイルをとってくるAnsible Role
シンプルにコマンド出力をとってくるAnsible Role(Unix/Linux版)

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