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.

Amazon Linux 2にnvmをインストールするテスト

Posted at

やること

  • amzn2にnvmをインストール
  • nvmでnodejs 16.3.0をインストール

ホスト

  • コントロールノード
    ansible
  • マネージドノード
    amzn2

マネージドノード: Amazon Linux 2

Dockerfile
FROM amazonlinux:2

COPY ./id_rsa.pub /tmp/

RUN yum -y update && \
    yum -y install \
        sudo \
        shadow-utils \
        procps \
        wget \
        openssh-server \
        openssh-clients \
        which \
        iproute \
        tar \
        e2fsprogs && \
    yum clean all && \
    wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python && \
    echo "PasswordAuthentication no" >> /etc/ssh/sshd_config && \
    useradd ec2-user && \
    echo "ec2-user ALL=NOPASSWD: ALL" >> /etc/sudoers && \
    sudo -u ec2-user mkdir -p /home/ec2-user/.ssh && \
    mv /tmp/id_rsa.pub /home/ec2-user/.ssh/ && \
    cat /home/ec2-user/.ssh/id_rsa.pub >> /home/ec2-user/.ssh/authorized_keys && \
    echo "export LANG=en_US.UTF-8" >> /home/ec2-user/.bash_profile

RUN chmod -R go-rwx /home/ec2-user/.ssh
RUN chown -R ec2-user:ec2-user /home/ec2-user/.ssh
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ""
RUN ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ""
RUN ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
docker build -t amazonlinux2 .

コントロールノード: Ansible

Dockerfile
FROM amazonlinux:2

COPY ./id_rsa.pub /tmp/
COPY ./id_rsa /tmp/

RUN yum -y update && \
    yum -y install \
        sudo \
        shadow-utils \
        procps \
        wget \
        openssh-server \
        openssh-clients \
        which \
        iproute \
        e2fsprogs && \
    yum clean all && \

    wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python && \
    echo "PasswordAuthentication no" >> /etc/ssh/sshd_config && \
    useradd ec2-user && \
    echo "ec2-user ALL=NOPASSWD: ALL" >> /etc/sudoers && \
    sudo -u ec2-user mkdir -p /home/ec2-user/.ssh && \
    mv /tmp/id_rsa.pub /home/ec2-user/.ssh/ && \
    mv /tmp/id_rsa /home/ec2-user/.ssh/ && \
    cat /home/ec2-user/.ssh/id_rsa.pub >> /home/ec2-user/.ssh/authorized_keys && \
    echo "export LANG=en_US.UTF-8" >> /home/ec2-user/.bash_profile

RUN chmod -R go-rwx /home/ec2-user/.ssh
RUN chown -R ec2-user:ec2-user /home/ec2-user/.ssh

RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ""
RUN ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ""
RUN ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""

RUN amazon-linux-extras enable ansible2
RUN yum clean metadata
RUN yum -y install ansible
docker build -t ansible .

docker-compose

docker-compose.yml
version: '3'
services:
  amzn2:
    image: amazonlinux2-sshd
    container_name: amzn2
    command: /usr/sbin/sshd -D
    hostname: amzn2
    ports:
      - "10022:22"

  ansible:
    image: ansible
    container_name: ansible
    command: /usr/sbin/sshd -D
    hostname: ansible
    ports:
      - "20022:22"

Playbook

hosts.ini
amzn2
install_nodejs.yml
- hosts: amzn2
  tasks:
    - get_url:
        url: https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh
        dest: /tmp/install.sh
        mode: 0755

    - shell: /tmp/install.sh
    - shell: source /home/ec2-user/.nvm/nvm.sh && nvm install 16.3.0
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?