Build and test your application using Ansible Container

Source: Internet
Author: User
Tags virtual environment ansible playbook ansible roles
This is a creation in Article, where the information may have evolved or changed.

Containers are an increasingly popular development environment. As a developer, you can choose from a variety of tools to manage your containers. This article will introduce you to Ansible Container and show you how to run and test your application in a similar production environment.

Entry

This example uses a simple Flask Hello world program. This program is served by Apache HTTP servers just as in production. First, install the necessary docker packages:

sudo dnf install docker

Ansible Container needs to communicate with the Docker service via a local socket. The following command changes the socket owner and adds you to the user group that can access the socket docker :

sudo groupadd docker && sudo gpasswd -a $USER dockerMYGRP=$(id -g) ; newgrp docker ; newgrp $MYGRP

Run the id command to make sure that the docker group is listed in your group members. Finally, use sudo to enable and start the Docker service:

sudo systemctl enable docker.servicesudo systemctl start docker.service

Set Ansible Container

Ansible Container enables you to build container images and orchestrate them using Ansible playbook. The program is described in a YAML file instead of using Dockerfile, which lists the Ansible roles that make up the container image.

Unfortunately, Ansible Container does not have RPM packages available in Fedora. To install it, use the PYTHON3 virtual environment module.

mkdir ansible-container-flask-examplecd ansible-container-flask-examplepython3 -m venv .venvsource .venv/bin/activatepip install ansible-container[docker]

These commands will install the Ansible Container and the Docker engine. Ansible Container offers three types of engines: Docker, Kubernetes and Openshift.

Set up the project

Now that the Ansible Container is installed, the project is set up. Ansible Container provides a simple command to create all the files needed to start up:

ansible-container init

Take a look at this command. Files created in the current directory:

    • ansible.cfg
    • ansible-requirements.txt
    • container.yml
    • meta.yml
    • requirements.yml

The project is used only container.yml to describe the program service. For more information about other files, see the Getting Started documentation for Ansible Container.

Defining containers

Update as follows container.yml :

Version: "2" Settings:conductor: # The conductor container does the heavy lifting, and provides a portable # Python Runtime for building your target containers.    It should is derived # from the same distribution as you ' re building your target containers with. BASE:FEDORA:26 # Roles_path: # Specify a local path containing Ansible roles # volumes: # Provide a list of Volumes to mount # environment: # List or mapping of environment variables # Set The name of the project.  Defaults to basename of the project directory.  # for built services, concatenated with service name to form the built image name.  Project_name:flask-helloworldservices: # ADD Your containers here, specifying the base image of your want to build from.  # To use this example, uncomment it and delete the curly braces after services key.  # You could need to run ' Docker pull Ubuntu:trusty ' for the work. Web:from: "fedora:26" roles:-Base ports:-"5000:80" command:["/usr/bin/dumb-init", "httpd", "-dforeground"] volumes:-$PWD/flask-helloworld:/flaskapp:z 

conductorSection updates the basic settings to use the Fedora 26 container base image.

servicesSection adds a web service. This service uses Fedora 26, followed by a role named base . It also sets the port mappings between the container and the host. The Apache HTTP Server serves the Flask program on port 80 of the container, which is redirected to Port 5000 of the host. The file then defines a volume that mounts the Flask program source code into the container /flaskapp .

Finally, the container runs the configuration when it starts command . This example uses Dumb-init, a simple process manager and initializes the system to launch the Apache HTTP server.

Ansible role

Now that you have finished setting up the container, create a Ansible role to install and configure the dependencies required for the Flask program. First, create the base role.

mkdir -p roles/base/taskstouch roles/base/tasks/main.yml

Now edit main.yml , it looks like this:

---- name: Install dependencies   dnf: pkg={{item}} state=present  with_items:    - python3-flask    - dumb-init    - httpd    - python3-mod_wsgi- name: copy the apache configuration  copy:    src: flask-helloworld.conf    dest: /etc/httpd/conf.d/flask-helloworld.conf    owner: apache    group: root    mode: 655

This Ansible character is simple. First it installs dependencies. Then, copy the Apache HTTP server configuration. If you are not familiar with the Ansible role, please review the role documentation.

Apache HTTP Configuration

Next, configure the flask-helloworld.conf Apache HTTP server by creating:

$ mkdir -p roles/base/files$ touch roles/base/files/flask-helloworld.conf

Finally, add the following to the file:

<VirtualHost *>    ServerName example.com    WSGIDaemonProcess hello_world user=apache group=root    WSGIScriptAlias / /flaskapp/flask-helloworld.wsgi    <Directory /flaskapp>        WSGIProcessGroup hello_world        WSGIApplicationGroup %{GLOBAL}    Require all granted    </Directory></VirtualHost>

An important part of this file is WSGIScriptAlias . The directive maps the script flask-helloworld.wsgi to a / . For more details on Apache HTTP servers and MOD_WSGI, please read the Flask documentation.

Flask "Hello World"

Finally, create a simple Flask program and flask-helloworld.wsgi script.

mkdir flask-helloworldtouch flask-helloworld/app.pytouch flask-helloworld/flask-helloworld.wsgi

Add the following to app.py :

from flask import Flaskapp = Flask(__name__)@app.route("/")def hello():    return "Hello World!"

Then edit flask-helloworld.wsgi , add this:

import syssys.path.insert(0, '/flaskapp/')from app import app as application

Build and run

Now is the time ansible-container build to use and ansible-container run command to build and run the container.

ansible-container build

This command takes some time to complete, so wait patiently.

ansible-container run

You can now access your Flask program via the following URL: Http://localhost:5000/

Conclusion

You have now seen how to use Ansible Container to manage, build, and configure programs that run in a container. All the configuration files and source code for this example are on Pagure.io. You can use this example as a basis to start using Ansible Container in your project.

via:https://fedoramagazine.org/build-test-applications-ansible-container/

Author: Clement Verna Translator: GEEKPI proofreading: Wxy

This article was compiled by LCTT original, Linux China honors launched

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.