ドットインストールさん、お世話なってます。
https://dotinstall.com/lessons/basic_ansible
習ったことのメモ。
ドットインストール Ansible 入門
Ansibleの良いところは、管理するサーバにansibleをいれて、 操作したいサーバへ公開鍵認証してしまえばチャチャっと行えること。
Inventory ファイルの作成
処理対象となるホストを記述するグループ分けすることが可能
[web] 192.168.43.52 [db] 192.168.43.53
コマンド 対象ホストにpingを飛ばす
ansible all -i hosts -m ping
・オプション
-i → Inventory ファイルの指定
-m → モジュール指定
Inventory ファイルの指定は省略可能
ansible.config にてファイルを指定
vi ansible.cfg ----- 編集内容 [defaults] hostfile = ./hosts ----- 編集終了後は以下でおけ ansible all -m ping
Playbook の活用
サーバのあるべき状態を記述したもの
→ 冪等性(ある操作を1回行っても複数回行っても結果が同じであること)
vi playbook.yml ---編集内容--- - hosts: all sudo: yes #sudo権限が必要な場合にyes tasks: - name: add a new user user: name=murajun
実行コマンド
ansible-playbook playbook.yml
absent
ユーザがいない(いる場合には削除がされる)
--- - hosts: all sudo: yes tasks: - name: add a new user user: name=murajun state=absent
文法チェック
ansible-playbook playbook.yml --syntax-check
タスク確認
ansible-playbook playbook.yml --list-task
テスト実行
ansible-playbook playbook.yml --check
変数
ユーザ名を変数で管理する
--- - hosts: all sudo: yes vars_prompt: username: "Enter username" tasks: - name: add a new user user: name={{username}}
MWの設定
web サーバに対してのみ行うので、新しいセクションを作成する。 yumモジュールにて、名前とlatest(最新版)を指定。 さらにサービスモジュールで起動と自動起動設定をする。
--- - hosts: all sudo: yes tasks: - name: add a new user user: name=murajun state=absent - hosts: web sudo: yes tasks: - name: install apache yum: name=httpd state=latest - name: start apache and enabled service: name=httpd state=started enabled=yes
htmlファイルを転送する
htmlファイル内容
<html> hello from ansible! </html>
playbookの内容変更
--- - hosts: all sudo: yes tasks: - name: add a new user user: name=taguchi - name: install libselinux-python yum: name=libselinux-python state=latest - hosts: web sudo: yes tasks: - name: install apache yum: name=httpd state=latest - name: start apache and enabled service: name=httpd state=started enabled=yes - name: change owner file: dest=/var/www/html owner=vagrant recurse=yes - name: copy index.html copy: src=./index.html dest=/var/www/html/index.html owner=vagrant
recurse=yes
src
→コピー元
dest
→コピー先
インストールされていないとのエラー
~省略~
(libselinux-python) aren't installed!
なので、playbookに以下内容を追記している
- hosts: all sudo: yes tasks: - name: add a new user user: name=murajun - name: install libselinux-python yum: name=libselinux-python state=latest
変数を使ってインストールをする
「yum: name={{item}} state=latest」と書いて、 その下に「with_items:」というのを作って、 その下に item にインストールするパッケージをリストとして作成
- name: install php packages yum: name={{item}} state=latest with_items: - php - php-devel - php-mbstring - php-mysql
インストール後の再起動
notify: - restart apache handlers: - name: restart apache service: name=httpd state=restarted
※handlers は最後に1回だけ呼ばれる。 複数箇所からrestart apacheを呼ばれても、 handlers が呼ばれるのは最後の1回のみ。
PHPを動作させる
copyモジュールを使って、hello.phpを作ってweb側に転送する。
- name: copy index.html copy: src=./index.html dest=/var/www/html/index.html owner=vagrant
hello.php ファイル内容
<?php echo "hello from PHP!";
作ったplaybookを動かして結果を確認する。
MySQLの導入
playbookにDBサーバ用のセクションを作る。
- host: db sudo: yes tasks: - name: install mysql yum: name=mysql-server state=latest - name: start mysql and enabled service: name=mysqld state=started enabled=yes
MysqlでDBとユーザ作成
- name: create a database mysql_db: name=mydb state=present - name: create a user for mydb mysql_user: name=dbuser password=dbpassword priv=mydb.*:ALL state=present
python mysqldb がないとのエラーが発生。 playbookにインストールを加える。
- name: install mysql yum: name={{item}} state=latest with_items: - mysql-server - MySQL-python
playbook.ymlの内容まとめ
--- - hosts: all sudo: yes tasks: - name: add a new user user: name=taguchi - name: install libselinux-python yum: name=libselinux-python state=latest - hosts: web sudo: yes tasks: - name: install apache yum: name=httpd state=latest - name: start apache and enabled service: name=httpd state=started enabled=yes - name: change owner file: dest=/var/www/html owner=vagrant recurse=yes - name: copy index.html copy: src=./index.html dest=/var/www/html/index.html owner=vagrant - name: install php packages yum: name={{item}} state=latest with_items: - php - php-devel - php-mbstring - php-mysql notify: - restart apache - name: copy hello.php copy: src=./hello.php dest=/var/www/html/hello.php owner=vagrant handlers: - name: restart apache service: name=httpd state=restarted - hosts: db sudo: yes tasks: - name: install mysql yum: name={{item}} state=latest with_items: - mysql-server - MySQL-python - name: start mysql and enabled service: name=mysqld state=started enabled=yes - name: create a database mysql_db: name=mydb state=present - name: create a user for mydb mysql_user: name=dbuser password=dbpassword priv=mydb.*:ALL state=present