Ansible で PHP 環境構築 ① - git インストール -
seven-greenz.hatenablog.com Vagrant を使って仮装環境を作成しました。その CentOS 仮装環境上に PHP 環境を構築したいと思います。 が、トライしてみたところ全然進まなかったので今回は git のインストールまでです。
Vagrantfile
Vagrant.configure("2") do |config| config.vm.box = "centos7.2" config.vm.network "private_network", ip: "192.168.33.20" config.vm.define :simple do |simple| simple.vm.hostname = 'simple' # ゲスト側にansibleをインストール config.vm.provision 'ansible_local' do |ansible| ansible.playbook = './playbook.yml' end end end
playbook.yml
- hosts: simple become: yes tasks: # - name: upgrade all packeages # yum: # name: '*' # state: latest # 最新のgitをインストールするため - name: add IUS repository yum_repository: name: ius description: IUS YUM repo for install the latest git baseurl: https://dl.iuscommunity.org/pub/ius/stable/CentOS/7/x86_64 # https://access.redhat.com/documentation/ja-JP/Red_Hat_Enterprise_Linux/6/html/Security_Guide/sect-Security_Guide-Updating_Packages-Verifying_Signed_Packages.html gpgkey: https://dl.iuscommunity.org/pub/ius/IUS-COMMUNITY-GPG-KEY gpgcheck: yes - name: install git yum: # 2 系は git2u というパッケージ名のようです。 name: git2u enablerepo: ius state: present # インストールした git のバージョンの確認、いちいちログインするのが面倒だったため # もっと良いやり方がありそうなので調査 - shell: git --version register: result - debug: var=result
これで vagrant up
、もし起動済みであれば vagrant provision
すれば git がインストールされます。
$ vagrant up Bringing machine 'simple' up with 'virtualbox' provider... ==> simple: Importing base box 'centos7.2'... ==> simple: Matching MAC address for NAT networking... ==> simple: Setting the name of the VM: ansible-playbook_simple_1493999707194_20088 ==> simple: Fixed port collision for 22 => 2222. Now on port 2200. ==> simple: Clearing any previously set network interfaces... ==> simple: Preparing network interfaces based on configuration... simple: Adapter 1: nat simple: Adapter 2: hostonly ==> simple: Forwarding ports... simple: 22 (guest) => 2200 (host) (adapter 1) ==> simple: Booting VM... ==> simple: Waiting for machine to boot. This may take a few minutes... simple: SSH address: 127.0.0.1:2200 simple: SSH username: vagrant simple: SSH auth method: private key simple: Warning: Remote connection disconnect. Retrying... simple: Warning: Connection reset. Retrying... simple: Warning: Remote connection disconnect. Retrying... simple: simple: Vagrant insecure key detected. Vagrant will automatically replace simple: this with a newly generated keypair for better security. simple: simple: Inserting generated public key within guest... simple: Removing insecure key from the guest if it's present... simple: Key inserted! Disconnecting and reconnecting using new SSH key... ==> simple: Machine booted and ready! ==> simple: Checking for guest additions in VM... simple: The guest additions on this VM do not match the installed version of simple: VirtualBox! In most cases this is fine, but in rare cases it can simple: prevent things such as shared folders from working properly. If you see simple: shared folder errors, please make sure the guest additions within the simple: virtual machine match the version of VirtualBox you have installed on simple: your host and reload your VM. simple: simple: Guest Additions Version: 4.3.30 simple: VirtualBox Version: 5.1 ==> simple: Setting hostname... ==> simple: Configuring and enabling network interfaces... ==> simple: Mounting shared folders... simple: /vagrant => /Users/sevengreenz/dev/ansible-playbook ==> simple: Running provisioner: ansible_local... simple: Installing Ansible... simple: Running ansible-playbook... PLAY [simple] ****************************************************************** TASK [Gathering Facts] ********************************************************* ok: [simple] TASK [install IUS repository] ************************************************** changed: [simple] TASK [install git] ************************************************************* changed: [simple] TASK [command] ***************************************************************** changed: [simple] TASK [debug] ******************************************************************* ok: [simple] => { "changed": false, "result": { "changed": true, "cmd": "git --version", "delta": "0:00:00.008418", "end": "2017-05-05 16:58:27.387269", "rc": 0, "start": "2017-05-05 16:58:27.378851", "stderr": "", "stderr_lines": [], "stdout": "git version 2.12.2", "stdout_lines": [ "git version 2.12.2" ] } } PLAY RECAP ********************************************************************* simple : ok=5 changed=3 unreachable=0 failed=0
上に書いた通りですが下記にあげています。
https://github.com/sevengreenz/ansible-php/tree/v0.1.1
memo
playbook.yml の検証
プロビジョニングはゲスト側にインストールした Ansible を使用しているのでホスト側にはいらないのですが、playbook の検証のためにいれました。
$ sudo easy_install pip $ sudo pip install ansible # playbook.yml 読み込みにシンタックスエラーが発生しないか検証 $ ansible-playbook -i /dev/null playbook.yml --syntax-check
-i
オプションはインベントリファイル(プロビジョニング先の設定)の指定ですが、Vagrant 側でやっているのでインベントリファイルはありません。指定しないと怒られるので /dev/null
にしています。
モジュールについて
Ansible はモジュールと呼ばれるものを提供していて、リソースの管理やコマンドの実行を行えるようです。 今回だと playbook.yml の yum_repojitory と yum がモジュールに当たります。 Ansible で何かする時はモジュール一覧を見て提供されていないか確認するのが良さそうです。使用例も結構あって使い方も乗ってます。