Ansible 始めてみる
前々から学習しようと思っていたAnsible
なかなか腰が重かったけれど腰を据えてやってみよう、というわけで試してみる
環境・教材
$cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=19.04 DISTRIB_CODENAME=disco DISTRIB_DESCRIPTION="Ubuntu 19.04" $ansible --version ansible 2.7.10 python version = 2.7.16 (default, Apr 6 2019, 01:42:57) [GCC 8.3.0] $vagrant -v Vagrant 2.2.3
下準備
Vagrant,Ansible、VirtualBoxのインストール
この辺は省略作業フォルダ
学習用の作業ディレクトリを作る
(https://gitlab.com/shkitayama/ansible_practical_guide/tree/master からソースコードを持ってきてもおk)
ext:~/study/effective-ansible/
以降はこの作業フォルダ内でやることとする
実際にやってみる
この記事ではますはじめに ’2-3 Ansibleの動作確認’をやってみることとする
* Vagrantの準備
まず作業フォルダでVagrantを使えるようにする
仮想マシンは書籍に合わせてCentOSを用いることにした
$vagrant init centos/7 $ls Vagrantfile
これでVagrantによってVMを立てることが可能になる
* Ansibleで操作する用意
AnsibleによってVagrantを使うにはVagrantfileに記述する必要があるみたい
inventryはVagrantが生成してくれるので個別に書く必要はない(そしてそのほうがノード間でのアクセスも容易)
Vagrant.configure("2") do |config| config.vm.define "test1" do |node| node.vm.box = "centos/7" node.vm.hostname = "test1" node.vm.network :private_network, ip: "192.168.0.101" end config.vm.define "test2" do |node| node.vm.box = "centos/7" node.vm.hostname = "test2" node.vm.network :private_network, ip: "192.168.0.102" end #Ansible config.vm.provision "ansible" do |ansible| ansible.playbook = "test_playbook.yml" ansible.groups = { "test_servers" => ["test1", "test2"] } end end
--- - hosts: test_servers tasks: - name: create directory file: path: /home/vagrant/tmp state: directory owner: vagrant mode: 0755 - name: copy file copy: src: /etc/hosts dest: /home/vagrant/tmp/hosts owner: vagrant mode: 0644
note
config.vm.define "ホスト名" do |node|
を複数記述することで複数同時にVMを作成、起動できる
ansible.groups =
セクションにグループ名と所属させるホストを記述することでinventryにも同様に記述されグループごとにplaybook指定することができる
- 実際に動かしてみる
$vagrant up
で記述ミスがなければ書いたとおりの構成になり、かつAnsibleによってplaybookの内容が実行される
$ vagrant up Bringing machine 'test1' up with 'virtualbox' provider... Bringing machine 'test2' up with 'virtualbox' provider... ==> test1: Importing base box 'centos/7'... ==> test1: Matching MAC address for NAT networking... ==> test1: Checking if box 'centos/7' version '1902.01' is up to date... ==> test1: Setting the name of the VM: sec2_test1_1557299846266_77551 ==> test1: Clearing any previously set network interfaces... ==> test1: Preparing network interfaces based on configuration... test1: Adapter 1: nat test1: Adapter 2: hostonly ==> test1: Forwarding ports... test1: 22 (guest) => 2222 (host) (adapter 1) ==> test1: Booting VM... ==> test1: Waiting for machine to boot. This may take a few minutes... test1: SSH address: 127.0.0.1:2222 test1: SSH username: vagrant test1: SSH auth method: private key test1: test1: Vagrant insecure key detected. Vagrant will automatically replace test1: this with a newly generated keypair for better security. test1: test1: Inserting generated public key within guest... test1: Removing insecure key from the guest if it's present... test1: Key inserted! Disconnecting and reconnecting using new SSH key... ==> test1: Machine booted and ready! ==> test1: Checking for guest additions in VM... test1: No guest additions were detected on the base box for this VM! Guest test1: additions are required for forwarded ports, shared folders, host only test1: networking, and more. If SSH fails on this machine, please install test1: the guest additions and repackage the box to continue. test1: test1: This is not an error message; everything may continue to work properly, test1: in which case you may ignore this message. ==> test1: Setting hostname... ==> test1: Configuring and enabling network interfaces... ==> test1: Rsyncing folder: /home/ripple/study/effectiveAnsible/sec2/ => /vagrant ==> test1: Running provisioner: ansible... Vagrant has automatically selected the compatibility mode '2.0' according to the Ansible version installed (2.7.10). Alternatively, the compatibility mode can be specified in your Vagrantfile: https://www.vagrantup.com/docs/provisioning/ansible_common.html#compatibility_mode test1: Running ansible-playbook... PLAY [test_servers] ************************************************************ TASK [Gathering Facts] ********************************************************* ok: [test1] TASK [create directory] ******************************************************** changed: [test1] TASK [copy file] *************************************************************** changed: [test1] PLAY RECAP ********************************************************************* test1 : ok=3 changed=2 unreachable=0 failed=0 ==> test2: Importing base box 'centos/7'... (以下略)
無事に作成できた!
vagrant ssh test1
で中に入ってみると、playbookが反映されていることがわかると思う
note:
自動作成されたinventryは
~/study/effectiveAnsible/sec2/.vagrant/provisioners/ansible/inventory
にある
中はこんな感じ
# Generated by Vagrant test1 ansible_host=127.0.0.1 ansible_port=2222 ansible_user='vagrant' ansible_ssh_private_key_file='/home/ripple/study/effectiveAnsible/sec2/.vagrant/machines/test1/virtualbox/private_key' test2 ansible_host=127.0.0.1 ansible_port=2200 ansible_user='vagrant' ansible_ssh_private_key_file='/home/ripple/study/effectiveAnsible/sec2/.vagrant/machines/test2/virtualbox/private_key' [test_servers] test1 test2
手動で作成する必要がなく、ノード間の疎通も取れるのでお手軽みたい
今回は最初の章なので、複雑なことはしてないようだけれど、Vagrantを用いることで個別にVMを構築、設定する手間が省けるのでぜひ使えるようになりたいところ
最後は$ vagrant destroy -f
で環境を消しておしまい
続きはまた後日