David Caulfield

Install Ansible on Windows Using Cygwin

1. Install Cygwin

Go to https://www.cygwin.com/install.html and download setup-x86_64.exe Run it

  • Select 'Install from Internet'
  • Choose root directory (default)
  • Choose pacakage directory to store installation files
  • Use System Proxy Settings
  • Select any mirror site to download

In 'Select Packages'

  • Select Category dropdown and search for lynx
  • Go to All -> Web -> lynx: A text-based Web Browser
  • Select latest version
  • Click next to complete installation

2. Install apt-cyg

This is a package manager for cygwin

To install:

  • lynx -source rawgit.com/transcode-open/apt-cyg/master/apt-cyg > apt-cyg
  • install apt-cyg /bin

3. Install dependencies for ansible

To install altogether

  • apt-cyg install binutils curl gcc-core gmp libffi-devel libgmp-devel make python python-crypto python-openssl python-setuptools python-devel git nano openssh openssl openssl-devel

You can also install individually by using

  • apt-cyg install  or by using the installation GUI

4. Install Ansible

To install

  • easy_install-2.7 pip
  • pip install ansible -vvv
  • Here, the -vvv is used in case the install seems too slow. It will show you if everything is still working.

5. Test ansible

To test installation

  • ansible
  • Should receive list of options

Some configuration

Cygwin comes with some strange default settings, particularly if using vi or vim.

Open a .virc file.

  • vi ~/.virc

Type in

set nocompatible
set backspace=2 

Run a playbook with Ansible

Make a new directory to test ansible with a simple playbook.

mkdir ansible-lab
cd ansible-lab

Ansible needs some configuration to work on a Windows machine. Our Windows machine is called the 'control' machine. Ansible does not officially support this.

Create an ansible configuration file.

vi ansible.cfg

Copy and paste the following into the file.

[ssh_connection]
ssh_args = -o ControlMaster=no

Now we will create our ansible playbook to test.

vi playbook.yml

Copy the following into the file.

- hosts: all   
  sudo: yes   
  tasks:   
    - name: install apache2  
      apt: name=apache2 update_cache=yes state=latest   

Bring up a virtual machine

We need a virtual machine to run this against. For this we will use a Vagrant box. Don't worry if you have not used Vagrant before. It is an easy way of installing virtual boxes on your machine and managing them.

We need a Vagrantfile to bring up our virtual machine.

vi Vagrantfile

Copy and paste the following.

Vagrant.configure("2") do | config|  
  config.vm.box = "bento/ubuntu-18.04"  
  config.vm.network "private_network", ip: "192.168.33.10"  
end

This will bring up a ubuntu virtual machine to run our ansible script against.

To bring up your virtual machine, make sure you are in the same directory as your Vagrantfile and type

vagrant up

Depending on your connection, this may take a little while to boot up.

Once your machine is booted (you can check if it was successfully booted by typing vagrant status), you need to make sure your control / Windows machine can connect to it.

Setup ssh connection

On your Cygwin terminal, generate an ssh -key.

ssh-keygen

Keep pressing enter until it is finished. Print out the .pub file where it saves the key. For example, mine looks like the following. Yours will be in the .ssh folder also.

cat /home/abcd/.ssh/id_rsa.pub

Copy the value that is printed out. Now you need to go to your vagrant machine.

vagrant ssh
vi .ssh/authorized_keys

Go to the end of the file, go to the next line (by pressing enter) and paste in your key from the id_rsa.pub file

Exit your vagrant machine.

exit

Test your ssh connection with the following.

ssh vagrant@192.168.33.10

You should be able to login to the virual machine without any prompt or failure.

Exit the machine once again.

exit

Run your ansible playbook

Make sure you are in the same directory as your ansible script. Ideally, you have your Vagrantfile, playbook.yml and ansible.cfg all in the same directory.

Ensure ansible is ready to go.

ansible-playbook --version

Now type the following.

ansible-playbook -i vagrant@192.168.33.10, playbook.yml

Your tasks should appear in a list. Once finished, the PLAY RECAP should have ok=2 and changed=1.

Congrats, you have just run your first ansible playbook!


10 kudos