Systemd-nspawnでCentosとDebianコンテナを作る
ArchLinux環境にCentOSとDebianのコンテナを作りましょう。
これでAnsibleのテストがいつでもできるようになります。 サーバーでよく使われているCentOSとDebianがあれば必要十分だろうと思います。 この記事は以前書いたsystemd-nspawnの記事の続編です。
CentOSのテスト用コンテナを作る場合
yum が必要なのですこぶる気持ち悪いですが、ArchLinuxにyumをインストールします。
yay -S yum
mkdir centos
すこぶる気持ち悪いですが、ArchLinux環境にCentOSのリポジトリを登録します。 CentOSのBaseインストールが終わったら不要なのでそれから消してもよいでしょう。 今回は別にArchLinuxでyumをたたかないだろうしそのままにしておきます。1
sudo vim /etc/yum/repos.d/centos.repo
[centos]
name=centos
baseurl=http://ftp.jaist.ac.jp/pub/Linux/CentOS/7/os/x86_64/
enabled=1
さっき掘ったcentosディレクトリにchroot環境でCentOS7をインストールします。
sudo yum -y --releasever=7 --installroot=~/centos groupinstall "Base"
chrootしてCentOSにログインしてパスワードを設定します。
sudo chroot centos
passwd root
systemd-nspawnでログイン
sudo systemd-nspawn -b -D ~/centos
ここから CentOSコンテナに入ります。
yum install python openssh-server zsh bash-completion sudo curl
rootでAnsibleで利用するuserを作成 user名はansibleにする
useradd -m -G wheel -s /bin/zsh ansible
su - ansible
ssh-keygen -t rsa -b 4096
cd .ssh/
mv id_rsa.pub authorized_keys
chmod 600 authorized_keys
curl https://github.com/yourgithubid.keys >> ~/.ssh/authorized_keys ← Register public key registered with github
rootに戻って
systemctl enable sshd
systemctl start sshd
ホスト名を設定
hostname centos
vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 centos
vi /etc/pam.d/su
# コメントアウトを外す
auth required pam_wheel.so use_uid
sudo が使えるユーザ(グループ)を設定する
visudo
#Defaults requiretty(コメントアウトしてあるか確認)
## User privilege specification
root ALL=(ALL) ALL
ansible ALL=(ALL) ALL
# Uncomment to allow members of group wheel to execute any command
%wheel ALL=(ALL) ALL
## Same thing without a password
%wheel ALL=(ALL) NOPASSWD: ALL
これで ansible-playbook が通るようになりました。
Debianのテスト用コンテナを作る場合
Debianをインストールするためにすこぶる気持ち悪いですが、ArchLinuxに debootstrap,debian-archive-keyringをインストールします。
sudo pacman -S debootstrap
yay -S debian-archive-keyring
Debian stretchをchroot環境でdebianディレクトリにインストールします。
mkdir debian
sudo debootstrap stretch debian http://ftp.jaist.ac.jp/pub/Linux/debian/
chrootでDebianにログインしてパスワードを設定する
sudo chroot debian
passwd root
systemd-nspawnでDebianにログイン
sudo systemd-nspawn -b -D ~/debian
ここからDebianコンテナに入ります。
apt-get install python openssh-server zsh bash-completion sudo curl
useradd -m -G sudo -s /bin/zsh ansible
su - ansible
ssh-keygen -t rsa -b 4096
cd .ssh/
mv id_rsa.pub authorized_keys
chmod 600 authorized_keys
curl https://github.com/yourgithubid.keys >> ~/.ssh/authorized_keys ← Register public key registered with github
root に戻って
systemctl enable ssh
systemctl start ssh
ホスト名を設定(debian とする)
hostname debian
vi /etc/hosts
127.0.0.1 localhost debian
sudoが使えるユーザ(グループ)を設定する
update-alternatives --config editor
visudo
#Defaults requiretty(コメントアウトしてあるか確認)
## User privilege specification
root ALL=(ALL) ALL
ansible ALL=(ALL) ALL
# Uncomment to allow members of group wheel to execute any command
%sudo ALL=(ALL) ALL
## Same thing without a password
%sudo ALL=(ALL) NOPASSWD: ALL
systemd-nspawnはLinuxカーネルをホストのArchLinuxと共用するせいか iptableなどはAnsibleでうまくテストできなかったので注意してください。
ブログ記事を投稿したらTwitterでお知らせいたします。ブログを気に入っていただけた方はお気軽にフォローしてください。
— Solist Work (@SolistWork) 2018年11月27日
関連記事
最新記事
タグ一覧
-
ansible (2)
application (8)
chrome (1)
command (14)
django (1)
docker (3)
document (1)
emacs (14)
git (2)
google (1)
hugo (1)
kubernetes (1)
linux (20)
makefile (6)
mariadb (2)
melpa (1)
postgresql (2)
python (1)
security (2)
terminal (11)
thinkpad (2)
work (6)
zsh (10)