Sample Ansible commands.

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

Ansible Host File

How to ping all servers in ansible inventory file.

[root@ansible-master ~]# ansible all -m ping

ansible 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

Ansible_host

Command to list servers included in dbservers group of the /etc/ansible/hosts file.

[root@ansible-master ~]# ansible dbservers –list-hosts

Ansible_DB

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”

Checking_Package_State

apache service should be started on all hosts under webservers groups.

[root@ansible-master ~]# ansible webservers -s -m service -a “name=httpd state=started”

Screenshot from 2016-07-27 16-45-15

Output of every ansible command will be in JSON format which can be latter used for reporting purpose.

Ansible facts

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

Screenshot from 2016-07-27 17-21-57

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/

Screenshot from 2016-07-27 17-32-40

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’

Screenshot from 2016-07-27 18-01-04

 

To get IP information via ansible filter.

[root@ansible-master facts]# ansible webservers -m setup -a ‘filter=ansible_*_ipv4′

Ansible_filter

 

To get kernal information via Ansible filter.

[root@ansible-master facts]# ansible webservers -m setup -a ‘filter=ansible_kernel’

Screenshot from 2016-07-27 18-17-31

This entry was posted in Uncategorised. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2015 Spring Systems. All rights reserved.