Sunday 18 June 2017

Working With Ansible



Ansible Playbook contains mainly 4 sections.They are
1.Setup
2.Variables
3.Tasks
4.Handlers

Setup section contains the information about Host name(Which we are going to connect),User info(by which user account ansible should establish connection with Managed Machine),Sudo permissions,Connection is ssh or not.These type info will be available in Setup section.



Variables,Tasks and Handlers will be discussed in later posts.

After writing inventory file.we must check SSH connection between the controller machine(CM) and managed machines(MM).We must create SSH connection between CM and MM as follow
create a user name with test in all the 3 VMS and give visudo permissions to it or you can work with root also.Then from server1 generate SSH key and copy it to server2,server3 as follow.

[test@server1 ~]$ ssh-keygen

[test@server1 ~]$ ssh-copy-id server2.abc.com

[test@server1 ~]$ ssh-copy-id server3.abc.com

Now you setup a password less authentication for server1 to server2,server3.It makes to communicate server1 to server2,server3 very simple.

The following command checks ansible connection between CM and MM .Here "webservers" is group name defind in inventory file.Now i can start working with ansible.

This below command confirms the Controler machne and managed machines are having connection.
[test@server1 ~]$ ansible webservers -m ping

server3.abc.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
server2.abc.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

or i can ping individual server as follow

[test@server1 ~]$ ansible server2.abc.com -m ping


First Playbook

To write the playbook we have some rules to follow
The first line should start with ---
And every comment should start with #.These comments are not be executable.
Every item contains - symbol and space before it starts.The playbook follow a structer. If we doesn't follow that structer it will shows the errors. Every line should follow indentation and spaces at required places.It follows Key:Value Rule.

[test@server1 ansible]$ vi web.yml
To Run the playbook we use following command:

Using syntax check option  will checks the syntax of the playbook like the playbook following indentation and spacing correctly. Check option for dry run means it won't do anything on servers but displays the resulted output.

[test@server1 ansible]$ ansible-playbook web.yml --syntax-check
[test@server1 ansible]$ ansible-playbook web.yml --check 

[test@server1 ansible]$ ansible-playbook web.yml 

This command effects on the servers.output will be as follow

If you face any problem while practicing feel free to comment it and Bookmark this blog for quick reference.We will try to help you.

If you like this share with your friends. Follow us by email for our new posts 

Thanks

Devops Desk Team

No comments:

Post a Comment