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?

Ansible学習メモ④(Container関連)

Posted at

業務で最近Ansibleを使い始めました。私自身はエンジニア職ではないので普段コーディングをほとんどしないです。少しAnsibleがわかるように学習し始めています。

1. AnsibleでLinux Centos環境にDockerインストール

こちらの三つのモジュールを使用
ansible.builtin.rpm_key
ansible.builtin.yum_repository
ansbile.builtin.yum

作成するマニフェストtest.yamlは以下のようです。

---
- name: install docker to redhat like system
  hosts: all
  become: true
  tasks:
    - name: add rpm_key
      ansible.builtin.rpm_key:
        key: "https://download.docker.com/linux/rhel/gpg"
        state: present

    - name: Add repository
      ansible.builtin.yum_repository:
        name: docker
        description: docker repository
        baseurl: "https://download.docker.com/linux/rhel/$releasever/$basearch/stable"
        enabled: true
        gpgcheck: true
        gpgkey: "https://download.docker.com/linux/rhel/gpg"

    - name: install Docker
      ansible.builtin.yum:
        name:
          - docker-ce
          - docker-ce-cli
          - containerd.io
        state: latest
        update_cache: true

    - name: Start Docker
      ansible.builtin.service:
        name: "docker"
        enabled: true
        state: started

inventoryファイルを以下のように設定

XX.XX.XX.XX ansible_ssh_private_key_file=~/XXXXX.pem

(XX.XX.XX.XX: 対象マシンのIPアドレスまたはホスト名)

以下のコマンドを実行し、対象マシンにパッケージインストールを行う。

ansible-playbook -i inventory test.yaml

2. Apache webサーバをコンテナ(Podman)としてデプロイする

※Podmanは、Dockerの代替として、Red Hat Enterprise Linux 8以降で導入され、現在RHELシステムではPodmanの使用を推奨
そのため、本タスクではDockerの代わりにPodmanを使用。

本タスクでは以下のモジュールを使用する。
ansible.builtin.yum
containers.podman.podman_image (イメージをプール)
containers.podman.podman_container (コンテナを実行)

まず、コレクションのインストール必要

ansible-galaxy collection install containers.podman

作成するマニフェストtest.yamlは以下のようです。

---
- name: Podman container demo
  hosts: all
  become: true
  tasks:
    - name: install podman
      ansible.builtin.yum:
        name: podman
        state: latest

    - name: pull image
      containers.podman.podman_image:
        name: httpd
        pull: true

    - name: run podman container
      containers.podman.podman_container:
        name: webserver
        image: httpd
        state: started
        detach: true
        expose:
          - 80
        ports:
          - 8080:80

inventoryファイルを以下のように設定

XX.XX.XX.XX ansible_ssh_private_key_file=~/XXXXX.pem

(XX.XX.XX.XX: 対象マシンのIPアドレスまたはホスト名)

以下のコマンドを実行し、対象マシンにパッケージインストールを行う。

ansible-playbook -i inventory test.yaml
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?