Install
[centos@haas ~]$ sudo install setuptools
[centos@haas ~]$ sudo install ansible-container
[centos@haas ~]$ ansible-container init
Ansible Container initialized.
作る
主に、2つのファイル編集。
- container.yml
- main.yml
つまり、「Apache httpdが動くコンテナがほしいな」というとき、コンテナのベースイメージや、コマンド、ポート、リンク、エクスポーズなんかはcontainer.yml
で、コンテナ内で動かすhttpdインストールして、設定して・・・なんかの情報はmain.yml
に書く。main.yml
の方は、まんまAnsibleのYAMLを書けばいい(もちろんコンテナ内で動くもの処理できるものが前提となる)。
container.yml
コンテナをビルドする際の情報、Docker Compose ライクに YAML で書く。
version: "1"
services:
hands_temp:
image: centos:latest
command: ["/usr/sbin/sshd","-D"]
# Add your containers here, specifying the base image you want to build from
# For example:
#
# web:
# image: ubuntu:trusty
# ports:
# - "80:80"
# command: ['/usr/bin/dumb-init', '/usr/sbin/apache2ctl', '-D', 'FOREGROUND']
# dev_overrides:
# environment:
# - "DEBUG=1"
#
registries: {}
# Add optional registries used for deployment. For example:
# google:
# url: https://gcr.io
# namespace: my-cool-project-xxxxxx
main.yml
container.yml
でビルドするコンテナをビルドするための中身の処理の情報
# This should be your Ansible playbooks to provision your containers.
# An inventory will be automatically created using the names of the services
# from your container.yml file.
# Add any roles or other modules you'll need to this directory too.
# For many examples of roles, check out Ansible Galaxy: https://galaxy.ansible.com/
#
---
- hosts: all
gather_facts: false
vars:
hands_name: "handsonname"
tasks:
- name: Only say hello when running via Ansible Container
command: echo "Hello!"
when: ansible_env.ANSIBLE_CONTAINER is defined
- block:
- name: Install openssh-server and clients
yum: name={{ item }} state=present
with_items:
- "openssh-server"
- "openssh-clients"
- name: Generate Host RSA Key
raw: "ssh-keygen -q -t rsa1 -f /etc/ssh/ssh_host_rsa_key -C '' -N ''"
- name: Generate Host ECDSA Key
raw: "ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -C '' -N ''"
- name: Generate Host ED25519 Key
raw: "ssh-keygen -q -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -C '' -N ''"
- name: Add PS
blockinfile:
dest: /root/.bashrc
block: |
export PS1="[\u@{{ hands_name }} \W] $ "
cd ~/
ビルド
[centos@haas ~]$ ansible-container build
No DOCKER_HOST environment variable found. Assuming UNIX socket at /var/run/docker.sock
Starting Docker Compose engine to build your images...
Attaching to ansible_ansible-container_1
Cleaning up Ansible Container builder...
No image found for tag centos-hands_temp:latest, so building from scratch
Attaching to ansible_ansible-container_1, ansible_hands_temp_1
ansible-container_1 | Host hands_temp running
ansible-container_1 |
ansible-container_1 | PLAY [all] *********************************************************************
ansible-container_1 |
ansible-container_1 | TASK [Only say hello when running via Ansible Container] ***********************
ansible-container_1 | skipping: [hands_temp]
ansible-container_1 |
ansible-container_1 | TASK [Install openssh-server and clients] **************************************
ansible-container_1 | changed: [hands_temp] => (item=[u'openssh-server', u'openssh-clients'])
ansible-container_1 |
ansible-container_1 | TASK [Generate Host RSA Key] ***************************************************
ansible-container_1 | ok: [hands_temp]
ansible-container_1 |
ansible-container_1 | TASK [Generate Host ECDSA Key] *************************************************
ansible-container_1 | ok: [hands_temp]
ansible-container_1 |
ansible-container_1 | TASK [Generate Host ED25519 Key] ***********************************************
ansible-container_1 | ok: [hands_temp]
ansible-container_1 |
ansible-container_1 | TASK [Add PS] ******************************************************************
ansible-container_1 | changed: [hands_temp]
ansible-container_1 |
ansible-container_1 | PLAY RECAP *********************************************************************
ansible-container_1 | hands_temp : ok=5 changed=2 unreachable=0 failed=0
ansible-container_1 |
ansible_ansible-container_1 exited with code 0
Aborting on container exit...
Stopping ansible_hands_temp_1 ... done
Exporting built containers as images...
Committing image...
Exported centos-hands_temp with image ID sha256:85e018efb704ce2970871a80b7a1fc0377e2d4f869ed134268b2e16f68b8317b
Cleaning up hands_temp build container...
Cleaning up Ansible Container builder...
ラン
[centos@haas ~]$ ls -F
ansible/
[centos@haas ~]$ ansible-container run
No DOCKER_HOST environment variable found. Assuming UNIX socket at /var/run/docker.sock
Attaching to ansible_ansible-container_1
Cleaning up Ansible Container builder...
Attaching to ansible_hands_temp_1