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:
- Vagrant (https://www.vagrantup.com/downloads.html)
- Oracle Virtual Box (https://www.virtualbox.org/wiki/Downloads)
- Mobaxterm( https://mobaxterm.mobatek.net/download.html )/putty ( https://www.putty.org/ ) to connect VM based on their ip and user as vagrant
- Internet Connection to download runtime package
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
BOX_IMAGE = "geerlingguy/centos7" NODE_COUNT = 2 #global script to setup ssh between hosts. $global = <<SCRIPT #check for private key for vm-vm comm #echo 'http_proxy="http://xxxxxxxxx:3128"' > /etc/environment #echo 'https_proxy="http://xxxxxxxxx:3128"' >> /etc/environment #echo 'proxy=http://xxxxxxxxx:3128' >> /etc/yum.conf #echo 'no_proxy=172.16.255.*' >> /etc/environment [ -f /vagrant/id_rsa ] || { ssh-keygen -t rsa -f /vagrant/id_rsa -q -N '' } #deploy key [ -f /home/vagrant/.ssh/id_rsa ] || { cp /vagrant/id_rsa /home/vagrant/.ssh/id_rsa chmod 0600 /home/vagrant/.ssh/id_rsa } #allow ssh passwordless grep 'vagrant@node' ~/.ssh/authorized_keys &>/dev/null || { cat /vagrant/id_rsa.pub >> ~/.ssh/authorized_keys chmod 0600 ~/.ssh/authorized_keys } #exclude * from host checking cat > ~/.ssh/config <<EOF Host * StrictHostKeyChecking no UserKnownHostsFile=/dev/null EOF chmod 600 ~/.ssh/config #end script SCRIPT unless Vagrant.has_plugin?("vagrant-vbguest") raise 'vagrant-vbguest plugin is not installed!' end Vagrant.configure("2") do |config| # Our laptops are slow config.vm.boot_timeout = 900 # don't need to update # config.vbguest.no_install = true config.vm.provider :virtualbox do |vb| # This makes sure we pass DNS requests back to the host. vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"] end config.vm.define "master" do |subconfig| subconfig.vm.box = BOX_IMAGE subconfig.vm.hostname = "master" subconfig.vm.network :private_network, ip: "172.16.255.10" subconfig.vm.provider :virtualbox do |vb| unless File.exist? ("storage/master_extra1.vdi") vb.customize ['createmedium', '--filename', 'storage/master_extra1', '--size', '2048'] end vb.customize ['storageattach', :id, '--storagectl', 'IDE Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', 'storage/master_extra1.vdi'] end end (1..NODE_COUNT).each do |i| config.vm.define "node#{i}" do |subconfig| subconfig.vm.box = BOX_IMAGE subconfig.vm.hostname = "node#{i}" subconfig.vm.network :private_network, ip: "172.16.255.#{i + 10}" # subconfig.vm.network "public_network", ip: "192.168.0.#{i + 10}" subconfig.vm.provision "shell", inline: <<-SHELL sudo yum install -y unzip gsl SHELL # Add a extra disk to play with your ansible code....in case needed. subconfig.vm.provider :virtualbox do |vb| unless File.exists? ("storage/node#{i}_extra1.vdi") vb.customize ['createmedium', '--filename', "storage/node#{i}_extra1", '--size', '2048'] end vb.customize ['storageattach', :id, '--storagectl', 'IDE Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', "storage/node#{i}_extra1.vdi"] end end end # first path '../' to look a dir to map in VM, that could be anything in place of ansible and VM will create the same mount(/ansible) config.vm.synced_folder "../", "/ansible", type: "nfs", mount_options: ["dmode=775,fmode=600"] config.vm.provision "shell", privileged: false, inline: $global config.vm.box = "geerlingguy/centos7" config.vm.network "public_network", ip: "192.168.0.17" config.vm.provision "shell", inline: <<-SHELL #echo 'http_proxy="http://xxxxxxxxx:3128"' > /etc/environment #echo 'https_proxy="http://xxxxxxxxx:3128"' >> /etc/environment #echo 'proxy=http://xxxxxxxxx:3128' >> /etc/yum.conf echo 'no_proxy=172.16.255.*' >> /etc/environment sudo yum install -y ansible pip SHELL #config.vm.provision hosts, sync_hosts : true end |
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
1 2 |
$cd <vagrantfile_dir> <vagrantfile_dir>$vagrant up |
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.
How to install Ansible on virtual machine
To install ansible using command line in linux use below command
1 |
sudo yum install -y ansible |
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
Inventory file does have only one target node IP address entry as below
1 |
172.16.255.11 |
Ansible playbook to ping the inventory hosts
1 2 3 4 5 6 |
--- - name: first playbook to ping the target machine hosts: all tasks: - name: ping the target machine which is define in the inventory file ping: |
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 |
name | define the purpose of the task/object |
hosts | target host, group to execute the task as defined in inventory file |
tasks | data 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
1 |
ansible-playbook -i inventory/inventory ansible_playbook_ping.yml |
-i is used to supply inventory file to parse the target machine to execute playbook tasks.
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 ๐ ๐