├── .gitignore ├── README.rst ├── ansible.cfg ├── requirements.txt └── roles ├── app ├── defaults │ └── main.yml ├── files │ └── flask_app.py ├── handlers │ └── main.yml ├── meta │ └── main.yml ├── tasks │ └── main.yml ├── templates │ └── flask_supervisor.conf.j2 └── vars │ └── main.yml ├── python ├── defaults │ └── main.yml └── tasks │ └── main.yml ├── supervisor ├── handlers │ └── main.yml └── tasks │ └── main.yml └── users ├── defaults └── main.yml └── tasks └── main.yml /.gitignore: -------------------------------------------------------------------------------- 1 | #ansible-test 2 | Dockerfile 3 | .ansible-test 4 | 5 | #emacs 6 | *~ -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | Example Ansible Automation for flask app 2 | ======================================== 3 | 4 | This repository is an example that shows how to use ansible-test. In addition, it is written in the style of using wrapper roles. For more information, see our blog post at: LINK HERE 5 | 6 | Getting Started 7 | --------------- 8 | 9 | To get started testing with this repository, run: 10 | 11 | .. code-block:: bash 12 | 13 | $ virtualenv venv 14 | $ source venv/bin/activate 15 | $ pip install -r requirements.txt 16 | 17 | 18 | Testing 19 | ------- 20 | 21 | To test locally with ansible-test, you must have docker installed locally. If you are on Mac OSX, I reccomend you use boot2docker to setup docker. 22 | 23 | .. code-block:: bash 24 | 25 | $ ansible-test app 26 | 27 | The above command will test the app role in a docker container. Once the automation is done, it will drop you into a shell on the container so you can inspect the resulting configuration. 28 | -------------------------------------------------------------------------------- /ansible.cfg: -------------------------------------------------------------------------------- 1 | [defaults] 2 | hash_behaviour=merge -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | ansible==1.9.1 2 | ecdsa==0.13 3 | Jinja2==2.7.3 4 | MarkupSafe==0.23 5 | paramiko==1.15.2 6 | pycrypto==2.6.1 7 | PyYAML==3.11 8 | -------------------------------------------------------------------------------- /roles/app/defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | app: 4 | owner: app 5 | install_dir: /opt/app 6 | conf_dir: /etc/app 7 | log_dir: /var/log/app -------------------------------------------------------------------------------- /roles/app/files/flask_app.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from flask import Flask 4 | app = Flask(__name__) 5 | 6 | @app.route('/') 7 | def hello_world(): 8 | return 'Hello World!' 9 | 10 | if __name__ == '__main__': 11 | app.run() 12 | -------------------------------------------------------------------------------- /roles/app/handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: restart flask app 4 | supervisorctl: name=app state=restarted -------------------------------------------------------------------------------- /roles/app/meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | dependencies: 4 | - { role: python } 5 | - { role: supervisor } 6 | - { role: users } -------------------------------------------------------------------------------- /roles/app/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: Setup Service Directories 4 | file: > 5 | state=directory 6 | path="{{ item }}" 7 | mode=755 8 | with_items: 9 | - "{{ app.install_dir }}" 10 | - "{{ app.conf_dir }}" 11 | - "{{ app.log_dir }}" 12 | 13 | - name: Install flask 14 | pip: name=flask 15 | 16 | - name: Install supervisor configuration 17 | template: > 18 | src=flask_supervisor.conf.j2 19 | dest=/etc/supervisor/conf.d/flask_supervisor.conf 20 | notify: reload supervisor config 21 | 22 | - name: Deploy Flask App 23 | copy: > 24 | src=flask_app.py 25 | dest="{{ app.install_dir }}/flask_app.py" 26 | mode=755 27 | notify: restart flask app -------------------------------------------------------------------------------- /roles/app/templates/flask_supervisor.conf.j2: -------------------------------------------------------------------------------- 1 | [program:app] 2 | command={{ app.install_dir }}/flask_app.py 3 | user={{ app.owner }} 4 | autostart=true 5 | autorestart=true 6 | stdout_logfile={{ app.log_dir }}/app.log 7 | stderr_logfile={{ app.log_dir }}/app.err.log 8 | -------------------------------------------------------------------------------- /roles/app/vars/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | python: 4 | version: 3.2 5 | 6 | users: 7 | user_list: 8 | app: {} -------------------------------------------------------------------------------- /roles/python/defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | python: 4 | version: 2.7 -------------------------------------------------------------------------------- /roles/python/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: install python 4 | apt: > 5 | name=python{{ python.version }} 6 | update_cache=yes 7 | cache_valid_time=300 8 | state=present -------------------------------------------------------------------------------- /roles/supervisor/handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: reload supervisor config 4 | command: supervisorctl reload && supervisorctl update -------------------------------------------------------------------------------- /roles/supervisor/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: install supervisor 4 | apt: > 5 | name=supervisor 6 | update_cache=yes 7 | cache_valid_time=300 8 | state=present 9 | 10 | 11 | - name: ensure supervisor is running 12 | service: name=supervisor state=started enabled=yes -------------------------------------------------------------------------------- /roles/users/defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | users: 4 | user_list: {} -------------------------------------------------------------------------------- /roles/users/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: Create users 4 | user: name={{ item.key }} 5 | with_dict: users.user_list --------------------------------------------------------------------------------