Extending the Nginx Formula

Hands-On Lab

 

Photo of Terrence Cox

Terrence Cox

Senior Vice President of Content

Length

01:00:00

Difficulty

Intermediate

Using Salt for Configuration Management and Orchestration generally involves more than a single server in your environment. Within this activity, the student will add an Ubuntu Minion to the CentOS 7 Master server to expand the Salt State for Nginx. By the end of these tasks, the student will have a strong understanding of how expand a Salt State with Jinja templates.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Extending the Nginx Formula

Introduction

As the technical intern at the special-interest publication, Apartment 42, you were previously tasked with creating a basic Nginx state to the web developer could compare the load time of the website on Nginx versus Apache. After doing some testing with CentOS 7, he has decided he's still not certain about the final infrastructure setup, and wants to test more options. He's now asked you to take the previously-created CentOS-based Nginx formula and alter it so it can also work on Debian-based machines; specially, he wants to test Ubuntu 16.04.

Clone the original formula onto your test server, then begin to update it to work with Ubuntu. The test environment includes a CentOS 7 master and an Ubuntu 16.04 minion. Make sure to run the finished formula on the Ubuntu server, do not just test it.

Solution

  1. Begin by logging in to the Salt Master lab server using the credentials provided on the hands-on lab page:

    ssh cloud_user@PUBLIC_IP_ADDRESS

Create and Utilize a map.jinja File

  1. Move to the salt directory.

    cd /srv/salt
  2. Clone the repository in GitHub provided for this lab.

    git clone https://github.com/linuxacademy/using-salt-nginx.git nginx
  3. Transition to the cloned directory.

    cd nginx/
  4. Edit the init.sls file.

    vim init.sls
  5. Place the include statement inside a conditional statement.

    1. Add the following line directly above the include line.

      {% if grains['os_family']=="RedHat" %}
    2. Add the following line directly below the - nginx.epel line.

      {% endif %}
  6. Save the changes and exit the editor.

  7. Create and open a map.jinja file.

    vim map.jinja
  8. Add the following to the new file.

    {% set nginx = salt['grains.filter_by']({
        'Debian': {
            'package': 'nginx',
            'service': 'nginx',
        },
        'RedHat': {
            'package': 'nginx',
            'service': 'nginx',
        },
    })%}
  9. Save the file and exit the editor.

  10. Edit the init.sls file.

    vim init.sls
  11. Add the following line to the top of the file.

    {% from "nginx/map.jinja" import nginx with context %}
  12. Change the value for the package name: parameter to the following.

    {{ nginx.package }}
  13. Change the value for the service name: parameter to the following.

    {{ nginx.service }}
  14. Save the changes and exit the editor.

  15. Verify the changes.

    sudo salt '*' state.sls nginx test=true
  16. Run the command.

    sudo salt '*' state.sls nginx

Update the Formula to Work on a Debian-Based Server

  1. View the config.sls file.

    vim config.sls
  2. Exit the editor.

  3. Change to the config directory.

    cd config
  4. Pull down the configuration file.

    wget -O debian-nginx.conf https://raw.githubusercontent.com/linuxacademy/using-salt-nginx/extended-version/config/.debian-nginx.conf
  5. Verify the files were acquired.

    ls
  6. Rename the nginx.conf file.

    mv nginx.conf redhat-nginx.conf
  7. Verify the name change.

    ls
  8. Change directories.

    cd
  9. Verify the directory contents.

    ls
  10. Edit the map.jina file.

    vim map.jinja
  11. Edit the Debian configuration by adding the following under service in the Debian group.

    'configfile': '/etc/nginx/nginx.conf',
    'configsource': 'salt://nginx/config/debian-nginx.conf',
  12. Edit the RedHat configuration by adding the following under service in the RedHat group.

    'configfile': '/etc/nginx/nginx.conf',
    'configsource': 'salt://nginx/config/redhat-nginx.conf',
  13. Edit the Debian configuration by adding the following to the bottom of the Debian group.

    'user': 'www-data',
  14. Edit the RedHat configuration by adding the following to the bottom of the RedHat group.

    'user': 'nginx',
  15. Save the changes and exit the editor.

  16. Edit the config.sls file.

    vim config.sls
  17. Add the following to the top of the file.

    {% from "nginx/map.jinja" import nginx with context %}
  18. Update the file.managed parameters under nginx_configuration.

    1. Replace the name: parameter with the following.

      {{ nginx.configfile }}
    2. Replace the source: parameter with the following.

      {{ nginx.configsource }}
    3. Replace the pkg: parameter with {{ nginx.package }}

  19. Save the changes and exit the editor.

  20. Test the changes.

    sudo salt '*' state.sls nginx,nginx.config test=true
  21. After verifying the test, run the command.

    sudo salt '*' state.sls nginx,nginx.config

Turn the Static Configuration Files Into File Templates

  1. Edit the debian-nginx.conf file.

    vim config/debian-nginx.conf
  2. Add the following line to the top of the file.

    {% from "nginx/map.jinja" import nginx with context %}
  3. Change the user line to the following.

    user {{ nginx.user }};
  4. Save the changes and exit the editor.

  5. Edit the redhat-nginx.conf file.

    vim config/redhat-nginx.conf
  6. Add the following line to the top of the file.

    {% from "nginx/map.jinja" import nginx with context %}
  7. Change the user line to the following.

    user {{ nginx.user }};
  8. Save the changes and exit the editor.

  9. Edit the config.sls file.

    vim config.sls
  10. Add the following line after the source parameter.

    - template: jinja
  11. Save the changes and exit the editor.

  12. Test the changes

    sudo salt '*' state.sls nginx,nginx.config test=true
  13. Run the command.

    sudo salt '*' state.sls nginx,nginx.config
  14. Edit the restart.sls file.

    vim restart.sls
  15. Change the m_name parameter to the following.

    {{ nginx.service }}
  16. Add the following line to the top of the file.

    {% from "nginx/map.jinja" import nginx with context %}
  17. Save the changes and exit the editor.

  18. Test the changes.

    sudo salt '*' state.sls nginx,nginx.config,nginx.restart test=true

Conclusion

Congratulations — you've completed this hands-on lab!