From charlesreid1

No edit summary
 
(23 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Playbook Example: Secure Nginx Server=
=Overview=


This page walks through a procedure resulting in the following files:
The Ansible documentation has a nice overview of playbooks: https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html


<pre>
==Brief Notes==
    playbooks/ansible.cfg


    playbooks/hosts
===Playbooks===


    playbooks/Vagrantfile
* playbooks are a way of executing commands on remote machines in a scripted way
* can orchestrate multiple ordered processes
* playbooks are written in YAML
* each playbook can contain one or more plays
* each play will target different servers (maybe all of them, maybe just web servers, maybe just db servers)
* user specifies hosts - remote machines on which Ansible will run commands
* users - the remote user is the user that Ansible will run the particular task as


    playbooks/web-notls.yml
===Tasks===


    playbooks/web-tls.yml
* each play has a list of tasks associated with it
* tasks are executed in order on all machines that match the host pattern
* ansible completes the task on all remote machines before it moves on to the next task


    playbooks/files/nginx.key
===Handlers===


    playbooks/files/nginx.crt
* handlers are "conditional tasks" - tasks that are triggered by other tasks
 
* the handlers are triggered once at the end of each block of tasks - this keeps them from being restarted over and over by a set of related tasks
    playbooks/files/nginx.conf
 
    playbooks/templates/index.html.j2
 
    playbooks/templates/nginx.conf.j2
</pre>
 
==Port configuration (Vagrantfile)==
 
We want to arrange the Vagrant machine so that we map the local port 8080 to the vagrant machine's port 80, and map the local port 8443 to the vagrant machine's port 443.
 
The Vagrantfile should be modified as follows:
 
<pre>
VAGRANTFILE_API_VERSION = "2"
 
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/xenial64"
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.network "forwarded_port", guest: 443, host: 8443
end
</pre>
 
Now instruct vagrant to reload from the Vagrantfile:
 
<pre>
$ vagrant reload
 
==> default: Forwarding ports...
    default: 80 => 8080 (adapter 1)
    default: 443 => 8443 (adapter 1)
    default: 22 => 2222 (adapter 1)
</pre>


==Simple playbook==
===Roles===


Here is a simple playbook for our secure nginx server:
* Roles are just a way of automatically loading variables, tasks, and task handlers.
* Roles use a known file structure to load appropriate vars/tasks/handlers
* A given play will specify a list of roles in the playbook - this indicates that ansible should look for variables/tasks/handlers that match up with those roles, and add them to the play
* Roles can be dependent on other roles (that's the purpose of the "meta" yaml file)


'''<code>web-notls.yml</code>:'''
Example:
* the following playbook entry will automatically populate the play with a list of variables, tasks, and handlers from the roles subdirectory
* the roles subdirectory will contain two subdirectories, common and web, with files for those roles


<pre>
<pre>
- name: Configure webserver with nginx
---
# playbook.yml
- name: example play
   hosts: webservers
   hosts: webservers
   become: True
   roles:
  tasks:
  - common
    - name: install nginx
  - web
      apt: name=nginx update_cache=yes
 
    - name: copy nginx config file
      copy: src=files/nginx.conf dest=/etc/nginx/sites-available/default
 
    - name: enable configuration
      file: >
        dest=/etc/nginx/sites-enabled/default
        src=/etc/nginx/sites-available/default
        state=link
 
    - name: copy index.html
      template: src=templates/index.html.j2 dest=/usr/share/nginx/html/index.html
        mode=0644
 
    - name: restart nginx
      service: name=nginx state=restarted
</pre>
</pre>


===Tags===


YAML truthy: <code>true, True, TRUE, yes, Yes, YES, on, On, ON, y, Y</code>
* Tags are a way of only running part of your playbook
* Think of tags as a filtering operation


YAML falsey: <code>false, False, FALSE, no, No, NO, off, Off, OFF, n, N</code>
===Idempotence===


* idempotent means you can run it multiple times with the same outcome as running it one time
* modules should check whether the desired final state has already been achieved, and exit without doing anything if so


=Example: Secure Nginx Server=


[[Ansible/Nginx Playbook]] - a page that walks through two example playbooks


* [https://charlesreid1.com/wiki/Ansible/Nginx_Playbook#Ansible_Playbook_Example_1:_Nginx_Server_Playbook Example 1: nginx server playbook]
* [https://charlesreid1.com/wiki/Ansible/Nginx_Playbook#Ansible_Playbook_Example_2:_Secure_Nginx_Server_Playbook Example 2: secure nginx server playbook]


=Full Stack Example: Django Celery RabbitMQ Postgres=


[[Ansible/Full Stack Playbook]] - a page that walks through a playbook for a full stack example


=Flags=


{{AnsibleFlag}}


 
[[Category:Ansible Playbooks]]
 
 
 
 
 
 
 
 
 
=Flags=
 
[[Category:Ansible]]
[[Category:Infrastructure]]
[[Category:Python]]

Latest revision as of 17:00, 10 December 2018

Overview

The Ansible documentation has a nice overview of playbooks: https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html

Brief Notes

Playbooks

  • playbooks are a way of executing commands on remote machines in a scripted way
  • can orchestrate multiple ordered processes
  • playbooks are written in YAML
  • each playbook can contain one or more plays
  • each play will target different servers (maybe all of them, maybe just web servers, maybe just db servers)
  • user specifies hosts - remote machines on which Ansible will run commands
  • users - the remote user is the user that Ansible will run the particular task as

Tasks

  • each play has a list of tasks associated with it
  • tasks are executed in order on all machines that match the host pattern
  • ansible completes the task on all remote machines before it moves on to the next task

Handlers

  • handlers are "conditional tasks" - tasks that are triggered by other tasks
  • the handlers are triggered once at the end of each block of tasks - this keeps them from being restarted over and over by a set of related tasks

Roles

  • Roles are just a way of automatically loading variables, tasks, and task handlers.
  • Roles use a known file structure to load appropriate vars/tasks/handlers
  • A given play will specify a list of roles in the playbook - this indicates that ansible should look for variables/tasks/handlers that match up with those roles, and add them to the play
  • Roles can be dependent on other roles (that's the purpose of the "meta" yaml file)

Example:

  • the following playbook entry will automatically populate the play with a list of variables, tasks, and handlers from the roles subdirectory
  • the roles subdirectory will contain two subdirectories, common and web, with files for those roles
---
# playbook.yml
- name: example play
  hosts: webservers
  roles:
   - common
   - web

Tags

  • Tags are a way of only running part of your playbook
  • Think of tags as a filtering operation

Idempotence

  • idempotent means you can run it multiple times with the same outcome as running it one time
  • modules should check whether the desired final state has already been achieved, and exit without doing anything if so

Example: Secure Nginx Server

Ansible/Nginx Playbook - a page that walks through two example playbooks

Full Stack Example: Django Celery RabbitMQ Postgres

Ansible/Full Stack Playbook - a page that walks through a playbook for a full stack example

Flags