Running vagrant ansible provisioner on Windows/Cygwin
Tools Required
- Vagrant 2.x
- Cygwin
install necessary packages like python, python2-paramiko,python-crypto,python-setuptools,wget,openssh,gcc-g++ from cygwin installer.
open the cygwin terminal
$ /home/<username>
$ vi .bashrcexport PYTHONHOME=/usr
export PYTHONPATH=/usr/lib/python2.7
now lets install ansible from the cygwin terminal ( setup proxy if you are behind the proxy ), before that install pip
$ python /usr/lib/python2.7/site-packages/easy_install.py pip
$ pip install ansible
NOTE: if the above ansible installation fails, make sure to install the necessary packages as told above
lets create a ansible provisioner in vagrant
create a proper workspace ( folder )
create the box
$ vagrant box add ubuntu/trusty64
init the box
$ vagrant init ubuntu/trusty64
liftoff the vagrant :)
$ vagrant up
this will generate the necessary files and folder
a. Vagrantfile
b. .vagrant/
c. playbook.yml ( should be create to provision this on vagrant guest )
Vagrantfile
Vagrant.configure("2") do |config|
config.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
if File.file? (".vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory")
ansible.inventory_path = ".vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory"
end
end
end
playbook.yml
---
- hosts: all
become: true
tasks:
- name: Install NTP
apt: package=ntp,ntpdate state=present update_cache=yes
tags: ntp
- name: Make sure NTP is started up
service: name=ntp state=started enabled=yes
tags: ntp - name: install JDK 11
apt:
name: openjdk-7-jdk
state: present - name: install apache2
apt: name=apache2 update_cache=yes state=latest
- name: enabled mod_rewrite
apache2_module: name=rewrite state=present
notify:
- restart apache2
handlers:
- name: restart apache2
service: name=apache2 state=restarted
now provision your playbook on the vagrant guest
$ vagrant provision
Now login to the vagrant and check if the provisions happened or not
$ vagrant ssh
Check for the status of NTP, java, apache2
# service status apache
# java -version
lets provision using “ansible_local”.
Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.synced_folder ".", "/vagrant"
config.vm.provision "ansible_local" do |ansible|
ansible.become = true
ansible.playbook = "playbook.yml"
ansible.verbose = true
ansible.install = true
ansible.local = true
vagrant_synced_folder_default_type=""
end
end
playbook.yml
---
- hosts: all
user: vagrant
become: yes
vars:
ansible_port: '2222'
ansible_ssh_user: 'vagrant' tasks: - name: Make sure NTP is started up
become: yes
service: name=ntp state=started enabled=yes
tags: ntp - name: install JDK 11
become: yes
apt:
name: openjdk-7-jdk
state: present - name: install apache2
become: yes
apt:
name: apache2
state: latest - name: enabled mod_rewrite
become: yes
apache2_module: name=rewrite state=present
notify:
- restart apache2 handlers:
- name: restart apache2
service: name=apache2 state=restarted
ansible.cfg
[defaults]
host_key_checking = no[ssh_connection]
control_path_dir = "/tmp/ansible-ssh-%%h-%%p-%%r
ssh_args = -o ControlMaster=no -o PasswordAuthentication=no
spinp up the vagrant
$ vagrant up --provision
expected result
$ vagrant provision
==> default: Running provisioner: ansible_local...
default: Running ansible-playbook...
cd /vagrant && PYTHONUNBUFFERED=1 ANSIBLE_NOCOLOR=true ansible-playbook --limit="default" --inventory-file=/tmp/vagrant-ansible/inventory --becom
e -v playbook.yml
[WARNING] Ansible is being run in a world writable directory (/vagrant), ignoring it as an ansible.cfg source. For more information see https://
docs.ansible.com/ansible/devel/reference_appendices/config.html#cfg-in-world-writable-dir
Using /etc/ansible/ansible.cfg as config file
PLAY [all] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [default]
TASK [Make sure NTP is started up] *********************************************
ok: [default] => {"changed": false, "enabled": true, "name": "ntp", "state": "started"}
TASK [install JDK 11] **********************************************************
ok: [default] => {"cache_update_time": 1592218185, "cache_updated": false, "changed": false}
TASK [install apache2] *********************************************************
ok: [default] => {"cache_update_time": 1592218185, "cache_updated": false, "changed": false}
TASK [enabled mod_rewrite] *****************************************************
ok: [default] => {"changed": false, "result": "Module rewrite enabled"}
PLAY RECAP *********************************************************************
default : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
that’s all folks , take it easy !