LoginSignup
12
10

More than 5 years have passed since last update.

Ansibleの勉強と実行環境の作成方法2

Last updated at Posted at 2015-12-27

Apacheをインストール

vi playbook.yml
playbook.yml
---
- hosts: all
  sudo: yes
  tasks:
    - name: add a new user
      user: name=ansible_test state=absent

- hosts: web
  sudo: yes
  tasks:
    - name: install apache
      yum: name=httpd state=latest
    - name: start apace and enabled
      service: name=httpd state=started enabled=yes
ansible-playbook playbook.yml

libselinux-pythonをインストール

vi playbook.yml
playbook.yml
---
- hosts: all
  sudo: yes
  tasks:
    - name: add a new user
      user: name=ansible_test state=absent
    - 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 apace and enabled
      service: name=httpd state=started enabled=yes

ansible-playbook playbook.yml

Apacheのindex.htmlを変更

vi index.html
<html>
  Hello ansible!
</html>

vi playbook.yml

playbook.yml
---
- hosts: all
  sudo: yes
  tasks:
    - name: add a new user
      user: name=ansible_test state=absent
    - 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

ansible-playbook playbook.yml

phpをインストール

vi playbook.yml

playbook.yml
---
- hosts: all
  sudo: yes
  tasks:
    - name: add a new user
      user: name=ansible_test state=absent
    - 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 hellp.php
      copy: src=./hello.php dest=/var/www/html/hello.php owner=vagrant
  handlers:
    - name: restart apache
      service: name=httpd state=restarted 
vi hello.php
hello.php
<?php
  echo "ansible php";
ansible-playbook playbook.yml

確認は、このアドレスからできます。
192.168.43.52/hello.php

MySQLをインストール

vi playbook.yml

playbook.yml
---
- hosts: all
  sudo: yes
  tasks:
    - name: add a new user
      user: name=ansible_test state=absent
    - 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 hellp.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=mysql-server state=latest
 - name: start mysql and enabled
 service: name=mysqld state=started enabled=yes
ansible-playbook playbook.yml
ssh db

MySQLのユーザーとデータベースを作成

mysql --version
playbook.yml
---
- hosts: all
  sudo: yes
  tasks:
    - name: add a new user
      user: name=ansible_test state=absent
    - 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 hellp.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 user for mydb
   mysql_user: name=dbuser password=dbpassword priv=mydb.*:aLL state=present

ユーザーとデータベースができたことを確認する

ssh db
mysql -udbuser -p mydb

前回はこちら

12
10
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
12
10