How to Install and Configure ‘Ansible’ Automation Tool for IT Management – Part 1

Step 3: Creating Inventory File for Remote Hosts

Inventory file, This file hold the host information’s like which host we need to get connect from local to remote. Default inventory file will be under /etc/ansible/hosts.

7. Now let’s add these three hosts to inventory file. Open and edit file using your favourite editor, Here I use vim.

# sudo vim /etc/ansible/hosts

Add the following three hosts IP address..

[web-servers]
192.168.0.112
192.168.0.113
192.168.0.114

Note: The ‘web-servers‘ in the brackets indicates as group names, it is used in classifying systems and deciding which systems you are going to controlling at what times and for what reason.

Create Ansible Inventory File
Create Ansible Inventory File

8. Now time to check our all 3 server by just doing a ping from my localhost. To perform the action we need to use the command ‘ansible‘ with options ‘-m‘ (module) and ‘-all‘ (group of servers).

# ansible -m ping web-servers

OR

# ansible -m ping -all

Ping Remote Hosts
Ping Remote Hosts

In the above example, we’ve used ping module with Ansible command to ping all remote hosts at ones, the same way there are various modules can be used with Ansible, you can find available modules from ansible Official site here.

9. Now, here we are using another module called ‘command‘, which is used to execute list of commands (like, df, free, uptim, etc.) on all selected remote hosts at one go, for example watch out few examples shown below.

a. To check the partitions on all remote hosts

# ansible -m command -a "df -h" web-servers
Check Disk Space on all Hosts
Check Disk Space on all Hosts

b. Check memory usage on all remote hosts.

# ansible -m command -a "free -mt" web-servers
Check Memory on all Hosts
Check Memory on all Hosts

c. Checking Uptime for all 3 servers.

# ansible -m command -a "uptime" web-servers
Check uptime on all Hosts
Check uptime on all Hosts

d. Check for hostname and Architecture.

# ansible -m command -a "arch" web-servers
# ansible -m shell -a "hostname" web-servers
Check hostname on all Hosts
Check hostname on all Hosts

e. If we need the output to any file we can redirect as below.

# ansible -m command -a "df -h" web-servers > /tmp/df_outpur.txt
Redirect Output to File
Redirect Output to File

Like this way, we can run many shell commands using ansible as what we have run the above steps.

Conclusion

Okay, We can see how to in next article.

Ansible is a Powerful IT automation tool which is must every sysadmins for deploying applications and managing server’s at one go. Among any other automation tool such as puppet, Capistrano, salt, Ansible is quit very interesting and very easy to setup for production environment. Capistrano oh no i feel headache please leave me alone :p this what i used to say.

Ansible use only SSH as there agent. We don’t have to install and run any agent in the remote servers. Hope this article will be interesting one for you too. In our next article, I will show you how to setup the directory structure for Ansible deployment and creating playbooks and working with it.

Till then keep on tracking us to get updated articles and don’t forget to tell us your opinions on the Ansible and also tell us do you use any other automation tool which is more powerful than Ansible….

Reference Links

http://www.ansible.com/get-started
http://docs.ansible.com/

Babin Lonston
I'm Working as a System Administrator for last 10 year's with 4 years experience with Linux Distributions, fall in love with text based operating systems.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

