これは、なにをしたくて書いたもの?
ちょっとAnsibleを勉強しようかなと思いまして。
まずは、Ubuntu Linuxにインストールして簡単に確認してみようかな、と。
Ansible
サーバーに対して、ソフトウェアのインストールを自動化するためのツールです。
Ansible is Simple IT Automation
いわゆる、IaCの文脈で語られるツールの一種ですね。
- 設定がYAMLファイル
- エージェントレス
というところが特徴かと。
環境
今回の環境は、こちらです。
$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 18.04.3 LTS Release: 18.04 Codename: bionic $ python -V Python 2.7.15+ $ python3 -V Python 3.6.9
Pythonは2系と3系が入っています。インストール要件としては、Python 2.7またはPython 3.5以上なので、どちらでもOKです。
ドキュメントに従うと、aptかpipでインストールするのが良さそうなので、この2つを試してみたいと思います。
Installing Ansible — Ansible Documentation
PPAからインストールする
まずは、PPAでインストールする方法から。
Latest Releases via Apt (Ubuntu)
インストール。
$ sudo apt-add-repository --yes --update ppa:ansible/ansible $ sudo apt install ansible
バージョンを確認。
$ ansible --version ansible 2.9.1 config file = /etc/ansible/ansible.cfg configured module search path = [u'$HOME/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/dist-packages/ansible executable location = /usr/bin/ansible python version = 2.7.15+ (default, Oct 7 2019, 17:39:04) [GCC 7.4.0]
Python 2系で動作するAnsibleがインストールされましたね。現時点でのバージョンは、2.9.1です。
pipでインストールする
続いて、pipでインストールしてみます。
Python 3系のpipでインストール。
$ pip3 install --user ansible
確認。
$ ansible --version ansible 2.9.1 config file = None configured module search path = ['$HOME/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = $HOME/.local/lib/python3.6/site-packages/ansible executable location = $HOME/.local/bin/ansible python version = 3.6.9 (default, Nov 7 2019, 10:44:02) [GCC 8.3.0]
pip3でインストールしたので、Python 3系で動くAnsibleがインストールされました。
今回は、pip版で動かしてみようかなと思います。
ちょっと動かしてみる
それでは、インストールしたAnsibleを動かしてみましょう。
今回は、Vagrantで立ち上げたUbuntu Linux 18.04の仮想マシンに、Apacheをインストールしてみたいと思います。
$ vagrant init generic/ubuntu1804 $ vagrant up
SSHの接続情報を確認。
$ vagrant ssh-config Host default HostName 192.168.121.114 User vagrant Port 22 UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no IdentityFile /path/to/.vagrant/machines/default/libvirt/private_key IdentitiesOnly yes LogLevel FATAL
AnsibleのInventryファイルを用意。
How to build your inventory — Ansible Documentation
hosts
[vagrant] 192.168.121.114
簡単なPlaybookを用意。
Intro to Playbooks — Ansible Documentation
playbook.yml
--- - hosts: vagrant remote_user: vagrant tasks: - name: install apache2 apt: name: apache2 become: true - name: apache2 is running systemd: state: started name: apache2
作成したInventry、Playbookと、VagrantのSSH Private Keyを指定して、ansible-playbookを実行。
$ ansible-playbook -i hosts playbook.yml --private-key /path/to/.vagrant/machines/default/libvirt/private_key PLAY [vagrant] ***************************************************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************************************************* The authenticity of host '192.168.121.114 (192.168.121.114)' can't be established. ECDSA key fingerprint is SHA256:cjlFesPErwQsFNkaywCIl2Tk0AM9eDJzqRcKi0DZhAg. Are you sure you want to continue connecting (yes/no)? yes ok: [192.168.121.114] TASK [install apache2] ********************************************************************************************************************************************************************* changed: [192.168.121.114] TASK [apache2 is running] ****************************************************************************************************************************************************************** ok: [192.168.121.114] PLAY RECAP ********************************************************************************************************************************************************************************* 192.168.121.114 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
完了。
Apacheの確認。
$ curl -s 192.168.121.114 | grep 'Apache2 Ubuntu' <title>Apache2 Ubuntu Default Page: It works</title> Apache2 Ubuntu Default Page
OKですね。
最初は、こんなところで。