Bookmark this page

Lab: Creating Roles

In this review, you will convert the ansible-vsftpd.yml playbook into a role, and then use that role in a new playbook that will also run some additional tasks.

Outcomes

You should be able to:

  • Create a role to configure the vsftpd service using tasks from an existing playbook.

  • Include a role in a playbook, and execute the playbook.

Important

You may find it useful to debug your role by testing it in a playbook that does not contain the extra tasks or playbook variables listed above, but instead contains a play that only targets hosts in the group ftpservers and applies the role.

After confirming that a simplified playbook using only the role works just like the original ansible-vsftpd.yml playbook, you can build the complete vsftpd-configure.yml playbook by adding the additional variables and tasks specified above.

Important

If you are having trouble with your site.yml playbook, make sure that both vsftpd-configure.yml and ftpclients.yml use consistent indentation.

Log in to workstation as student using student as the password.

On workstation, run the lab review-roles start command. This script ensures that the remote hosts are reachable on the network. The script also checks that Ansible is installed on workstation, creates a directory structure for the lab environment, and installs required lab files.

[student@workstation ~]$ lab review-roles start

Procedure 10.3. Instructions

  1. Change to the review-roles working directory. Configure the Ansible project to use the static inventory file inventory. Verify the inventory configuration using the ansible-inventory command.

    1. Change to the review-roles working directory.

      [student@workstation ~]$ cd ~/review-roles
      [student@workstation review-roles]$
    2. Edit the ansible.cfg file, add the inventory directive in the [defaults] section, and set it to ./inventory.

      The [defaults] section of the ansible.cfg file looks like this:

      [defaults]
      remote_user=devops
      inventory=./inventory
    3. Use the ansible-inventory command to verify the project inventory configuration:

      [student@workstation review-roles]$ ansible-inventory --list all
      {
          "_meta": {
              "hostvars": {}
          },
          "all": {
              "children": [
                  "ftpclients",
                  "ftpservers",
                  "ungrouped"
              ]
          },
          "ftpclients": {
              "hosts": [
                  "servera.lab.example.com",
                  "serverc.lab.example.com"
              ]
          },
          "ftpservers": {
              "hosts": [
                  "serverb.lab.example.com",
                  "serverd.lab.example.com"
              ]
          }
      }
  2. Convert the ansible-vsftpd.yml playbook to the role ansible-vsftpd.

    1. Create the roles subdirectory.

      [student@workstation review-roles]$ mkdir -v roles
      mkdir: created directory 'roles'
    2. Using ansible-galaxy, create the directory structure for the new ansible-vsftpd role in the roles subdirectory.

      [student@workstation review-roles]$ cd roles
      [student@workstation roles]$ ansible-galaxy init ansible-vsftpd
      - Role ansible-vsftpd was created successfully
      [student@workstation roles]$ cd ..
      [student@workstation review-roles]$
    3. Using tree, verify the directory structure created for the new role.

      [student@workstation review-roles]$ tree roles
      roles
      └── ansible-vsftpd
          ├── defaults
          │   └── main.yml
          ├── files
          ├── handlers
          │   └── main.yml
          ├── meta
          │   └── main.yml
          ├── README.md
          ├── tasks
          │   └── main.yml
          ├── templates
          ├── tests
          │   ├── inventory
          │   └── test.yml
          └── vars
              └── main.yml
      
      9 directories, 8 files
    4. Replace the roles/ansible-vsftpd/defaults/main.yml file with the variable definitions in the defaults-template.yml file.

      [student@workstation review-roles]$ mv -v defaults-template.yml \
      > roles/ansible-vsftpd/defaults/main.yml
      renamed 'defaults-template.yml' -> 'roles/ansible-vsftpd/defaults/main.yml'
    5. Replace the roles/ansible-vsftpd/vars/main.yml file with the variable definitions in the vars.yml file.

      [student@workstation review-roles]$ mv -v vars.yml \
      > roles/ansible-vsftpd/vars/main.yml
      renamed 'vars.yml' -> 'roles/ansible-vsftpd/vars/main.yml'
    6. Use the templates/vsftpd.conf.j2 file as a template for the ansible-vsftpd role.

      [student@workstation review-roles]$ mv -v vsftpd.conf.j2 \
      > roles/ansible-vsftpd/templates/
      renamed 'vsftpd.conf.j2' -> 'roles/ansible-vsftpd/templates/vsftpd.conf.j2'
    7. Copy tasks from the ansible-vsftpd.yml playbook to the roles/ansible-vsftpd/tasks/main.yml file. The value of the src keyword in the template module task no longer needs to reference the templates subdirectory. The roles/ansible-vsftpd/tasks/main.yml file should contain the following when you finish.

      ---
      # tasks file for ansible-vsftpd
      - name: Packages are installed
        yum:
          name: '{{ vsftpd_package }}'
          state: present
      
      - name: Ensure service is started
        service:
          name: '{{ vsftpd_service }}'
          state: started
          enabled: true
      
      - name: Configuration file is installed
        template:
          src: vsftpd.conf.j2
          dest: '{{ vsftpd_config_file }}'
          owner: root
          group: root
          mode: '0600'
          setype: etc_t
        notify: restart vsftpd
      
      - name: firewalld is installed
        yum:
          name: firewalld
          state: present
      
      - name: firewalld is started and enabled
        service:
          name: firewalld
          state: started
          enabled: yes
      
      - name: FTP port is open
        firewalld:
          service: ftp
          permanent: true
          state: enabled
          immediate: yes
      
      - name: Passive FTP data ports allowed through the firewall
        firewalld:
          port: 21000-21020/tcp
          permanent: yes
          state: enabled
          immediate: yes
    8. Copy the handlers from the ansible-vsftpd.yml playbook to the roles/ansible-vsftpd/handlers/main.yml file. The roles/ansible-vsftpd/handlers/main.yml file should contain the following when you finish.

      ---
      # handlers file for ansible-vsftpd
      - name: restart vsftpd
        service:
          name: "{{ vsftpd_service }}"
          state: restarted
  3. Update the contents of the roles/ansible-vsftpd/meta/main.yml file.

    VariableValue
    authorRed Hat Training
    descriptionexample role for RH294
    companyRed Hat
    licenseBSD
    1. Change the value of the author entry to Red Hat Training.

        author: Red Hat Training
    2. Change the value of the description entry to example role for RH294.

        description: example role for RH294
    3. Change the value of the company entry to Red Hat.

        company: Red Hat
    4. Change the value of the license: entry to BSD.

        license: BSD
  4. Modify the contents of the roles/ansible-vsftpd/README.md file so that it provides pertinent information regarding the role. After modification, the file should contain the following.

    ansible-vsftpd
    =========
    Example ansible-vsftpd role from Red Hat's "Linux Automation" (RH294)
    course.
    
    Role Variables
    --------------
    
    * defaults/main.yml contains variables used to configure the vsftpd.conf template
    * vars/main.yml contains the name of the vsftpd service, the name of the RPM
    package, and the location of the service's configuration file
    
    Dependencies
    ------------
    
    None.
    
    Example Playbook
    ----------------
    
        - hosts: servers
          roles:
            - ansible-vsftpd
    
    License
    -------
    
    BSD
    
    Author Information
    ------------------
    
    Red Hat (training@redhat.com)
  5. Remove the unused directories from the new role.

    [student@workstation review-roles]$ rm -rvf roles/ansible-vsftpd/tests
    removed 'roles/ansible-vsftpd/tests/inventory'
    removed 'roles/ansible-vsftpd/tests/test.yml'
    removed directory: 'roles/ansible-vsftpd/tests'
  6. Create the new playbook vsftpd-configure.yml. It should contain the following.

    ---
    - name: Install and configure vsftpd
      hosts: ftpservers
      vars:
        vsftpd_anon_root: /mnt/share/
        vsftpd_local_root: /mnt/share/
    
      roles:
        - ansible-vsftpd
    
      tasks:
        - name: /dev/vdb1 is partitioned
          parted:
            device: /dev/vdb
            number: 1
            label: gpt
            part_start: 1MiB
            part_end: 100%
            state: present
    
        - name: XFS file system exists on /dev/vdb1
          filesystem:
            dev: /dev/vdb1
            fstype: xfs
            force: yes
    
        - name: anon_root mount point exists
          file:
            path: '{{ vsftpd_anon_root }}'
            state: directory
    
        - name: /dev/vdb1 is mounted on anon_root
          mount:
            path: '{{ vsftpd_anon_root }}'
            src: /dev/vdb1
            fstype: xfs
            state: mounted
            dump: '1'
            passno: '2'
          notify: restart vsftpd
    
        - name: Make sure permissions on mounted fs are correct
          file:
            path: '{{ vsftpd_anon_root }}'
            owner: root
            group: root
            mode: '0755'
            setype: "{{ vsftpd_setype }}"
            state: directory
    
        - name: Copy README to the ftp anon_root
          copy:
            dest: '{{ vsftpd_anon_root }}/README'
            content: "Welcome to the FTP server at {{ ansible_fqdn }}\n"
            setype: '{{ vsftpd_setype }}'
  7. Change the site.yml playbook to use the newly created vsftpd-configure.yml playbook instead of the ansible-vsftpd.yml playbook.

    ---
    # FTP Servers playbook
    - import_playbook: vsftpd-configure.yml
    
    # FTP Clients playbook
    - import_playbook: ftpclients.yml
  8. Verify that the site.yml playbook works as intended by executing it with ansible-playbook.

    [student@workstation review-roles]$ ansible-playbook site.yml

Evaluation

From workstation, run the lab review-roles grade command to confirm success on this exercise. Correct any reported failures and rerun the script until successful.

[student@workstation ~]$ lab review-roles grade

Finish

Run the lab review-roles finish command to clean up the lab tasks on servera and serverb.

[student@workstation ~]$ lab review-roles finish

This concludes the lab.

Revision: rh294-8.4-9cb53f0