Home Ansible Examples - Part 1
Post
Cancel

Ansible Examples - Part 1

Here is a sample Ansible script that gets the version number of the “grep” utility installed on your computers.

  1. Use an existing computer. Install Ansible on it.
  2. For this example, create two VMs or use two or more computers you already have. These will be our test computers.
  3. Enter the names and IP addresses of the test computers you want to poll in your /etc/hosts file of your Ansible computer.
  4. From your Ansible computer, try to ping the names of your test computers you want to manage and make sure the names resolve.
  5. For simpicity’s sake, setup passwordless login from your Ansible computers to your test computers. https://castinganet.net/posts/passwordless-login/
  6. On the Ansible computer, make a directory for this example.
  7. Place the following three files into that folder:

inventory.ini

1
2
3
4
5
[group1]
test1.lan

[group2]
test3.lan

example1.sh

1
2
3
4
5
6
7
#!/bin/bash

# Hardcoding a password here is NOT recommended. We will fix this later using an Ansible vault.
ansible-playbook \
  -i inventory.ini \
  --extra-vars "ansible_user=dave ansible_sudo_pass='password'" \
  example1.yml

example1.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
---
- name:  Checking the version numbers of the grep utility
  hosts: all
  become: true
  become_method: sudo
  gather_facts: no

  tasks:
    - name: Getting the version number
      shell: 'dnf list installed | grep "grep"'
      register: log_results

    - name: Showing the results
      debug:
        msg: "{{ log_results.stdout_lines }}"
  1. In the inventory.ini file change the names and IP address as needed.
  2. Mark example1.sh as executable (chmod +x example1.sh).
  3. If all goes well you should see this output.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
PLAY [Checking the version numbers of the grep utility] ************************************************************************************************************************************************************************************

TASK [Getting the version number] **********************************************************************************************************************************************************************************************************
changed: [test3.lan]
changed: [test1.lan]

TASK [Show the results] ********************************************************************************************************************************************************************************************************************
ok: [test1.lan] => {
    "msg": [
        "grep.x86_64                           3.6-5.el9                       @anaconda "
    ]
}
ok: [test3.lan] => {
    "msg": [
        "grep.x86_64                           3.6-5.el9                       @anaconda "
    ]
}

PLAY RECAP *********************************************************************************************************************************************************************************************************************************
test1.lan                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
test3.lan                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  • In the example1.yml, you can change the hosts parameter to group1 or group2. “all” means all of the hosts mentioned in your inventory file.
  • There are some commands (notably the shell command) that will always say “changed” even though the command did not change anything.
This post is licensed under CC BY 4.0 by the author.