:>/dev/null

ラガードエンジニアの不撓不屈の精神/unlearning/go beyond

ホスト側からリモートコマンド実行

ansibleのplaybookを使用し、ホスト側から配下内に複数クライアントへ特定コマンドを実行する
* 下記サンプルはmysqlのerror,slowに一括でコマンド実行しログ出力させる。
* td-agent等使用し、ログ収集行う場合のテスト出力で便利

# cat hosts
[hostlist]
hosta ansible_host=172.27.1.1
hostb ansible_host=172.27.1.2
hostc ansible_host=172.27.1.3

# cat test.yml

---
- hosts: hostlist
  become: yes
  become_method: su
  become_user: root
  roles:
     - td-agent
  tasks:

    - name: check mysql errlog
      shell: echo "2018-04-05T01:10141.161639+09:00 0 [Warning] "tables_priv" entry "user mysql.session@localhost" ignored in --skip-name-resolve mode." >> /data/mysql/{{ inventory_hostname }}.errlog

    - name: check mysql slow
      shell: mysql -u[username] -p[passwd] -e "select sleep(2);"

■ 実行
* -C:テスト実行
* -l:hosts内の特定ホストのみ実行

# ansible-playbook -i hosts -u [username] -l hosta,hostb --ask-pass --ask-become-pass test.yml --diff -C