Giter Site home page Giter Site logo

ansible-practice's Introduction

ansible-practice

What's in this repo?

  • Commands to install ansible on Red Hat Enterprise Linux (RHEL)
  • A files with ad-hoc ansible commands to perform simple tasks like ping
  • 2 playbooks that have 1 tasks each. 1 uses the copy module and 1 uses the shell module
  • 1 playbook that calls a role that's contained in copy-role folder

I'm doing this lab on AWS environment with Amazon EC2's where password auhencation is disabled by default

Lesson 1: Install Ansible on RHEL. We will call this server Ansible-master

  1. Become the root user.
sudo su - 
  1. Download the EPEL repo
rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

Note: if you get an error for a conflict, you already have epel downloaded

Enter this to enable the repo

yum-config-manager --enable epel
  1. Install Ansible
yum install ansible -y
    • Add a user named ansible
sudo useradd ansible
  1. Add a password to the user and choose a password of your choice
sudo passwd ansible
  • Enable password authentication. To do this open tis file
sudo vi /etc/ssh/sshd_config 
  • Then uncomment the line that says PasswordAuthentication yes and comment the line that says PasswordAuthentication no :
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication yes
#PermitEmptyPasswords no
#PasswordAuthentication no
  • Restart the sshd daemon on the master machine
sudo service sshd restart

Note: by default ansible may need to setup the environment before we can use /etc/ansible. Use the link to find alternatives for host , config and inventory files https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html

  1. Create/Edit the hosts file for ansible and add the slave Ip.
sudo vi inventory.ini
  1. Inside the file add these 3 lines. Let's give the group ( in this case only 1 instance ) a name . This is on top of the IP of the slave enclosed in the square brackets.
[webservers]
10.0.0.5
127.0.0.1
  1. Open the sudoers file
sudo visudo
  1. Add this line. There is a line similar to this already for the wheel group . Put it right under it. This makes sure that the ansible user can call all commands without the need of password
ansible         ALL=(ALL)       NOPASSWD: ALL
  1. Open the /etc/ansible/ansible.cfg file
sudo vi /etc/ansible/ansible.cfg
  1. Find and Uncomment this line inside the file. THis will disable ssh key check
# uncomment this to disable SSH key host checking
host_key_checking = False
  1. Install SSHPASS . Without this you may not able to use ssh with password
sudo yum --enablerepo=epel -y install sshpass

Lesson 2: On another RHEL/centos machine which we will call Ansible-slave

  • Become root user
sudo su -
  • Add a user named ansible
sudo useradd ansible
  • Add a password to the user and choose a password of your choice
sudo passwd ansible
  • Enable password authentication. To do this open tis file
sudo vi /etc/ssh/sshd_config 
  • Then uncomment the line that says PasswordAuthentication yes and comment the line that says PasswordAuthentication no :
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication yes
#PermitEmptyPasswords no
#PasswordAuthentication no
  • Restart the sshd daemon on the Slave machine
sudo service sshd restart
  • Try logging into the slave machine from the Ansible master. you should be able to
  • Open the sudoers file on the slave machine
sudo visudo
  • Add this line. There is a line similar to this already for the wheel group . Put it right under it. This makes sure that the ansible user can call all commands without the need of password
ansible         ALL=(ALL)       NOPASSWD: ALL
  • From the Ansible master ping the slave server:
ansible -m ping all -u ansible --ask-pass -i inventory.ini
  • you should see a return like this
172.31.10.247 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

**Congratulations , you are able to ping the slave server using the ping module!

  • Now, let's install apache web server on the slave server.
ansible all  -m yum -a "name=httpd state=present" -u ansible --ask-pass --become

**In the above command -m stands for module , in this case we are using the yum module. -a is for arguements where we pass the the name of the package and state. present means install. Similarly if you run the command and say state=absent then the web server will be removed. --become is used to be the root user with sudo

  • Use the service module to start the apache web server
ansible all  -m service -a "name=httpd state=started " -u ansible --ask-pass --become 
  • Congratulations! your web server should be installed now. Browse the public IP of the slave server on a browser to verify. You should see the apache web server test page.

apache web server

Lesson 3 : Using ansible playbooks

  1. Write a "hello. This is my webserver" line and save it to a file called index.html . This will replace our webserver default page.
echo "hello. This is my webserver" > index.html
  1. Create a new yaml file with vi editor and call it copy.yml
vi copy.yml
  1. copy and paste this to the file and save
- hosts: all
  become: yes
  tasks:
    - copy:
        src: /home/ansible/index.html
        dest: /var/www/html/index.html
        owner: ansible
        group: ansible
        mode: 0655
  1. Use the ansible playbook command to run the playbook. As you can see above, the task has a copy module that will copy the index.html to the remote machine.
ansible-playbook copy.yml -u ansible --ask-pass
  1. You should get a success response. Check your web server now. It should display the contents of index.html that you copied to the slave machine.

ansible-practice's People

Contributors

ravsau avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.