Bookmark this page

Guided Exercise: Implementing Configuration Changes with an Ansible Playbook

Configure a system with Ansible as a configuration manager.

Outcomes

You should be able to install and configure the web service on the system by using Ansible.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

[student@workstation ~]$ lab start baseline-configmanagement

This command confirms that the required hosts for this exercise are accessible and gets the templates for the web service configuration.

Instructions

In this exercise, you configure workstation as an Ansible control node, and then install and configure the web service on the servera managed node.

  1. On workstation as student user, create the workdir directory. Change directory to it.

    [student@workstation ~]$ mkdir workdir
    [student@workstation ~]$ cd workdir
  2. Create an ansible.cfg configuration file.

    [defaults]
    inventory = inventory
    remote_user = root
    host_key_checking = False
    deprecation_warnings = False
  3. Create the inventory file and set servera as the web_prod production web server.

    [webservers]
    web_prod ansible_host=servera
  4. Verify connectivity with the web_prod managed node.

    [student@workstation workdir]$ ansible web_prod -m ping
    web_prod | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": false,
        "ping": "pong"
    }
  5. Move the downloaded templates to the workdir directory and view the included substitution variables.

    [student@workstation workdir]$ mv ~/apache_* .
    [student@workstation workdir]$ grep "{{" apache_httpdconf.j2
    # {{ ansible_managed }}
    [student@workstation workdir]$ grep "{{" apache_indexhtml.j2
    <!-- {{ ansible_managed }} -->
    Hello from {{ inventory_hostname }}
  6. Create the mywebserver.yaml Ansible Playbook for the web service configuration, to include these tasks:

    • Install httpd package if not installed.

    • Use the templates as the web service configuration files.

    • Ensure that the firewall service is enabled for the web service.

    1. Set the managed hosts to configure. It is not necessary to gather facts for these tasks.

      - name: Install and configure a customized web server
        hosts: webservers
        gather_facts: False
    2. Configure tasks to verify that the httpd service is the latest version, and verify the presence of the firewalld service.

      - name: Install httpd package
        ansible.builtin.yum:
          name: httpd
          state: latest
      
      - name: Validate firewall
        ansible.builtin.yum:
          name: firewalld
          state: present
    3. Configure tasks to inject templates as the httpd configuration files.

      - name: Template out httpd configuration file
        template:
          src: apache_httpdconf.j2
          dest: /etc/httpd/conf/httpd.conf
          owner: root
          group: root
          mode: '0444'
      
      - name: Template out httpd index file
        template:
          src: apache_indexhtml.j2
          dest: /var/www/html/index.html
          owner: root
          group: root
          mode: '0444'
    4. Configure tasks to ensure that the httpd and firewalld services are active and enabled at system startup.

      - name: Start and enable httpd daemon
        ansible.builtin.service:
          name: httpd
          state: started
          enabled: true
      
      - name: Start and enable firewalld daemon
        ansible.builtin.service:
          name: firewalld
          state: started
          enabled: true
    5. Configure a task to ensure that the http service is allowed in the firewall.

      - name: Open http firewalld port
        firewalld:
          service: http
          immediate: yes
          permanent: yes
          state: enabled
    6. Verify that the final Ansible Playbook mywebserver.yaml contains this content:

      - name: Install and configure a customized web server
        hosts: webservers
        gather_facts: False
      
        tasks:
          - name: Install httpd package
            ansible.builtin.yum:
              name: httpd
              state: latest
      
          - name: Validate firewall
            ansible.builtin.yum:
              name: firewalld
              state: present
      
          - name: Template out httpd configuration file
            template:
              src: apache_httpdconf.j2
              dest: /etc/httpd/conf/httpd.conf
              owner: root
              group: root
              mode: '0444'
      
          - name: Template out httpd index file
            template:
              src: apache_indexhtml.j2
              dest: /var/www/html/index.html
              owner: root
              group: root
              mode: '0444'
      
          - name: Start and enable httpd daemon
            ansible.builtin.service:
              name: httpd
              state: started
              enabled: true
      
          - name: Start and enable firewalld daemon
            ansible.builtin.service:
              name: firewalld
              state: started
              enabled: true
      
          - name: Open http firewalld port
            firewalld:
              service: http
              immediate: yes
              permanent: yes
              state: enabled

      Note

      You can verify the syntax of an Ansible Playbook without implementing it.

      [student@workstation workdir]$ ansible-playbook --syntax-check mywebserver.yaml
      
      playbook: mywebserver.yaml
  7. Run the Ansible Playbook and verify its successful execution.

    [student@workstation workdir]$ ansible-playbook mywebserver.yaml
    PLAY [Install and configure a customized web server] *********************
    
    TASK [Install httpd package] *********************************************
    changed: [web_prod]
    
    TASK [Validate firewall] *************************************************
    ok: [web_prod]
    
    TASK [Template out httpd configuration file] *****************************
    changed: [web_prod]
    
    TASK [Template out httpd index file] *************************************
    ok: [web_prod]
    
    TASK [Start and enable httpd daemon] *************************************
    changed: [web_prod]
    
    TASK [Start and enable firewalld daemon] *********************************
    ok: [web_prod]
    
    TASK [Open http firewalld port] ******************************************
    changed: [web_prod]
    
    PLAY RECAP ***************************************************************
    web_prod : ok=7    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  8. Verify that the web server can be accessed.

    [student@workstation workdir]$ curl servera
    <!-- ansible managed -->
    <html>
    <head><title>Apache is running!</title></head>
    <body>
    <h1>
    Hello from web_prod
    </h1>
    </body>
    </html>
    [student@workstation workdir]$ cd ~

Finish

On the workstation machine, use the lab command to complete this exercise. This is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish baseline-configmanagement

Revision: rh342-8.4-6dd89bd