Skip to content

This week as I set about the motions to keep up my learning with Ansible and achieve my goal of completing my first project, I learned about the basics of inventory, conditions and more Linux basics.

Part of the development this week I want to share with you is the learning I had with Conditions. It is but a small thing to anyone who has used Ansible, but I feel it was an achievement given my lack of knowledge on the subject.

As a preface here is a relevant portion my lab setup to give context.

I have 7 servers, with a mix of distros. This server collection in my ansible hosts inventory is organised as web_srv. There 2x Ubuntu, 2x CentOS, 1x OpenSUSE, 2x Fedora.

I am trying to install Apache2 across all of them simultaneously and with the latest build and due to the unique distros I will need to create a condition which deals with the different ways of installing said service.

  1. Determine name of OS
    1. Ran ansible -m setup across all web_srv
    1. Too much data, so limited to one of each type
    1. OS Families gathered – Ubuntu == ‘Debian’, Fedora & CentOS == ‘RedHat’, OpenSUSE == ‘Suse’
  2. Playbook
  - hosts: web_srv
    become: yes

      - name: install apache2 on Ubuntu Hosts
        apt: name=apache2 state=latest
        when: ansible_os_family == 'Debian'

      - name: install httpd on Fedora & CentOS hosts
        yum: name=httpd state=latest
        when: ansible_os_family == 'RedHat'

      - name: install apache2 on OpenSUSE hosts
        zypper: name=apache2 state=latest
        when: ansible_os_family == 'Suse'
  • Results – Success! But, apologies I didn’t get a screenshot of the first completed run of the playbook, this is the 2nd run which just shows essentially nothing was changed and it was all installed with latest version.
conditionals_test01.yaml – instant success!

My first conditionals ansible playbook to install a service across multiple distributions and done within 10-15 mins. Obviously a very small and easy to do thing as shown here, but that’s precisely the point. I am really looking forward to expanding on my ansible knowledge over this coming week so I will be armed with the knowledge and tools to get started on Project 1.

Have a great week guys and please feel free to let me know any other sources of great guides for network ansible playbooks or other places to learn basic things to do in Ansible.

Leave a Reply

Skip to toolbar