2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Ansibleでnginxを起動

Last updated at Posted at 2023-02-06

はじめに

レポジトリーはこちらです。

今回は、controllerにAnsibleをインストールし、各serverで実行します。

Ubuntsuの設定

/etc/hostsに以下を追加します。

/etc/hosts
192.168.56.8    server1
192.168.56.9    server2

Ansibleの設定

UbuntsuへのAnsibleのインストール

Ansibleのインストールします。

sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible

Ansibleのserviceを設定ファイルに追記する

/etc/ansible/hosts
[group]
server1
server2

SSH接続エラーの回避設定

/etc/ansible/ansible.cfg
[defaults]
host_key_checking = false

そして、ssh keyを作成後、各サーバーに配ります。

疎通の確認する

pingをしてみます。
kはパスワード認証の要求、mはコマンドの実行です。

ansible group -k -m ping

Screenshot 2023-02-06 at 15.08.24.png

ansible-playbookを実行します

playbookのsyntax-check後、問題がなければ実行します。

ansible-playbook hello.yml --syntax-check
ansible-playbook hello.yml -k
hello.yml
---
- hosts: group
  tasks:
    - name: "first ansible"
      debug: msg="Hello world!!!!!"

Screenshot 2023-02-06 at 15.11.00.png

Nginxのダウンロードと起動

Nginxのダウンロード方法を確認

Ubuntsuへのダウンロードを確認し、playbookに書きます。

main.yml
---
- hosts: group
  become: yes
  tasks:
    - name: "install nginx"
      ansible.builtin.apt:
        name: nginx
        state: latest
        cache_valid_time: 86400
        update_cache: yes
    - name: "start nginx"
      service:
        name: nginx
        state: started

paybookの構文をチェック

paybookの構文をチェックします。

ansible-playbook main.yml --syntax-check

Screenshot 2023-02-06 at 12.34.00.png

paybookを実行

ansible-playbook main.yml -k --ask-become-pass

--ask-become-passはssh先のsudoコマンドで求められるパスワード認証を要求させます。

Screenshot 2023-02-06 at 12.32.07.png

結果

下記にアクセスします。

http://192.168.56.8:80
http://192.168.56.9:80

Screenshot 2023-02-06 at 12.29.14.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?