Ubuntu(WSL)にansibleインストール & 作業準備

7月 25, 2022ansible,AWS,EC2,Ubuntu(WSL)

概要

  • 今回は、備忘録としてansibleインストール & セットアップ方法をまとめます。前半は、Ubuntu(WSL)にansibleをインストールしています。後半は、ansible からAWS のEC2 に対してplaybook を実行する方法を記載しています。
  • 背景として、playgook を利用した作業があり、手元にansible の環境がなかったためセットアップします。前提として、Windows 10 + WSL2 上のUbuntu を利用します。

 

Ubuntu(WSL)にansibleインストール手順

パッケージのアップデート

  • 事前作業として、apt-get updateでパッケージリストを更新します。
niikawa@niikawa1:~$ sudo apt-get update
Hit:1 http://archive.ubuntu.com/ubuntu bionic InRelease
Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:4 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [2092 kB]
Get:5 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:6 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [2439 kB]
Get:7 http://security.ubuntu.com/ubuntu bionic-security/main Translation-en [370 kB]
Get:8 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [628 kB]
Get:9 http://archive.ubuntu.com/ubuntu bionic-updates/main Translation-en [462 kB]
Get:10 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [652 kB]
Get:11 http://security.ubuntu.com/ubuntu bionic-security/restricted Translation-en [85.8 kB]
Get:12 http://archive.ubuntu.com/ubuntu bionic-updates/restricted Translation-en [89.4 kB]
Get:13 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [1175 kB]
Get:14 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [1790 kB]
Get:15 http://security.ubuntu.com/ubuntu bionic-security/universe Translation-en [270 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic-updates/universe Translation-en [387 kB]
Get:17 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [17.6 kB]
Get:18 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [24.2 kB]
Get:19 http://security.ubuntu.com/ubuntu bionic-security/multiverse Translation-en [3660 B]
Get:20 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse Translation-en [5980 B]
Get:21 http://archive.ubuntu.com/ubuntu bionic-backports/main amd64 Packages [10.3 kB]
Get:22 http://archive.ubuntu.com/ubuntu bionic-backports/main Translation-en [4824 B]
Get:23 http://archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [11.3 kB]
Get:24 http://archive.ubuntu.com/ubuntu bionic-backports/universe Translation-en [5772 B]
Fetched 10.8 MB in 6s (1800 kB/s)
Reading package lists... Done

 

requiredパッケージのインストール

  • ansible に必要な software-properties-common のパッケージをインストールします。
niikawa@niikawa1:~$ sudo apt-get install software-properties-common
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
  libfreetype6
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
  python3-software-properties
The following packages will be upgraded:
  python3-software-properties software-properties-common
2 upgraded, 0 newly installed, 0 to remove and 123 not upgraded.
Need to get 33.8 kB of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 software-properties-common all 0.96.24.32.18 [10.1 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3-software-properties all 0.96.24.32.18 [23.8 kB]
Fetched 33.8 kB in 1s (39.7 kB/s)
(Reading database ... 37916 files and directories currently installed.)
Preparing to unpack .../software-properties-common_0.96.24.32.18_all.deb ...
Unpacking software-properties-common (0.96.24.32.18) over (0.96.24.32.14) ...
Preparing to unpack .../python3-software-properties_0.96.24.32.18_all.deb ...
Unpacking python3-software-properties (0.96.24.32.18) over (0.96.24.32.14) ...
Setting up python3-software-properties (0.96.24.32.18) ...
Setting up software-properties-common (0.96.24.32.18) ...
Processing triggers for dbus (1.12.2-1ubuntu1.2) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

 

リポジトリの追加

  • ansible のリポジトリを追加します。
niikawa@niikawa1:~$ sudo apt-add-repository --yes --update ppa:ansible/ansible
Hit:1 http://security.ubuntu.com/ubuntu bionic-security InRelease
Get:2 http://ppa.launchpad.net/ansible/ansible/ubuntu bionic InRelease [15.9 kB]
Hit:3 http://archive.ubuntu.com/ubuntu bionic InRelease
Hit:4 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:5 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
Get:6 http://ppa.launchpad.net/ansible/ansible/ubuntu bionic/main amd64 Packages [704 B]
Get:7 http://ppa.launchpad.net/ansible/ansible/ubuntu bionic/main Translation-en [472 B]
Fetched 17.1 kB in 2s (9680 B/s)
Reading package lists... Done

 

ansible のインストール

  • apt-get install でansible をインストールします。
niikawa@niikawa1:~$ sudo apt-get install ansible
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
  libfreetype6
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
  python-asn1crypto python-cffi-backend python-crypto python-cryptography python-enum34 python-httplib2 python-idna
  python-ipaddress python-jinja2 python-markupsafe python-paramiko python-pyasn1 python-six python-yaml sshpass
Suggested packages:
  python-crypto-doc python-cryptography-doc python-cryptography-vectors python-enum34-doc python-jinja2-doc
  python-gssapi
The following NEW packages will be installed:
  ansible python-asn1crypto python-cffi-backend python-crypto python-cryptography python-enum34 python-httplib2
  python-idna python-ipaddress python-jinja2 python-markupsafe python-paramiko python-pyasn1 python-six python-yaml
  sshpass
0 upgraded, 16 newly installed, 0 to remove and 123 not upgraded.
Need to get 6982 kB of archives.
After this operation, 64.8 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ppa.launchpad.net/ansible/ansible/ubuntu bionic/main amd64 ansible all 2.9.27-1ppa~bionic [5804 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-markupsafe amd64 1.0-1build1 [13.0 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python-jinja2 all 2.10-1ubuntu0.18.04.1 [94.8 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-yaml amd64 3.12-1build2 [115 kB]
Get:5 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-asn1crypto all 0.24.0-1 [72.7 kB]
Get:6 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-cffi-backend amd64 1.11.5-1 [63.4 kB]
Get:7 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-enum34 all 1.1.6-2 [34.8 kB]
Get:8 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-idna all 2.6-1 [32.4 kB]
Get:9 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-ipaddress all 1.0.17-1 [18.2 kB]
Get:10 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-six all 1.11.0-2 [11.3 kB]
Get:11 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python-cryptography amd64 2.1.4-1ubuntu1.4 [276 kB]
Get:12 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-pyasn1 all 0.4.2-3 [46.7 kB]
Get:13 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python-paramiko all 2.0.0-1ubuntu1.2 [110 kB]
Get:14 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python-httplib2 all 0.9.2+dfsg-1ubuntu0.3 [34.9 kB]
Get:15 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-crypto amd64 2.6.1-8ubuntu2 [244 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic/universe amd64 sshpass amd64 1.06-1 [10.5 kB]
Fetched 6982 kB in 5s (1295 kB/s)
Selecting previously unselected package python-markupsafe.
(Reading database ... 37916 files and directories currently installed.)
Preparing to unpack .../00-python-markupsafe_1.0-1build1_amd64.deb ...
Unpacking python-markupsafe (1.0-1build1) ...
Selecting previously unselected package python-jinja2.
Preparing to unpack .../01-python-jinja2_2.10-1ubuntu0.18.04.1_all.deb ...
Unpacking python-jinja2 (2.10-1ubuntu0.18.04.1) ...
Selecting previously unselected package python-yaml.
Preparing to unpack .../02-python-yaml_3.12-1build2_amd64.deb ...
Unpacking python-yaml (3.12-1build2) ...
Selecting previously unselected package python-asn1crypto.
Preparing to unpack .../03-python-asn1crypto_0.24.0-1_all.deb ...
Unpacking python-asn1crypto (0.24.0-1) ...
Selecting previously unselected package python-cffi-backend.
Preparing to unpack .../04-python-cffi-backend_1.11.5-1_amd64.deb ...
Unpacking python-cffi-backend (1.11.5-1) ...
Selecting previously unselected package python-enum34.
Preparing to unpack .../05-python-enum34_1.1.6-2_all.deb ...
Unpacking python-enum34 (1.1.6-2) ...
Selecting previously unselected package python-idna.
Preparing to unpack .../06-python-idna_2.6-1_all.deb ...
Unpacking python-idna (2.6-1) ...
Selecting previously unselected package python-ipaddress.
Preparing to unpack .../07-python-ipaddress_1.0.17-1_all.deb ...
Unpacking python-ipaddress (1.0.17-1) ...
Selecting previously unselected package python-six.
Preparing to unpack .../08-python-six_1.11.0-2_all.deb ...
Unpacking python-six (1.11.0-2) ...
Selecting previously unselected package python-cryptography.
Preparing to unpack .../09-python-cryptography_2.1.4-1ubuntu1.4_amd64.deb ...
Unpacking python-cryptography (2.1.4-1ubuntu1.4) ...
Selecting previously unselected package python-pyasn1.
Preparing to unpack .../10-python-pyasn1_0.4.2-3_all.deb ...
Unpacking python-pyasn1 (0.4.2-3) ...
Selecting previously unselected package python-paramiko.
Preparing to unpack .../11-python-paramiko_2.0.0-1ubuntu1.2_all.deb ...
Unpacking python-paramiko (2.0.0-1ubuntu1.2) ...
Selecting previously unselected package python-httplib2.
Preparing to unpack .../12-python-httplib2_0.9.2+dfsg-1ubuntu0.3_all.deb ...
Unpacking python-httplib2 (0.9.2+dfsg-1ubuntu0.3) ...
Selecting previously unselected package python-crypto.
Preparing to unpack .../13-python-crypto_2.6.1-8ubuntu2_amd64.deb ...
Unpacking python-crypto (2.6.1-8ubuntu2) ...
Selecting previously unselected package sshpass.
Preparing to unpack .../14-sshpass_1.06-1_amd64.deb ...
Unpacking sshpass (1.06-1) ...
Selecting previously unselected package ansible.
Preparing to unpack .../15-ansible_2.9.27-1ppa~bionic_all.deb ...
Unpacking ansible (2.9.27-1ppa~bionic) ...
Setting up python-idna (2.6-1) ...
Setting up python-yaml (3.12-1build2) ...
Setting up python-asn1crypto (0.24.0-1) ...
Setting up python-crypto (2.6.1-8ubuntu2) ...
Setting up python-pyasn1 (0.4.2-3) ...
Setting up python-markupsafe (1.0-1build1) ...
Setting up python-httplib2 (0.9.2+dfsg-1ubuntu0.3) ...
Setting up python-cffi-backend (1.11.5-1) ...
Setting up python-six (1.11.0-2) ...
Setting up sshpass (1.06-1) ...
Setting up python-enum34 (1.1.6-2) ...
Setting up python-ipaddress (1.0.17-1) ...
Setting up python-jinja2 (2.10-1ubuntu0.18.04.1) ...
Setting up python-cryptography (2.1.4-1ubuntu1.4) ...
Setting up python-paramiko (2.0.0-1ubuntu1.2) ...
Setting up ansible (2.9.27-1ppa~bionic) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

 

  • インストールされたことを確認します。
niikawa@niikawa1:~$ which ansible
/usr/bin/ansible
niikawa@niikawa1:~$ ansible --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/niikawa/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.17 (default, Feb 27 2021, 15:10:58) [GCC 7.5.0]

 

ansible コマンドをテストする

  • テストのため、localhost に対してping を行います。無事に、成功しました。
niikawa@niikawa1:~$ ansible -m ping localhost
localhost | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

 

AWS EC2 にplaybook を実行する

inventory とplaybook の準備

  • inventory を準備します。inventory は、ターゲットノードを管理するためのファイルです。ansible_host にserver1 で使用するIPアドレスを定義します。
  • “[ ]"で囲み、グループの定義も可能です。グループに対して、ユーザー名、ssh秘密鍵を指定しています。
$ cat test01_inventory.ini
[server]
server1 ansible_host=11.11.11.11

[server:vars]
ansible_user=ec2-user
ansible_ssh_private_key_file=./niikawa-testkey.pem
  • playbook を準備します。playbook は、タスクを定義する YAMLファイルです。今回のタスクは、/tmp にtest.txt を作成します。
$ cat test_book.yml
- hosts: server
  tasks:
   - name: playbook-test
     file:
        path: /tmp/test.txt
        state: touch

 

ansible-playbook コマンドでplaybook を実行する

  • ansible-playbook コマンドを実行します。playbook の実行は成功しました。
$ ansible-playbook -i test01_inventory.ini test_book.yml

PLAY [server] **********************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
The authenticity of host '11.11.11.11 (11.11.11.11)' can't be established.
ECDSA key fingerprint is SHA256:ET+dyyO9ibjPcnkO367b2g8NjXOTIUf6ofQyup1BIqg.
Are you sure you want to continue connecting (yes/no)? yes
[WARNING]: Platform linux on host server1 is using the discovered Python interpreter at /usr/bin/python, but future
installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [server1]

TASK [playbook-test] ***************************************************************************************************
changed: [server1]

PLAY RECAP *************************************************************************************************************
server1                    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

 

  • 念のため、ssh でログインし、期待値通り、/tmp にtest.txt が作成されたことを確認します。
$ ssh -i ./niikawa-testkey.pem ec2-user@11.11.11.11
Last login: Tue Jul 26 01:26:09 2022 from xxx.xxx.xxx.xxx

       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/
37 package(s) needed for security, out of 82 available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-172-31-23-227 ~]$ ls -l /tmp/test.txt
-rw-rw-r-- 1 ec2-user ec2-user 0 Jul 26 01:26 /tmp/test.txt

 

  • これで準備が整いましたので、目的のplaybook を使った作業に入ります!

 

ansible,AWS,EC2,Ubuntu(WSL)

Posted by takaaki