ansible study

Photo by Mae Mu on Unsplash

I’m no expert on ansible. I use provided playbooks.

You can start by using ansible to do simple things – which is good for learning ansible but otherwise pretty pointless on the whole:

- name: Gather system information
  hosts: "{{ target_hosts }}" #  ---extra-vars "target_hosts=my-hostname"
  gather_facts: yes
  tasks:

    - name: Get disk space details
      command: df -h
      register: disk_space

    - name: show disk space
      debug:
        msg:
          - "{{ disk_space.stdout_lines }}"

So you could run that playbook like this:

ansible-playbook  myplaybook.yaml --extra-vars 'target_hosts=hostname1' -k

So thats essentially equivalent to “ssh my-hostname df -h” so beyond trivial and quite obviously not much value.

Ansible gathers facts about the hosts it works with so you can just access those facts:

 name: Gather system information
  hosts: "{{ target_hosts }}" #  ---extra-vars "target_hosts=hostname1"
  gather_facts: yes
  tasks:

#   - name: Show all available facts
#     debug:
#       var: ansible_facts

    - name: Show various (including disk details)
      debug:
        msg:
          - "For {{ ansible_hostname }}:"
          - "Operating System: {{ ansible_distribution }} {{ ansible_distribution_version }}"
          - "CPU Cores: {{ ansible_processor_vcpus }}"
          - "Total RAM: {{ ansible_memtotal_mb }} MB"
          - "Uptime:  {{ ansible_facts['facter_system_uptime']['uptime'] }}"
          - "Interfaces: {{ ansible_facts['interfaces'] | reject('search', '^lo') | list }}"
          - "IP Address: {{ ansible_facts['locally_reachable_ips']['ipv4'] | reject('search', '^127\\.') | list }}"
          - "Disks: {% for item in ansible_facts['facter_disks'] | dict2items | rejectattr('key', 'equalto', 'sr0') %} {{ item.key }} (Size: {{ item.value['size'] }}) {% endfor %}"
          - "Network Interfaces: {% for item in ansible_facts['facter_networking']['interfaces'] | dict2items | rejectattr('key', 'equalto', 'lo') %} {{ item.key }} (Address: {{ item.value['bindings'][0]['address'] }}, Netmask: {{ item.value['bindings'][0]['netmask'] }}) {% endfor %}"
          - "Disk Capacity: {% for mount in ansible_facts['facter_mountpoints'] | dict2items | selectattr('value.device', 'search', '/dev/mapper') %} {{ mount.key }} (Cap: {{ mount.value['capacity'] }}) {% endfor %}"

The output looks includes this:

TASK [Show various (including disk details)] ********************************************************************************************************************
ok: [hostname1] => 
  msg:
  - 'For hostname1:'
  - 'Operating System: AlmaLinux 8.10'
  - 'CPU Cores: 2'
  - 'Total RAM: 1644 MB'
  - 'Uptime:  3 days'
  - 'Interfaces: [''ens160'']'
  - 'IP Address: [''192.168.0.89'']'
  - 'Disks:  sda (Size: 52.00 GiB) '
  - 'Network Interfaces:  ens160 (Address: 192.168.0.89, Netmask: 255.255.255.0) '
  - 'Disk Capacity:  / (Cap: 71.71%)  /home (Cap: 40.94%)  /opt (Cap: 13.42%)  /tmp (Cap: 3.35%)  /var (Cap: 70.68%) '

The syntax is quite opaque and needs some rehearsal. How do you find ansible to work with? I’m currently interested in exploring the possibilities. Of course these days Copilot is very helpful too.

Leave a Reply

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