This example shows us how to install composer globally with Ansible and Vagrant.


Structure


.
├── Vagrantfile
└── provisioning
├── hosts.yml
└── site.yml

Files


Vagrantfile


# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"

config.vm.define :vagrant do |vagrant|
vagrant.vm.hostname = "test"
vagrant.vm.network "private_network", ip: "192.168.99.30"
end

config.vm.provider :virtualbox do |virtualbox|
virtualbox.name = "test - 192.168.99.30 - 16.04"
end

config.vm.provision :ansible do |ansible|
ansible.raw_arguments = Shellwords.shellsplit(ENV["ANSIBLE_ARGS"]) if ENV["ANSIBLE_ARGS"]
ansible.verbose = "-vvv"
ansible.inventory_path = "provisioning/hosts.yml"
ansible.playbook = "provisioning/site.yml"
end
end

hosts.yml


all:
hosts:
vagrant:
ansible_host: 192.168.99.30
ansible_python_interpreter: /usr/bin/python3

site.yml


---
# This playbook sets up whole stack.

- name: Configurations to "vagrant" host
hosts: vagrant
remote_user: root
become: yes

vars:
installer: /tmp/installer.php

tasks:
# sudo apt-get install php-cli
- name: Install php-cli
apt:
name: php-cli
state: present
tags:
- composer

# curl -sSk https://getcomposer.org/installer -o /tmp/composer-installer.php
- name: Download composer
get_url:
url: https://getcomposer.org/installer
dest: "{{ installer }}"
tags:
- composer

# php /tmp/composer-installer.php
# sudo mv composer.phar /usr/local/bin/composer
- name: Install composer
command: "{{ item }}"
with_items:
- "php {{ installer }}"
- mv composer.phar /usr/local/bin/composer
tags:
- composer

# sudo rm /tmp/composer-installer.php
- name: Remove composer installer
file:
path: "{{ installer }}"
state: absent
tags:
- composer

- name: Print composer version
command: composer -v
register: version
- debug: msg="{{ version.stdout_lines }}"

Build


PLAY [Configurations to "vagrant" host] ****************************************

TASK [Gathering Facts] *********************************************************
ok: [vagrant]

TASK [Install php-cli] *********************************************************
ok: [vagrant]

TASK [Download composer] *******************************************************
changed: [vagrant]

TASK [Install composer] ********************************************************
changed: [vagrant] => (item=php /tmp/installer.php)
changed: [vagrant] => (item=mv composer.phar /usr/local/bin/composer)

TASK [Remove composer installer] ***********************************************
changed: [vagrant]

TASK [Print composer version] **************************************************
changed: [vagrant]

TASK [debug] *******************************************************************
ok: [vagrant] => {
"msg": [
" ______",
" / ____/___ ____ ___ ____ ____ ________ _____",
" / / / __ \\/ __ `__ \\/ __ \\/ __ \\/ ___/ _ \\/ ___/",
"/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /",
"\\____/\\____/_/ /_/ /_/ .___/\\____/____/\\___/_/",
" /_/",
"Composer version 1.6.3 2018-01-31 16:28:17"
]
}

PLAY RECAP *********************************************************************
vagrant : ok=7 changed=4 unreachable=0 failed=0