読者です 読者をやめる 読者になる 読者になる

カメニッキ

カメとインコと釣りの人です

Vagrantで作ったホストにAnsibleでプロビジョニングする

なんかすごく簡単だった。

Ansibleつかってみた - インフラと亀と で書いた、SSHがどうのこうのは一切きにしなくてよかった。

前提

  • Macです
  • xcodeが入ってること
  • vagrantが入ってること
  • まともに動くboxがあること

ansibleを入れる

Installation — Ansible Documentation

[PMAC226S vagansible]$ sudo easy_install pip
[PMAC226S vagansible]$ sudo pip install ansible --quiet

playbook書く

特に意味ないレシピですが、epel入れて、slコマンド入れて、ls叩いてる・・・

[PMAC226S vagansible]$ vi playbook.yml
---
- hosts: all
  sudo: yes
  tasks:
  - name: epel-release install
    yum: name=epel-release state=installed

  - name: sl command installed
    yum: name=sl state=installed

  - name: ls command run!
    shell: /bin/ls > /tmp/list.txt
    ignore_errors: True

Vagrantfileにansible叩くように記載

[PMAC226S vagansible]$ vi Vagrantfile
(略)
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "playbook.yml"
  end
(略)

vagrant up

[PMAC226S vagansible]$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'CentOS6.6_Packer'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagansible_default_1432823564506_68852
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2200 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if its present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/PMAC226S/Vagrant/vagansible
==> default: Running provisioner: ansible...
PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --private-key=/Users/PMAC226S/Vagrant/vagansible/.vagrant/machines/default/virtualbox/private_key --user=vagrant --connection=ssh --limit='default' --inventory-file=/Users/PMAC226S/Vagrant/vagansible/.vagrant/provisioners/ansible/inventory playbook.yml

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [default]

TASK: [epel-release install] **************************************************
changed: [default]

TASK: [sl command installed] **************************************************
changed: [default]

TASK: [ls command run!] *******************************************************
changed: [default]

PLAY RECAP ********************************************************************
default                    : ok=4    changed=3    unreachable=0    failed=0

一つのboxからslコマンドが入っているゲストと、slコマンドが入っていないゲストを自由に作って潰してできるようになるので、試行錯誤してなんか作るときにはいいと思った

itamaeも似たような感じだった Itamae | vagrant-itamae plugin で Vagrantにトイレを設置 #devops #itamae - Qiita