LoginSignup
5
5

More than 5 years have passed since last update.

Ansibleを使ってサーバごとに設定をしたい場合

Last updated at Posted at 2015-02-28

Ansible、いいですよね、楽しいですね。

ただし、今の環境にそぐわないデザインであり、環境を変えることが不可能なので
Ansibleの冪等性を守るっていうのを破る?感じで使っています。

今の環境では、Ansibleを使ってシェルスクリプトを各サーバにぶん投げる使い方をしています。
例えば、シェルスクリプトがサーバごとに用意されてるでとかの場合は
バカみたいな書き方ですが、下記方法で出来ました。

本当は、varsにちゃんとした変数を用意してあげる必要があると想いますが
用意するのが面倒だったので、ちゃちゃっと代用するという邪な考え。

P.S. 手元にコードがないので記憶で書いています・・・

前提:
シェルスクリプトは、ホスト名で用意されている。

hosts.yml
[sample]
sample01.sampledomain.com ansible_ssh_host=192.168.1.1
sample02.sampledomain.com ansible_ssh_host=192.168.1.2
Sample.yml

- hosts: sample
  sudo: yes
  gather_facts: false

  tasks:
   - name: Transfer file for each server
     file: src=/tmp/{{ inventory_hostname }} dest=/tmp/{{ inventory_hostname }}
     when: inventory_hostname == inventory_hostname

シェルスクリプト投げるだけなら違うツールでもいいじゃないか!というお怒りはごもっとも。

なお、こんな使い方をしてます。
シェルスクリプトには引数で対象ホスト名を渡し
それによって処理を変えさせています。

Sample.yml

- hosts: sample
  sudo: yes
  gather_facts: false

  tasks:
   - name: Transfer file for each server
     file: src=/tmp/ShellScript.sh dest=/tmp/ShellScript.sh
     when: inventory_hostname == inventory_hostname

   - name: Run The ShellScript
     shell: sudo chmod +x /tmp/ShellScript.sh; bash /tmp/ShellScript.sh {{ inventory_hostname }}

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