1
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 5 years have passed since last update.

docker で ansibleコンテナ作ってlocalともう1台にpingするまで

Posted at

Dockerfile

# Dockerfile
#
#
FROM centos:centos6
MAINTAINER tkni2005

ENV USER_NAME root
ENV USER_PASSWORD hogehoge

RUN yum clean all
RUN yum update -y
RUN yum install -y which rsyslog rsync cronie mailx
RUN cp /usr/share/zoneinfo/Japan /etc/localtime

# repos
RUN rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
RUN rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
RUN rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
RUN rpm -ivh http://repo.zabbix.com/zabbix/2.2/rhel/6/x86_64/zabbix-release-2.2-1.el6.noarch.rpm

# ssh
RUN yum install -y sudo passwd openssh openssh-clients openssh-server
RUN sed -i '/pam_loginuid\.so/s/required/optional/' /etc/pam.d/sshd
RUN echo "$USER_NAME:$USER_PASSWORD" | chpasswd

# ansible
RUN yum install -y ansible

# ec2-user
RUN useradd ec2-user
RUN mkdir /home/ec2-user/.ssh
RUN echo 'ssh-rsa AAAAB3NzaC1yc2EAAAA(略) hoge' > /home/ec2-user/.ssh/authorized_keys
RUN chown -R ec2-user.  /home/ec2-user/.ssh
RUN chmod 700  /home/ec2-user/.ssh 
RUN chmod 600  /home/ec2-user/.ssh/authorized_keys 
 
# hoge.pem
ADD hoge.pem /hoge.pem

# run     
RUN touch /bin/run ; chmod 755 /bin/run
RUN echo /etc/init.d/sshd start >> /bin/run
RUN echo ssh-agent bash >> /bin/run
          
EXPOSE 22/tcp

docker build

 docker build -t tkni2005/ansible .

docker run

docker run -t -i -p 2222:22 tkni2005/ansible /bin/bash

sshまわり設定

コンテナログイン後
bash-4.1# run
Generating SSH1 RSA host key:                              [  OK  ]
Generating SSH2 RSA host key:                              [  OK  ]
Generating SSH2 DSA host key:                              [  OK  ]
Starting sshd:                                             [  OK  ]
bash-4.1# 
bash-4.1# ssh-add /hoge.pem
Identity added: /hoge.pem (/hoge.pem)
bash-4.1# 

ansible hosts

bash-4.1# cat /etc/ansible/hosts 
10.101.0.68
127.0.0.1
bash-4.1#

ansible -m ping

bash-4.1# ansible all -uec2-user -m ping

paramiko: The authenticity of host '127.0.0.1' can't be established.
The ssh-rsa key fingerprint is f4a370547208cd59742523f2760217b9.
Are you sure you want to continue connecting (yes/no)?
yes

paramiko: The authenticity of host '10.101.0.68' can't be established.
The ssh-rsa key fingerprint is 129c632cb6ce3bc298fa19277938cbf5.
Are you sure you want to continue connecting (yes/no)?
yes
127.0.0.1 | success >> {
    "changed": false, 
    "ping": "pong"
}

10.101.0.68 | success >> {
    "changed": false, 
    "ping": "pong"
}

bash-4.1#
1
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
1
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?