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


Apacheをインストール

```bash
vi playbook.yml
```

```yaml: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
```

```bash
ansible-playbook playbook.yml
```

libselinux-pythonをインストール

```bash
vi playbook.yml
```

```yaml: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

```

```bash
ansible-playbook playbook.yml
```

Apacheのindex.htmlを変更

```bash
vi index.html
```

```
<html>
  Hello ansible!
</html>

```


```bash
vi playbook.yml

```


```yaml: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

```

```bash
ansible-playbook playbook.yml
```

phpをインストール

```bash
vi playbook.yml

```

```yaml: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 
```

```bash
vi hello.php
```

```php:hello.php
<?php
  echo "ansible php";
```

```bash
ansible-playbook playbook.yml
```

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


MySQLをインストール

```bash
vi playbook.yml

```

```yaml: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
```

```bash
ansible-playbook playbook.yml
```

```bash
ssh db
```

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


```bash
mysql --version
```

```yaml: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
```

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

```bash
ssh db
```

```bash
mysql -udbuser -p mydb
```

[前回はこちら](https://qiita.com/joji/items/31b93404b699585164ff "前回はこちら")