Running Ansible commands on remote hosts.
Ansible default inventory file is /etc/ansible/hosts which keeps list of all hosts. You can setup number of hosts within groups.
tail -n 15 /etc/ansible/hosts
How to ping all servers in ansible inventory file.
[root@ansible-master ~]# ansible all -m ping
Ansible all command will list all the hosts in /etc/ansible/host file, does connect to the remote servers using your current user name, over SSH.
[root@ansible-master ~]# ansible all –list-hosts
Command to list servers included in dbservers group of the /etc/ansible/hosts file.
[root@ansible-master ~]# ansible dbservers –list-hosts
Ensuring if bash package has been installed on all servers under dbservers group but don’t update it:
[root@ansible-master ~]# ansible dbservers -s -m yum -a “name=bash state=present”
apache service should be started on all hosts under webservers groups.
[root@ansible-master ~]# ansible webservers -s -m service -a “name=httpd state=started”
Output of every ansible command will be in JSON format which can be latter used for reporting purpose.
Ansible facts are a way of collecting information about remote machines for playbook variables known as facts. We always setup module in Ansible to explore wide array of system information or custom facts modules can also be written. However, we can use simple way to provide systems or user specific information for Ansible variables.
Such As To list the available facts of machine, you can call the “setup” module.
[root@ansible-master ~]# ansible -m setup localhost
Facts can also be dumped from desired hosts and save them followed by hostname on target location.
[root@ansible-master facts]# ansible -m setup webservers –tree /tmp/facts/
Ansible filters are medium to find exact information that match this shell-style facts Such as you might want to determine the used/free memory/swap on systems and even if configuration value on another system.
[root@ansible-master facts]# ansible webservers -m setup -a ‘filter=ansible_*_mb’
To get IP information via ansible filter.
[root@ansible-master facts]# ansible webservers -m setup -a ‘filter=ansible_*_ipv4′
To get kernal information via Ansible filter.
[root@ansible-master facts]# ansible webservers -m setup -a ‘filter=ansible_kernel’