getting started ansible


centOSを3つ立ち上げて、そのうちの一つにepel、ansibleをインストールします。

vagrant.file

  config.vm.define "host" do |node|
    node.vm.box = "bento/centos-6.7"
    node.vm.hostname = "host"
    node.vm.network :private_network, ip: "192.168.43.51"
  end

  config.vm.define "web" do |node|
  node.vm.box = "bento/centos-6.7"
    node.vm.hostname = "web"
    node.vm.network :private_network, ip: "192.168.43.52"
  end

  config.vm.define "db" do |node|
  node.vm.box = "bento/centos-6.7"
    node.vm.hostname = "db"
    node.vm.network :private_network, ip: "192.168.43.53"
  end
vagrant ssh host
[vagrant@host ~]$ wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[vagrant@host ~]$ sudo rpm -Uvh epel-release-6-8.noarch.rpm
[vagrant@host ~]$ sudo yum -y install ansible
[vagrant@host ~]$ ansible --version
ansible 2.2.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

vi .ssh/config

Host web
 HostName 192.168.43.52
Host db
 HostName 192.168.43.53

アクセス権を変更し、秘密鍵、公開鍵を作成して、web, dbにcopy。

vagrant@host ~]$ chmod 600 .ssh/config
[vagrant@host ~]$ ssh-keygen -t rsa
[vagrant@host ~]$ ssh-copy-id web
[vagrant@host ~]$ ssh-copy-id db

すると、web, dbにログインできるようになります。

[vagrant@host ~]$ ssh web
[vagrant@web ~]$ exit
logout
Connection to 192.168.43.52 closed.
[vagrant@host ~]$ ssh db
[vagrant@db ~]$

hostファイルの作成

[vagrant@host ~]$ vi hosts
[web]
192.168.43.52

[db]
192.168.43.53
[vagrant@host ~]$ vi ansible.cfg
[defaults]
hostfile = ./hosts