Task 2. Create a simple playbook

1. Introduction

Playbooks are Ansible’s configuration, deployment, and orchestration language. They can describe a policy you want your remote systems to enforce, or a set of steps in a general IT process.

If Ansible modules are the tools in your workshop, playbooks are your instruction manuals, and your inventory of hosts are your raw material.

At a basic level, playbooks can be used to manage configurations of and deployments to remote machines. At a more advanced level, they can sequence multi-tier rollouts involving rolling updates, and can delegate actions to other hosts, interacting with monitoring servers and load balancers along the way.

While there’s a lot of information here, there’s no need to learn everything at once. You can start small and pick up more features over time as you need them.

Playbooks are designed to be human-readable and are developed in a basic text language. There are multiple ways to organize playbooks and the files they include, and we’ll offer up some suggestions on that and making the most out of Ansible.

https://docs.ansible.com/ansible/2.5/user_guide/playbooks.html

2. Create playbook

copy and paste the following content in a YAML playbookfile. let’s call it: /tmp/task2.yml

---
- hosts: production:&bigip
  remote_user: fchmainy
  gather_facts: false
  vars:
    username: "admin"
    password: "supernetops"
    app_name: "myApp"
    pool_name: "{{ app_name }}_pool"
    redirect_port: "80"
    vip_ip: "10.100.26.143"
    vip_port: "443"
    pool_members:
      - port: "80"
        host: "10.100.26.144"
      - port: "80"
        host: "10.100.26.145"

  tasks:
    - name: Create nodes
      bigip_node:
        server: "{{ inventory_hostname }}"
        user: "{{ username }}"
        password: "{{ password }}"
        host: "{{item.host}}"
        name: "{{item.host}}"
        validate_certs: False
      with_items: "{{pool_members}}"
      delegate_to: localhost

    - name: Create pool
      bigip_pool:
        server: "{{ inventory_hostname }}"
        user: "{{ username }}"
        password: "{{ password }}"
        name: "{{pool_name}}"
        lb_method: "round-robin"
        monitors: "/Common/http"
        validate_certs: False
      delegate_to: localhost

    - name: Add Pool members
      bigip_pool_member:
        server: "{{ inventory_hostname }}"
        user: "{{ username }}"
        password: "{{ password }}"
        name: "{{item.host}}"
        host: "{{item.host}}"
        port: "{{item.port}}"
        pool: "{{pool_name}}"
        validate_certs: False
      with_items: "{{pool_members}}"
      delegate_to: localhost

    - name: Add Virtual Server
      bigip_virtual_server:
        server: "{{ inventory_hostname }}"
        user: "{{ username }}"
        password: "{{ password }}"
        name: "{{ app_name }}_vs_https"
        destination: "{{ vip_ip }}"
        port: "{{ vip_port }}"
        all_profiles:
        - http
        - name: clientssl
          context: client-side
        pool: "{{pool_name}}"
        snat: "automap"
        irules:
        - "_sys_https_redirect"
        validate_certs: False
      delegate_to: localhost

    - name: Add Redirect Virtual Server
      bigip_virtual_server:
        server: "{{ inventory_hostname }}"
        user: "{{ username }}"
        password: "{{ password }}"
        name: "{{ app_name }}_vs_http_redirect"
        destination: "{{ vip_ip }}"
        port: "80"
        all_profiles:
        - http
        irules:
        - "_sys_https_redirect"
        validate_certs: False
      delegate_to: localhost

run the playbook using the following command:

$ ansible-playbook /tmp/task2.yml -vvv

*Note: You can run the playbook multiple time as F5 ansible modules are idempotent (https://en.wikipedia.org/wiki/Idempotence) *