LoginSignup
0
2

More than 5 years have passed since last update.

ansibleでnginxをinstallしてみる

Last updated at Posted at 2017-10-13

概要

ansibleを使ってnginxをinstallしてみました。

環境

ansible 2.4.0.0
CentOS release 6.9

ansibleの設定

ansible.cfgの一部の設定を変更しました。

/etc/ansible/ansible.cfg
inventory = /etc/ansible/hosts
remote_user = user_name

/etc/ansible/hosts
install先のserverを定義します。

[webserver]
web1

おすすめdirectoryを作る

ansible-galaxy init nginx

directoryの構造

.
├── nginx.yml
└── roles
    ├── common
    └── nginx
        ├── README.md
        ├── defaults
        │   └── main.yml
        ├── files
        ├── handlers
        │   └── main.yml
        ├── meta
        │   └── main.yml
        ├── tasks
        │   └── main.yml
        ├── templates
        ├── tests
        │   ├── inventory
        │   └── test.yml
        └── vars
            └── main.yml

mainとなるnginx.yml

nginx.yml
- name: nginx
  become: yes
  hosts: web1
  roles:
    - { role: nginx }

nginxを操作するtask

--extra-vars "op=xxxx"として引数で実行するtaskを変更する。
もっとよい方法はあるのだろうか。

roles/nginx/tasks/main.yml
---
# tasks file for nginx
- name: Install nginx
  yum: name=nginx
  when: op == "install"

- name: Start nginx
  service: name=nginx enabled=yes state=started
  when: op == "start"

- name: Stop nginx
  service: name=nginx state=stopped
  when: op == "stop"

- name: Restart nginx
  service: name=nginx state=restarted
  when: op == "restart"

- name: Reload nginx
  service: name=nginx state=reloaded
  when: op == "reload"

ansible-playbookの実行

ansible-playbook nginx.yml --extra-vars "op=start"

server側のsudoはpassword不要にしておきました。

0
2
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
0
2