The most easiest way to setup Ansible on windows

This article is dedicated to Ansible lovers. Here, I am going to explain to setup Ansible for windows user as Ansible can only run on linux platform.

Lots of developer are finding difficult to use Ansible in windows machine therefore, I made this quick guide to setup Ansible in few minutes on windows.

In this article, I will cover following topics:

  • What’s vagrant and virtual machine
  • What is Ansible
  • How to create virtual machine using vagrant
  • How to connect created VMs
  • How to install Ansible on virtual machine
  • How to write first ansible playbook
  • How to run the ansible playbook

Prerequisite:

To do all in quick way, we need following tools to be installed on windows machine:

What’s vagrant and virtual Box

Vagrant: It’s a tool to create and manage virtual machine. Vagrant works with its own vagrantfile where we defined all the requirement to setup a virtual machine. It’s so powerful that it’s build multiple virtual machine in one way. We chose this tool because we need more than one virtual machine to simulate ansible actual structure. For more information visit https://vagrantup.com.

Virtual Box: Virtual Box is not a physical box like normal desktop but just it’s just a container to place any OS(Operating System) Image. This will allow us to have multiple OS as at a same time on same machine.

What is Ansible

Ansible is a solution tool for all who has hundreds of server and hard to maintain, configure and deploy their application on several servers.

Ansible is provisioning, configuration management and deployment tool which provide a consistent state of the machine across environments.

How to create virtual machine using vagrant

To setup Ansible , we need at least two virtual machine. One machine will be control node(to give/execute the instruction on target node) and another will target node.

To save the time, I have already written a vagrant file which can generate as many VMs based on the configuration. Below is the vagrantfile.

To run the above vagrantfile, you need to create a directory and place above file in that newly created directory. There is one variable in the script name ‘NODE_COUNT’ which is used to create different VMs. To run the vagrant script, run below command

After running above vagrant script, following things will be achieved

  • 2 VMs created with ‘geerlingguy/centos7’ image (one is ‘master’ and another is ‘node1’)
  • SSH key generation and deployment, so that control node(master) can access target node.
  • Create a user as ‘vagrant’
  • Set the private network between VMs
  • Map windows directory to the ‘/ansible’ in the VM
  • Install ansible latest version (command: sudo yum install -y ansible)
  • Master node ip address(172.16.255.10) and target node ip address (172.16.255.11)

How to connect created VMs

To connect the created VMs as master(control node) and node1(target node), I will use putty to connect master and check the ansible installation verification.

Enter master node ip addess to connect and click open
enter ‘vagrant’ as password to login
check VM private network to access target node from master node
command: ansible –version to check ansible version

How to install Ansible on virtual machine

To install ansible using command line in linux use below command

How to write first playbook

To write an ansible playbook, we need an inventory file where we specify all the target machines. There are several ways to define the target machine in the inventory file. ( click here to get help on inventory file ). I will follow below steps to create our first playbook which will ping our target node from the control node(master).

  • create a directory for ansible project
  • create a directory for inventory file under ansible project
  • create inventory file
  • create playbook
create the above shown structure for ansible project

Inventory file does have only one target node IP address entry as below

Ansible playbook to ping the inventory hosts

All ansible playbooks are yml file which have first line with ‘—‘ and data structure define as key/value pairs. Without ‘—‘ yml file wont parse by ansible. Yaml/yml is indentation sensitive therefore pay attention to write any yml file. All keys at same level must be aligned by indentation. e.g. key as (name, hosts, tasks) are having same indentation after define ‘-‘ and inside the tasks, name and ping as key at the same indentation level.

yaml/yml file key Description
namedefine the purpose of the task/object
hoststarget host, group to execute the task as defined in inventory file
tasksdata object to have collection of defined tasks
ping‘ping’ is a ping module to ping the ansible_host as supplied by ‘hosts’ variable and defined in inventory

How to run the ansible playbook

To run the ansible playbook, below command will be used along with the inventory file

-i is used to supply inventory file to parse the target machine to execute playbook tasks.

output of ansible playbook execution

PLAY RECAP shows the summery of the playbook. The playbook finished successfully as failed = 0.

Conclusion

Finally, this comes to end, we have successfully created 2 VMs and installed Ansible on the control node. Our first ansible playbook also ran successfully. Ansible setup completed succesfully.

Happy Ansible Learning ๐Ÿ™‚ ๐Ÿ™‚

share or like this post
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Share