53 thoughts on “How to Install and Configure ‘Ansible’ Automation Tool for IT Management – Part 1”

  1. I don’t want to use ssh-key for ansible, what I need is I always need to use passwords for each and every commands

    how to configure that?

    Reply
  2. Hi, I am getting below error when I try to install ansible on Linux.

     
    Error: Package: python-paramiko-2.1.1-0.4.el7.noarch (epel)
               Requires: python2-pyasn1
    Error: Package: python-paramiko-2.1.1-0.4.el7.noarch (epel)
               Requires: python-cryptography
    

    Can someone please help..

    Reply
  3. Error: Package: ansible-2.4.2.0-1.el7.noarch (epel)
    Requires: python-jinja2

    Ansible installation issue
    =====================
    Error: Package: python-paramiko-2.1.1-0.2.el7.noarch (epel)
    Requires: python-cryptography
    You could try using –skip-broken to work around the problem
    You could try running: rpm -Va –nofiles –nodigest

    https://centos.pkgs.org/7/epel-testing-x86_64/python-paramiko-2.1.1-0.4.el7.noarch.rpm.html

    when I have tried to install the ansible, after enable the repo also I am getting the following error

    Reply
    • @Manikandan,

      I think you first install python-cryptography manually using PIP tool as shown.

      $ sudo pip install pycrypto
      
      Reply
  4. Faced some issues while installing Ansible in linux. Got resolved by installing below packages.

    error: Failed dependencies:
    python-httplib2 is needed by ansible-2.4.1.0-2.el6.noarch
    python-jinja2-26 is needed by ansible-2.4.1.0-2.el6.noarch
    python-keyczar is needed by ansible-2.4.1.0-2.el6.noarch
    python-setuptools is needed by ansible-2.4.1.0-2.el6.noarch
    python-six is needed by ansible-2.4.1.0-2.el6.noarch
    sshpass is needed by ansible-2.4.1.0-2.el6.noarch

    Downloaded separately all the rpms from rpmfind.net and installed.

    Reply
  5. Issue got resolved by below command after keygen in host1

    ssh-copy-id

    then run the ansible
    ansible -m command -a “df -h ” webservers

    Reply
  6. I was not able to install ansible in my REDHAT 7, after running the below commands;

    $ sudo yum install ansible -y
    $ wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-10.noarch.rpm
    $ sudo rpm -ivh epel-release-7-10.noarch.rpm
    

    Return output

    Loaded plugins: amazon-id, rhui-lb, search-disabled-repos
    epel/x86_64/metalink                                                                                                                                                                      |  18 kB  00:00:00
    epel                                                                                                                                                                                      | 4.3 kB  00:00:00
    (1/3): epel/x86_64/group_gz                                                                                                                                                               | 170 kB  00:00:00
    (2/3): epel/x86_64/updateinfo                                                                                                                                                             | 840 kB  00:00:00
    (3/3): epel/x86_64/primary_db                                                                                                                                                             | 4.8 MB  00:00:00
    No package ansible available.
    
    Reply
  7. Hello Team,

    On Production server, We can’t provide internet access to server. I have download ansible.tar file. Can you suggest me the steps to install ?

    Regards,
    Rajesh kathar

    Reply
  8. 192.168.0.25 | UNREACHABLE! => {
    “changed”: false,
    “msg”: “Failed to connect to the host via ssh: Permission denied (publickey,password).\r\n”,
    “unreachable”: true
    }
    192.168.4.23 | UNREACHABLE! => {
    “changed”: false,
    “msg”: “Failed to connect to the host via ssh: ssh: connect to host 192.168.4.23 port 22: Connection timed out\r\n”,
    “unreachable”: true
    }

    Reply
    • I got the same error. I fixed it by
      1) installing python on remote servers
      2) Adding the following line to /etc/ansible/hosts ( my user is shivaji )
      ansible_user=shivaji
      [web-servers]
      192.168.1.200
      192.168.1.201

      Reply
    • my be your network (server )is not reachable or you havent copy ssh-key for password less login or defined user havent any key to that server.

      Reply
  9. Hi ,

    I am trying to create /etc/ansible/hosts file but it is not allowing me to either touch this file or save this file

    [root@localhost ~]# ansible –version
    ansible 2.2.0.0
    config file =
    configured module search path = Default w/o overrides
    [root@localhost ~]# touch /etc/ansible/hosts
    touch: cannot touch ‘/etc/ansible/hosts’: No such file or directory

    when i am doing vim /etc/ansible/hosts it is giving below error whien i am trying to save
    “/etc/ansible/hosts”
    “/etc/ansible/hosts” E212: Can’t open file for writing

    Is this file created by default or we need to create manually

    Reply
  10. Thanks for this post., I am looking for installation of ansible from the servers which is not exposed to internet. I downloaded the ansible tar.gz from git hub and ran make command but getting error as below
    ansible –version
    Traceback (most recent call last):
    File “/home/jboss/Ansible/ansible-2.1.1.0/bin/ansible”, line 44, in
    import ansible.constants as C

    I tried adding the env variables like below but again seeing exception
    ANSIBLE=/home/xxx/Ansible/ansible-2.1.1.0
    export PATH=$PATH:$ANSIBLE/bin
    export PYTHONPATH=$ANSIBLE/lib
    export ANSIBLE_LIBRARY=$ANSIBLE/library

    Error:
    ansible:85: DeprecationWarning: BaseException.message has been deprecated as of Python 2.6
    if e.message.endswith(‘ %s’ % sub):
    ERROR! Unexpected Exception: No module named yaml
    the full traceback was:

    Traceback (most recent call last):
    File “/home/jboss/Ansible/ansible-2.1.1.0/bin/ansible”, line 81, in
    from ansible.cli.adhoc import AdHocCLI as mycli
    File “/home/jboss/Ansible/ansible-2.1.1.0/lib/ansible/cli/__init__.py”, line 27, in
    import yaml
    ImportError: No module named yaml

    Kindly suggest to prepare install the prerequisites and ansible in my situation

    Reply
  11. Noticed a possible typo – shouldn’t the prerequisite “parmiko” be “paramiko”? As in python-paramiko package (or pip install paramiko)?

    Reply
  12. Hi,
    When i tried to copy remote server to ssh, then i got below error.

    ]$ ssh-copy-id 10.247.146.163
    /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
    /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed — if you are prompted now it is to install the new keys
    [email protected]‘s password:

    Could not chdir to home directory /export/home/akumar3: No such file or directory

    Please suggest.

    Reply
  13. hi, excellent post, just having a doubt here, I’m getting the same error sampada does, I already tried with the user the ssh keys generated, any idea what might be happening? thanks

    Reply
    • @Erick Bazan,

      Please run the ansbile from (user) were you create SSH keygen for remote servers. It will resolve this issue.

      Regards

      Reply
  14. Hi I am trying to set up a ansible lab on azure cloud and trying to get hand on to replace puppet actually.
    But when I m trying to run : [ ansible -m ping web-servers ]
    I am getting a error as : as mentioned below in-between [ ]

    [
    10.1.0.17 | UNREACHABLE! => {
    “changed”: false,
    “msg”: “Failed to connect to the host via ssh.”,
    “unreachable”: true
    }

    ]

    So I tried command : [ ansible -m ping web-servers:2222 ]
    But i am still getting the error as : [ [WARNING]: provided hosts list is empty, only localhost is available ]

    Can somebody help me please

    Thank you
    Sampada

    Reply
  15. Hello. I am trying to setup ansible on my home network for testing and mabey replace puttet if i like anisble.
    BUT! I have copied the key from my anisble host to my test vm, added the ip for my test vm in sudo vim /etc/ansible/hosts under [web-servers]
    but when i am trying to ping the test vm from anisble then i get this message ?

    # ansible -m ping web-servers
    # No hosts matched

    Reply
  16. I am getting error after executing
    ansible -m ping ‘webservers’
    SUDO password:
    192.168.1.239 | FAILED => SSH Error: Permission denied (publickey,password).
    while connecting to 192.168.1.239:22
    It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.

    But ssh [email protected] is working fine. Can you please help.

    Reply
  17. I love to read tecmint.com articles. …

    Thankssss
    For
    Great
    Great
    Knowledge. ..

    Waiting for 2nd & 3rd part…..
    Please upload soon…..

    Reply
  18. i configured ansible server but i have some proble. When i ma using “sudo ansible -m ping web-servers” this command
    , i got the error “No hosts matched”
    I am configured “/etc/ansible/hosts” file properly.

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.