Skip to content

Latest commit

 

History

History
86 lines (53 loc) · 2.35 KB

readme.md

File metadata and controls

86 lines (53 loc) · 2.35 KB

Lab: Displaying CPU Count with Ansible

Overview

In this beginner-friendly lab, you will learn how to use Ansible to gather facts about your managed hosts and display specific information. Specifically, you will create a playbook to display the number of CPUs on the target host.


Objectives

  • Understand how to gather facts in Ansible.
  • Access and use gathered facts in tasks.
  • Create a task to display the CPU count of a target host.

Prerequisites

  • Basic understanding of YAML syntax.
  • Ansible installed on your ansible machine.
  • Access to one or more hosts configured for management with Ansible.

Duration

Estimated Time: 20 minutes


Instructions

Step 1: Create Your Playbook

  1. On your ansible machine, open a text editor and create a new file named cpu_count_playbook.yml.

  2. Start defining your playbook with the following content:

    ---
    - name: Display CPU count
      hosts: servers
      gather_facts: yes

    Explanation:

    • hosts: servers specifies that the playbook should run on hosts in the servers group.
    • gather_facts: yes enables Ansible to collect information about the host before executing tasks.

Step 2: Add a Task to Display CPU Count

  1. Under the tasks section, add a task to display the number of CPUs:

      tasks:
        - name: Display CPU count
          debug:
            msg: "The target host has {{ ansible_processor_vcpus }} CPUs."

    Explanation:

    • This task uses the debug module to print a message.
    • {{ ansible_processor_vcpus }} is a variable provided by Ansible's gathered facts, representing the number of CPUs on the host.

Step 3: Run Your Playbook

  1. Save the cpu_count_playbook.yml file.

  2. Execute the playbook using the following command:

    ansible-playbook cpu_count_playbook.yml
  3. Observe the output. The playbook will display the number of CPUs for each host in the servers group.


Conclusion

Congratulations! You've successfully created an Ansible playbook that gathers facts about your hosts and displays the number of CPUs. This exercise demonstrates how to access and use the information collected by Ansible, a fundamental skill for more advanced automation tasks. Keep experimenting with different facts to explore what information you can gather and utilize. 👏