ほげほげ

プログラミング、英会話、ヨガ、料理などの備忘録など。

ansible playbookの使い方 超基本

playbookというキーワードをしょっちゅう忘れてしまいます。 ansible-playbookというコマンドを使って、環境構築を行います。 実際のコマンドは以下のようになります。

$ ansible-playbook db.yml -i hosts

db.yml

chefでいうレシピのようなもので、作成したい環境を記述します。 こんな感じに。

- hosts: db-server
  become: yes
  tasks:
    - name: be sure mariadb-server is installed
      yum: name=mariadb-server state=installed
 
    - name: be sure mariadb is running and enabled
      service: name=mariadb state=running enabled=yes

    - name: be sure mariadb is installed
      yum: name=mariadb state=installed

    - name: SELinuxのDisable設定
      selinux: state=disabled

    - name: Firewalldの停止
      firewalld: state=disabled permanent=true

    - name: fuga
      service: name=firewalld state=stop enabled=yes

hosts

hostsは接続するサーバーの情報を記述したファイルです。 こんな感じ。

[db-server]
db1 ansible_port=2222 ansible_host=127.0.0.1 ansible_user=vagrant ansible_ssh_private_key_file=.vagrant/machines/default/virtualbox/private_key