0
0

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.

github codespaceでansible環境を作る

Posted at

はじめに

github codespaceを使ってansible playbookの動作検証ができないものかと思い
ansibleの実行環境を作ったときの手順を残す。

当初はテンプレート選択したらすぐに作れるかと思ったら、そんなテンプレートなかったのでやむなくイチから作ることにした。

手順

端的にまとめておきます

  1. Codespaceを起動し、ターミナルを開く。
  2. Ansibleをインストールするために、以下のコマンドを入力する。
sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
  1. バージョンを確認する
ansible --version

結果がこちら。簡単!
image.png

ちなみにバージョンを指定したい場合はこんな感じでコマンド入力

sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible-2.9
sudo apt install ansible=2.9.*

ansible playbookを使用する

テスト用のプレイブックはこんな感じで作りました。

playbook.yml
- name: Install and configure Apache
  hosts: your_server_hostname
  become: true

  tasks:
    - name: Install Apache
      package:
        name: apache2
        state: present

    - name: Configure Apache
      lineinfile:
        path: /etc/apache2/ports.conf
        regexp: '^Listen'
        line: 'Listen 80'

    - name: Restart Apache
      service:
        name: apache2
        state: restarted

github codespaceは複数サーバ作れなさそうなのでlocalhostで実行できるように
inventory.ymlを作成。
”ansible_connection: local”使ったらlocalhostに対しての指示出しができる。便利。

inventory.yml
all:
  hosts:
    localhost:
  vars:
    ansible_connection: local

実行する。以下のコマンドを入力

ansible-playbook playbook.yml -i inventory.yml

結果がこちら。動いた!
ばっちりapache2も動いてるし、curlで応答とれるし、いいですね!
image.png

おわりに

以上の手順はすべてChatGPTに教わりながら実施。
今までなら何時間も七転八倒しながらたどりつくのに今回はほぼ一直線にやりたいことができた。
トータル30分くらい?ChatGPTはマジで神ですね。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?