Well OK, now we've ran a few playbooks. But these have been pretty simple, can I put some logic in a playbook?
This playbook will check to see what distro you are running and then adjust the commands accordingly.
This tries to do a system update. If anything gets upgraded, it will reboot your computer. If not, it does
nothing.
Notice I didn't use an ansible module to find out which distro we are using. I used the shell command
to check the /etc/release file. While are hundreds of modules, there still isn't one for everything, sometimes
you have to use your own commands.
But in this case, I actually could have done it another way, the ansible way.
Near the top of my script, I have a "gather_facts" line. This will check the ansible_distribution for me. No shell commands
are necessary. The gather_facts is kind of an all or none option, it can slow your playbook down by a second or two while it
gathers the facts from your client computer.
Code:
---
# Ansible playbook to update packages and reboot if any were upgraded
- name: Update and reboot if packages were upgraded
hosts: fedora,redhat,ubuntu,debian
become: yes # Use sudo to become root
tasks:
- name: Determine the OS type
ansible.builtin.shell: |
if [[ -f /etc/debian_version ]]; then
echo "debian"
elif [[ -f /etc/fedora-release ]]; then
echo "fedora"
elif [[ -f /etc/redhat-release ]]; then
echo "redhat"
fi
register: os_type # Register the output of the command to use in next tasks
- name: Update packages for Debian-based systems
ansible.builtin.apt:
upgrade: dist
update_cache: yes
when: os_type.stdout == "debian"
register: debian_upgrade
- name: Update packages for Fedora-based systems
ansible.builtin.dnf:
name: "*"
state: latest
update_cache: yes
when: os_type.stdout == "fedora" or os_type.stdout == "redhat"
register: fedora_upgrade
- name: Check if packages were upgraded (Debian-based)
ansible.builtin.debug:
msg: "Nothing was updated."
when: os_type.stdout == "debian" and debian_upgrade.changed == false
- name: Check if packages were upgraded (Fedora-based)
ansible.builtin.debug:
msg: "Nothing was updated."
when: (os_type.stdout == "fedora" or os_type.stdout == "redhat") and fedora_upgrade.changed == false
- name: Reboot if packages were upgraded (Debian-based)
ansible.builtin.reboot:
when: os_type.stdout == "debian" and debian_upgrade.changed == true
- name: Reboot if packages were upgraded (Fedora-based)
ansible.builtin.reboot:
when: (os_type.stdout == "fedora" or os_type.stdout == "redhat") and fedora_upgrade.changed == true
This playbook will check to see what distro you are running and then adjust the commands accordingly.
This tries to do a system update. If anything gets upgraded, it will reboot your computer. If not, it does
nothing.
Notice I didn't use an ansible module to find out which distro we are using. I used the shell command
to check the /etc/release file. While are hundreds of modules, there still isn't one for everything, sometimes
you have to use your own commands.
But in this case, I actually could have done it another way, the ansible way.

Code:
---
# Ansible playbook to update packages and reboot if any were upgraded
- name: Update and reboot if packages were upgraded
hosts: fedora,redhat,ubuntu,debian
gather_facts: yes # Gather facts to use ansible_distribution
become: yes # Use sudo to become root
tasks:
- name: Update packages for Debian-based systems
ansible.builtin.apt:
upgrade: dist
update_cache: yes
when: ansible_distribution == "Ubuntu" or ansible_distribution == "Debian"
register: debian_upgrade
- name: Update packages for Fedora-based systems
ansible.builtin.dnf:
name: "*"
state: latest
update_cache: yes
when: ansible_distribution == "Fedora" or ansible_distribution == "RedHat"
register: fedora_upgrade
- name: Check if packages were upgraded (Debian-based)
ansible.builtin.debug:
msg: "Nothing was updated."
when: (ansible_distribution == "Ubuntu" or ansible_distribution == "Debian") and debian_upgrade.changed == false
- name: Check if packages were upgraded (Fedora-based)
ansible.builtin.debug:
msg: "Nothing was updated."
when: (ansible_distribution == "Fedora" or ansible_distribution == "RedHat") and fedora_upgrade.changed == false
- name: Reboot if packages were upgraded (Debian-based)
ansible.builtin.reboot:
when: (ansible_distribution == "Ubuntu" or ansible_distribution == "Debian") and debian_upgrade.changed == true
- name: Reboot if packages were upgraded (Fedora-based)
ansible.builtin.reboot:
when: (ansible_distribution == "Fedora" or ansible_distribution == "RedHat") and fedora_upgrade.changed == true
Near the top of my script, I have a "gather_facts" line. This will check the ansible_distribution for me. No shell commands
are necessary. The gather_facts is kind of an all or none option, it can slow your playbook down by a second or two while it
gathers the facts from your client computer.
Last edited: