Installing and Configuring Ansible

Installing and Configuring Ansible


1. Enable EPEL Repository

     wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
     # rpm -ivh epel-release-6-8.noarch.rpm


2. Install Ansible

     sudo yum install ansible

3. Setup Passwordless access to remote machines

       ssh-keygen -t rsa -b 4096
       ssh-copy-id essadmin@10.201.32.127

   Now test pass-wordless access.

4. Creating Inventory File for Remote Hosts with below entry 

          sudo vim /etc/ansible/hosts
          [webservers]
          10.201.32.127 

5. Test ansible using the ping module with -m option

          $ ansible -m ping webservers
             10.201.32.127 | SUCCESS => {
                    "changed": false,
                          "ping": "pong"
                    }


           $ ansible -m ping all
               10.201.32.127 | SUCCESS => {
               changed": false,
               "ping": "pong"
            }
  


6. Use command module to execute any command on remote hosts

     $ ansible -m command -a "w" webservers
         10.201.32.127 | SUCCESS | rc=0 >>
         06:38:52 up 53 days, 12:18,  2 users,  load average: 0.02, 0.08, 0.14


7. Using file module to create files and directories

    $ ansible -m file -a "dest=/home/essadmin/testing mode=755  state=directory" webservers
         10.201.32.127 | SUCCESS => {
                changed": true,
                      "gid": 510,
                  "group": "essadmin",
                  "mode": "0755",
                 "owner": "essadmin",
                    "path": "/home/essadmin/testing",
                     "size": 4096,
                    "state": "directory",
                       "uid": 510
           }
 

  To remove the directory created above
         
     $ ansible -m file -a "dest=/home/essadmin/testing mode=755  state=absent" webservers
            10.201.32.127 | SUCCESS => {
                       "changed": true,
                       "path": "/home/essadmin/testing",
                       "state": "absent"
                    }


8. To execute command which requires administrative privilege , we can use -b option to run commands with privilege escalation.

      $ ansible -m file -a "dest=/home/essadmin/testfile mode=755 owner=root group=root state=directory" -b  webservers
10.201.32.127 | SUCCESS => {
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/home/essadmin/testfile",
    "size": 4096,
    "state": "directory",
    "uid": 0
}


Comments

Popular Posts