ansibleを使用してnginxやDockerを単純インストール


事前準備

事前にAnsibleをインストールしておく、ssh-keygen -t rsaでキーを作成してキーでログインできるようにする。

共通設定

接続などの共通設定をいかに示す

inventory.yml
web:
  hosts:
    hostname
  vars:
    domain: helloworld.com
    ansible_user: username
    ansible_ssh_private_key_file: ~/.ssh/id_rsa
    ansible_sudo_pass: test_password

nginxをインストールする場合

aptコマンドを使用して単純にインストール

nginx.yml
- hosts: web
  become: yes
  tasks:
  - name: "install nginx"
    apt:
      name: ['nginx']
      state: latest

実行結果メモ

$ ansible-playbook -i inventory.yml nginx.yaml 

PLAY [web] ********************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************
ok: [hostname]

TASK [install nginx] **********************************************************************************************************************************************
changed: [hostname]

PLAY RECAP ********************************************************************************************************************************************************
hostname     : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

nginxサーバが動いていることの単純な動作確認

$ curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

dockerの単純インストール

aptを使用して単純にインストールするだけの動作

docker.yaml
- hosts: web
  become: yes
  tasks:
  - name: "install docker.io"
    apt:
      name: ['docker.io']
      state: latest

実行結果

$ ansible-playbook -i inventory.yml docker.yaml 

PLAY [web] ********************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************
ok: [hostname]

TASK [install docker.io] ******************************************************************************************************************************************
changed: [hostname]

PLAY RECAP ********************************************************************************************************************************************************
hostname     : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

dockerが動いていることの確認

$ sudo docker ps
[sudo] username のパスワード: 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES