LoginSignup
5
4

More than 3 years have passed since last update.

Ansibleを利用してAzure仮想マシンを作成する

Last updated at Posted at 2019-02-18

はじめに

前回は、Windows 10 の WSL を利用して Azure 資格情報を設定し、Ansible を利用できる Python 仮想環境を作成しました。
Ansible を利用して Azure 仮想マシンを作成してみます。

前回:Ansibleを利用してAzure仮想マシンを作成するためのAzure資格情報の作成と準備

SSH接続のための公開鍵を確認する

  • Azure 仮想マシンを作成する際に必要となる公開鍵を確認します。

  • 既存の鍵を確認

#!/bin/bash
$ ls -al ~/.ssh
  • 必要に応じて新しい鍵の作成
#!/bin/bash
$ ssh-keygen -t rsa
  • Azure仮想マシンに設定する公開鍵の確認
#!/bin/bash
$ cat ~/.ssh/id_rsa.pub

仮想マシンを作成するための Ansible Playbook の作成

  • 下記ページを参考に Playbook を作成しました。

参考:Ansible を使用して Azure に Linux 仮想マシンを作成する

  • Ubuntu 18.04-LTS を作成するための Playbook となります。

  • リソースグループ名、ネットワーク設定、サイズなどは必要に応じて変更してください。

参考:汎用仮想マシンのサイズ

- name: Create Azure VM
  hosts: localhost
  connection: local
  tasks:
  - name: リソースグループの作成
    azure_rm_resourcegroup:
      name: ubuntu02ResourceGroup
      location: japaneast
  - name: 仮想ネットワークの作成
    azure_rm_virtualnetwork:
      resource_group: ubuntu02ResourceGroup
      name: vnet01
      address_prefixes: "20.0.0.0/16"
  - name: サブネットの追加
    azure_rm_subnet:
      resource_group: ubuntu02ResourceGroup
      name: subnet01
      address_prefix: "20.0.1.0/24"
      virtual_network: vnet01
  - name: パブリックIPアドレスの作成(固定IP)
    azure_rm_publicipaddress:
      resource_group: ubuntu02ResourceGroup
      allocation_method: Static
      name: pubip01
    register: output_ip_address
  - name: 作成したIPの表示
    debug:
      msg: "The public IP is {{ output_ip_address.state.ip_address }}."
  - name: ネットワークセキュリティグループの作成(SSHとHTTPを許可)
    azure_rm_securitygroup:
      resource_group: ubuntu02ResourceGroup
      name: nsg01
      rules:
        - name: SSH
          protocol: Tcp
          destination_port_range: 22
          access: Allow
          priority: 1001
          direction: Inbound
        - name: HTTP
          protocol: Tcp
          destination_port_range: 80
          access: Allow
          priority: 2001
          direction: Inbound
  - name: 仮想ネットワークインターフェースの作成
    azure_rm_networkinterface:
      resource_group: ubuntu02ResourceGroup
      name: vnic01
      virtual_network: vnet01
      subnet: subnet01
      public_ip_name: pubip01
      security_group: nsg01
  - name: Azure 仮想マシンの作成
    azure_rm_virtualmachine:
      resource_group: ubuntu02ResourceGroup
      name: ubuntu01
      vm_size: Standard_A1_v2
      admin_username: azureuser
      ssh_password_enabled: false
      ssh_public_keys:
      - path: /home/azureuser/.ssh/authorized_keys
        key_data: "ここに公開鍵を指定する"
      network_interfaces: vnic01
      image:
        offer: UbuntuServer
        publisher: Canonical
        sku: '18.04-LTS'
        version: latest
  • 作成した Playbook のファイル名を azure_create_complete_vm.yml として保存します。

  • 前回作成した Python 仮想環境を有効化して Playbook を実行します。

#!/bin/bash
$ cd ansible
$ source venv/bin/activate
$ ansible-playbook azure_create_complete_vm.yml
  • しばらく待つと Azure 仮想マシンができあがるはずです。
#!/bin/bash
$ ansible-playbook azure_create_complete_vm.yml
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
 [WARNING]: No inventory was parsed, only implicit localhost is available
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match
'all'

PLAY [Create Azure VM] *********************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [リソースグループの作成] ****************************************************
changed: [localhost]

TASK [仮想ネットワークの作成] ****************************************************
changed: [localhost]

TASK [サブネットの追加] **********************************************************
changed: [localhost]

TASK [パブリックIPアドレスの作成(固定IP)] ***************************************
changed: [localhost]

TASK [作成したIPの表示] **********************************************************
ok: [localhost] => {
    "msg": "The public IP is xxx.xxx.xxx.xxx."
}

TASK [ネットワークセキュリティグループの作成(SSHとHTTPを許可)] ********************************************************************************
changed: [localhost]

TASK [仮想ネットワークインターフェースの作成] **************************************
[DEPRECATION WARNING]: Setting ip_configuration flatten is deprecated and will be removed. Using ip_configurations list
 to define the ip configuration. This feature will be removed in version 2.9. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.
changed: [localhost]

TASK [Azure 仮想マシンの作成] ****************************************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=9    changed=7    unreachable=0    failed=0
  • 作成した Azure 仮想マシンに対して、ユーザーとIPアドレスを指定して SSH で接続します。
#!/bin/bash
ssh azureuser@xxx.xxx.xxx.xxx
  • SSH 接続を許可して、パスフレーズを入力すると、Azure 仮想マシンに SSH 接続できました。
#!/bin/bash
$ ssh azureuser@xxx.xxx.xxx.xxx
The authenticity of host 'xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx)' can't be established.
ECDSA key fingerprint is SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'xxx.xxx.xxx.xxx' (ECDSA) to the list of known hosts.
Enter passphrase for key '/home/hoge/.ssh/id_rsa':
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-1037-azure x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Mon Feb 18 12:41:52 UTC 2019

  System load:  0.1               Processes:           106
  Usage of /:   4.1% of 28.90GB   Users logged in:     0
  Memory usage: 12%               IP address for eth0: 20.0.1.4
  Swap usage:   0%

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

azureuser@ubuntu01:~$
  • SSH 接続できたので、いろいろ弄ってみたいと思います。

  • 異なる環境から Azure 仮想マシンに接続する場合は、authorized_keys に公開鍵を追加します。

#!/bin/bash
azureuser@ubuntu01:~$ vi ~/.ssh/authorized_keys

おわりに

思っていたよりも簡単に Ansible で Azure 仮想マシンを作成することができました。
リソースグループやネットワーク設定などを変数定義すれば、手軽に環境を量産できそうです。

5
4
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
5
4