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.
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
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
b. Check memory usage on all remote hosts.
# ansible -m command -a "free -mt" web-servers
c. Checking Uptime for all 3 servers.
# ansible -m command -a "uptime" web-servers
d. Check for hostname and Architecture.
# ansible -m command -a "arch" web-servers # ansible -m shell -a "hostname" web-servers
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
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/
Excellent article on Ansible for starters!
It’s a really interesting article on Ansible…
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?
Use
--ask-pass
command will ask to enter the password.is not a right one
Replace with below mentioned command:
Hi, missing only Chapter 4, when I click does not open the link for the rest of the tutorial.
Hi, I am getting below error when I try to install ansible on Linux.
Can someone please help..
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
@Manikandan,
I think you first install python-cryptography manually using PIP tool as shown.
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.
Issue got resolved by below command after keygen in host1
ssh-copy-id
then run the ansible
ansible -m command -a “df -h ” webservers
I was not able to install ansible in my REDHAT 7, after running the below commands;
Return output
#8 command should be -> ansible all -m ping
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
@Rajesh,
you can try from below link, FYI it’s good to install from RPM.
http://docs.ansible.com/ansible/latest/intro_installation.html#running-from-source
If you have a proxy export it as follows your server will get internet access.
Just run below commands in terminal.
For HTTP URL: export http_proxy=http://xxx.xxx.xxx.xxx:3128
For HTTPS URL: export https_proxy=https://xxx.xxx.xxx.xxx:3128
Replace xxx with your Proxy IP and instead of 3128 replace with your proxy port.
Thanks & Regards,
Babin Lonston
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
}
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
@Arun,
Have you tried ssh-add, ssh-agent?
Regards,
Babin Lonston
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.
Hi, I created script that installa Ansible on SuSe SLES11
https://github.com/kmonticolo/Ansible-on-SLES11
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
make sure the ansible directory was created under /etc
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
Noticed a possible typo – shouldn’t the prerequisite “parmiko” be “paramiko”? As in python-paramiko package (or pip install paramiko)?
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.
@Nakul,
Are you sure the /export/home/akumar3 exists on the remote server? Please confirm..
Hi Ravi,
I don’t find /export/home/akumar3 in remote server.Only /export/home/ is available.
@Nakul,
Then create that user’s home directory and try the command again, it will work for sure.
Thanks Ravi.
Its working.
No this /export/home/akumar3 doean’t exist in remote server.
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
@Erick Bazan,
Please run the ansbile from (user) were you create SSH keygen for remote servers. It will resolve this issue.
Regards
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
Sampada,
Try to execute the same command with the user that ssh keys generated
it was really interesting…
Thanks, very useful for a new user of ansible….
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
@Daniel
Have you listed all hostname or IPs under /etc/ansible/hosts ?
Excellent, nice job! :)
Thanks
Can’t install from PPA on debian, when do ‘apt-get update’ get:
‘Failed to fetch http://ppa.launchpad.net/ansible/ansible/ubuntu/dists/wheezy/main/binary-amd64/Packages 404 Not Found’
even though ‘sudo apt-add-repository ppa:ansible/ansible -y’ was successful
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.
Excellent Tutorial. Thanks Tecmint
Nice and clear.
I’ll be waiting for next post about ansible.
Thanks.
I love to read tecmint.com articles. …
Thankssss
For
Great
Great
Knowledge. ..
Waiting for 2nd & 3rd part…..
Please upload soon…..
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.
can you cable to ping using
# sudo ansible -i /etc/ansible/hosts -m ping web-servers
Let me know..
may be you have given a different host name i.e web-servers. For me i gave webservers as my host name. Please check your host name
Nice writeup. Looking forward for part-2 with WordPress installation.
So what happened to part 2 ?
There are three parts pending to post, Will post
Thanks for the clear walkthrough. I am looking forward to part 2.
Nice article. easy to understand.
Thanks for your valuable comments, Keep watching more article about Ansible soon.