chiwata’s blog

たまに技術の話をします。

WSL+VagrantでつくったサーバをAnsibleでプロビジョニングしたい

最近、同期から「Linux勉強したいんだけど何したらいい?」とよく聞かれるのですが、私が教えてほしいです。

そこで、初めてインフラをやり始めたときに何してたかなーと思ったところ、ミニレンタルサーバっぽい何かを作ったことを思い出したので、そんな感じのやつを作ってみたら?と勧めてみました。

ミニレンタルサーバは、webサーバとリバースプロキシを二台ずつ、LBを構築してなんかいい感じに勉強するためだけのやつです。レンサバ的機能は一つも作りません、名前負けです。あと私の力では作れません。勉強します

ただ、自分が何もできないと悲しいので、最低限環境だけは整えたいと思います。 ちなみに、当時はitamaeでプロビジョニングしていた気がしますが、今回はAnsibleでプロビジョニングします。

WSLのUbuntuでVagrantを使いたい - chiwata’s blog

Vagrantwindowsで使えるようにするには前回の記事でやっていますので、続きからになります。

まず。Ansibleをインストールします

sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible

今回は以下のようにして試します

. 
├── README.md
├── Vagrantfile
├── ansible.cfg
└── provisioning
    ├── hosts
    └── roles 
        └── users
            └── users.yaml

ansible.cfgに以下を記載

[default] 
inventory = provisioning/hosts

provisioning/roles/users/users.yamlは以下のように

---  
- hosts: users
  sudo: true
  tasks:
  - name: install apache
    yum: name=httpd

Vagrantfileは以下のように

Vagrant.configure("2") do |config|  
  config.vm.box = "centos/7"
  config.vm.define "users001" do |users001|
    users001.vm.network "private_network", ip: "192.168.33.10"
    config.vm.provision "ansible" do |ansible|
      ansible.playbook = "provisioning/roles/users/users.yaml"
      ansible.inventory_path = "provisioning/hosts"
      ansible.limit = "users"
    end
  end  
end

vagrant upしてみます

$ vagrant up 
/opt/vagrant/embedded/gems/2.2.4/gems/vagrant-2.2.4/lib/vagrant/util/which.rb:37: warning: Insecure world writable dir /mnt/c/ProgramData/DockerDesktop/version-bin in PATH, mode 040777
Bringing machine 'users001' up with 'virtualbox' provider... 
==> users001: Importing base box 'centos/7'... 
==> users001: Matching MAC address for NAT networking... 
==> users001: Checking if box 'centos/7' version '1902.01' is up to date... 
==> users001: Setting the name of the VM: mini_rental_server_users001_1560671696972_10166 
==> users001: Clearing any previously set network interfaces... 
==> users001: Preparing network interfaces based on configuration... 
    users001: Adapter 1: nat
    users001: Adapter 2: hostonly
==> users001: Forwarding ports... 
    users001: 22 (guest) => 2222 (host) (adapter 1) 
==> users001: Booting VM... 
==> users001: Waiting for machine to boot. This may take a few minutes... 
    users001: SSH address: 127.0.0.1:2222 
    users001: SSH username: vagrant
    users001: SSH auth method: private key
    users001:  
    users001: Vagrant insecure key detected. Vagrant will automatically replace
    users001: this with a newly generated keypair for better security.
    users001:  
    users001: Inserting generated public key within guest...
    users001: Removing insecure key from the guest if it's present... 
    users001: Key inserted! Disconnecting and reconnecting using new SSH key... 
==> users001: Machine booted and ready! 
==> users001: Checking for guest additions in VM... 
    users001: No guest additions were detected on the base box for this VM! Guest 
    users001: additions are required for forwarded ports, shared folders, host only
    users001: networking, and more. If SSH fails on this machine, please install
    users001: the guest additions and repackage the box to continue.
    users001:
    users001: This is not an error message; everything may continue to work properly,
    users001: in which case you may ignore this message.
==> users001: Configuring and enabling network interfaces... 
==> users001: Rsyncing folder: /home/chiwata/dev/mini_rental_server/ => /vagrant 
==> users001: Running provisioner: ansible... 
Vagrant has automatically selected the compatibility mode '2.0' 
according to the Ansible version installed (2.8.1).

Alternatively, the compatibility mode can be specified in your Vagrantfile:
https://www.vagrantup.com/docs/provisioning/ansible_common.html#compatibility_mode

    users001: Running ansible-playbook... 
 [WARNING] Ansible is being run in a world writable directory (/home/chiwata/dev/mini_rental_server), ignoring it as an ansible.cfg source. For more information see https://docs.ansible.com/ansible/devel/reference_appendices/config.html#cfg-in-world-writable-dir
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and  
make sure become_method is 'sudo' (default). This feature will be removed in 
version 2.9. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.

PLAY [users] *******************************************************************

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

TASK [install apache] **********************************************************
changed: [192.168.33.10] 

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

ゲストにログインして、Apacheがインストールされているか確認します

[vagrant@localhost ~]$ httpd -v 
Server version: Apache/2.4.6 (CentOS) 
Server built:   Apr 24 2019 13:45:48

ちゃんとインストールされていますね。 sudo: trueは非推奨だそうなのでbecome: yesのほうがいいっぽいです。

ansible.limit = "users"をはじめ記載しておらず、はまってしまいました。 とりあえず動いたのでここまで