Ansible Playbook for Get Hosts Information | Ansible Playbook Tutorial

Linuxtopic
0










ansible, ansible tutorial, ansible certification, ansible tags, ansible tasks, ansible modules, ansible debug, ansible inventory, ansible hosts file, ansible facts
Ansible Playbook -  Get System Information

tags: ansible, ansible tutorial, ansible certification, ansible tags, ansible tasks, ansible modules, ansible debug, ansible inventory, ansible hosts file, ansible facts















In this absible playbook example we will write a ansible tasks using a gathering facts of ansible hosts. we use ansible facts as variable and echo there value.

out ansible playbook will display hostname, date and timezone, network information, os information, kernel version and Hardware information of hosts.







To help or how to use ansible-playbook command,  use -h option


ansible-playbook -h





We define all hosts of inventory using "all" keyword "Click Here for more Information" then we define plays and write our first ansible tasks "- name: hostname" then  use ansible debug module to print value.



To Create a yaml file with suitable name 


cd /etc/ansible


vi system_info.yml

TASK 1

---

 - hosts: all

   tasks:

   - name: hostname

     debug:

       var=ansible_hostname







You may check ansible first task by sava this ansible playbook yml file and execute / run using ansible-playbook command.



First we will check syntax error using --check-syntax flags


ansible-playbook  --check-syntax system_info.yml

To run / execute ansible playbook


ansible-playbook system_info.yml

ansible, ansible tutorial, ansible certification, ansible tags, ansible tasks, ansible modules, ansible debug, ansible inventory, ansible hosts file, ansible facts

TASK - 2



ansible tasks :  we will print system date and  timezone of hosts. we define ansible variable  and use debug module. 



   - name: date and time

     vars:

      msg: |

       Date: {{ ansible_date_time.date }}

       Timezone: {{ ansible_date_time.tz }}

     debug:

      msg: "{{ msg.split('\n') }}"





TASK 3



Ansible Tasks : we will print system network information. we define ansible variable like ansible_interfaces to print all Interface of system,  ansible_all_ipv4_addresses to Print all ipv4,  ansible_default_ipv4 to Print default Gateway and ansible_eth0 to Print eth0 MAC Address.




   - name: network info

     vars:

      msg: |

       All Interface List: {{ ansible_interfaces }}

       All IP: {{ ansible_all_ipv4_addresses }}

       Gateway: {{ ansible_default_ipv4.gateway }}

       Eth0 MAC: {{ ansible_eth0.macaddress }}

     debug:

      msg: "{{ msg.split('\n') }}"





TASK 4



Ansible Tasks:  we will print system OS and Kernel Information. we define ansible variable like ansible_distribution to print OS Distribution, ansible_distribution_release to print OS Release, ansible_distribution_version to Print OS Version, ansible_kernel to print Kernel Version and ansible_architecture to Print OS Architecture.



   - name: OS and Kernel info

     vars:

      msg: |

       Distribution: {{ ansible_distribution }}

       Release: {{ ansible_distribution_release }}

       Distribution Version: {{ ansible_distribution_version }}

       Kernel: {{ ansible_kernel }}

       Architecture: {{ ansible_architecture }}

     debug:

      msg: "{{ msg.split('\n') }}"







TASK 5



A the end of  ansible tasks we will print system Hardware Information. we define ansible variable like ansible_processor to print  CPU, ansible_processor_core to print CPU Core Information, ansible_memtotal_mb to print system RAM, and ansible_memory_mb.swap.total to print system SWAP Memory.



   - name: HW info

     vars:

      msg: |

       CPU: {{ ansible_processor }}

       CPU Core: {{ ansible_processor_cores }}

       RAM: {{ ansible_memtotal_mb }}

       SWAP: {{ ansible_memory_mb.swap.total }}

     debug:

      msg: "{{ msg.split('\n') }}"





The Final ansible playbook look like :


cat system_info.yml

---

 - hosts: all

   tasks:

   - name: hostname

     debug:

       var=ansible_hostname



   - name: date and time

     vars:

      msg: |

       Date: {{ ansible_date_time.date }}

       Timezone: {{ ansible_date_time.tz }}

     debug:

      msg: "{{ msg.split('\n') }}"



   - name: network info

     vars:

      msg: |

       All Interface List: {{ ansible_interfaces }}

       All IP: {{ ansible_all_ipv4_addresses }}

       Gateway: {{ ansible_default_ipv4.gateway }}

       Eth0 MAC: {{ ansible_eth0.macaddress }}

     debug:

      msg: "{{ msg.split('\n') }}"



   - name: OS and Kernel info

     vars:

      msg: |

       Distribution: {{ ansible_distribution }}

       Release: {{ ansible_distribution_release }}

       Distribution Version: {{ ansible_distribution_version }}

       Kernel: {{ ansible_kernel }}

       Architecture: {{ ansible_architecture }}

     debug:

      msg: "{{ msg.split('\n') }}"



   - name: HW info

     vars:

      msg: |

       CPU: {{ ansible_processor }}

       CPU Core: {{ ansible_processor_cores }}

       RAM: {{ ansible_memtotal_mb }}

       SWAP: {{ ansible_memory_mb.swap.total }}

     debug:



      msg: "{{ msg.split('\n') }}"



To check the syntax of a playbook with the --syntax-check flag.


ansible-playbook --syntax-check system_info.yml



To run playbook on hosts of inventory


ansible-playbook  system_info.yml



Please Visit Video for full output










Thanks

End of this ansble playbook tutorial,  we need your support so i request you to please comment if something missed by me, and share and like this post aslo 




www.linuxtopic.com


Post a Comment

0Comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Ok, Go it!