Home Ansible Examples - Part 2
Post
Cancel

Ansible Examples - Part 2

Here is a sample Ansible script that updates the packages 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

update.sh

1
2
3
4
5
6
7
8
#!/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" \
  update.yml

update.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
---
- name: Update machines
  hosts: all
  become: yes
  gather_facts: no

  tasks:
    - name: Cleaning the repo
      shell: "dnf clean all"
      register: clean_results

    - name: Updating the  machines
      dnf:
        name: "*"
        state: latest
        update_cache: yes
      register: update_results

    - name: Show results for clean
      debug:
        msg: "{{ clean_results.stdout }}"

    - name: Show results for update
      debug:
        msg: "{{ update_results.msg }}"
  1. In the inventory.ini file change the names and IP address as needed.
  2. Mark update.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
22
23
24
25
26
27
28
29
PLAY [Update machines] **********************************************************************************************************************************************************************************************************************

TASK [Cleaning the repo] ********************************************************************************************************************************************************************************************************************
changed: [test3.lan]
changed: [test1.lan]

TASK [Updating the  machines] ***************************************************************************************************************************************************************************************************************
ok: [test3.lan]
ok: [test1.lan]

TASK [Show results for clean] ***************************************************************************************************************************************************************************************************************
ok: [test1.lan] => {
    "msg": "25 files removed"
}
ok: [test3.lan] => {
    "msg": "25 files removed"
}

TASK [Show results for update] **************************************************************************************************************************************************************************************************************
ok: [test1.lan] => {
    "msg": "Nothing to do"
}
ok: [test3.lan] => {
    "msg": "Nothing to do"
}

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
test1.lan                  : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
test3.lan                  : ok=4    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.
  • The dnf ansible module (line 8) does not have a “clean” command so we have to resort to a shell command.
  • Using the dnf module (line 12) produces better results and better output.
  • The register value specifies where to store the output of this section.
This post is licensed under CC BY 4.0 by the author.