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

## やること

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

## ホスト

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


## マネージドノード: Amazon Linux 2
```dockerfile: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 ""
```

```shell
docker build -t amazonlinux2 .
```

## コントロールノード: Ansible

```dockerfile: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
```

```shell
docker build -t ansible .
```

## docker-compose

```yaml: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

```ini:hosts.ini
amzn2
```

```yaml: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
```