Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ansible: 10 tips and tricks

Ansible: 10 tips and tricks

Tips and tricks for Ansible. Code at git.io/pK57

Steven Ringo

March 12, 2015
Tweet

More Decks by Steven Ringo

Other Decks in Programming

Transcript

  1. 10 tips & tricks
    Sydney Ansible Meetup,
    Thursday 12 March 2015
    Dynamic Clarity

    View Slide

  2. define variables with a
    JSON web service
    1

    View Slide

  3. {
    "server_ip": "192.168.0.44",
    "server_name": "dilbert"
    }
    https://api.site.com/config/staging

    View Slide

  4. {
    "server_ip": "10.10.11.34",
    "server_name": "pointy_haired_boss"
    }
    https://api.site.com/config/production

    View Slide

  5. multi-stage environments
    2

    View Slide

  6. !"" inventory
    # !"" _common
    # # $"" inventory
    # !"" production
    # # !"" group_vars
    # # # $"" all
    # # !"" host_vars
    # # $"" inventory -> ../_common/inventory
    # $"" staging
    # !"" group_vars
    # # $"" all
    # !"" host_vars
    # $"" inventory -> ../_common/inventory

    View Slide

  7. ansible_connection=local
    for local connections, use
    3

    View Slide

  8. single self-contained repo
    4

    View Slide

  9. remove global config

    (/usr/local/etc/ansible/hosts)
    keep SSH config in Ansible project
    check out repo and run!

    View Slide

  10. cloud-local control host
    5

    View Slide

  11. co-located on
    same VPC / VLAN

    View Slide

  12. ~zero latency
    no public SSH
    repo access required

    View Slide

  13. buildkite.com*
    for deployment
    6

    View Slide

  14. install agent on
    Ansible control host

    View Slide

  15. View Slide

  16. use roles
    7

    View Slide

  17. don’t use roles,
    use include

    View Slide

  18. roles can’t strictly be
    mixed with tasks

    View Slide

  19. pre_tasks:
    - remove host from load balancer
    pool
    roles:
    - rails
    tasks:
    - do stuff
    post_tasks:
    - return load balancer back to pool

    View Slide

  20. pre_tasks:
    - remove host from load balancer pool
    tasks:
    - do stuff
    - role: nginx
    - do more other stuff
    - role: rails
    post_tasks:
    - return load balancer back to pool

    View Slide

  21. roles/
    nginx/
    files/
    templates/
    tasks/
    handlers/
    vars/
    defaults/
    meta/

    View Slide

  22. $ ansible-galaxy init

    View Slide

  23. Ruby* modules
    8

    View Slide

  24. located in /library
    must be executable
    task name is file name

    View Slide

  25. #!/usr/bin/env ruby
    # WANT_JSON
    shebang warning!

    View Slide

  26. *any executable that can
    read a JSON formatted file
    (input)
    and returns JSON to stdout
    (output)

    View Slide

  27. instant offline access
    to documentation
    9

    View Slide

  28. Dash Alfred
    +

    View Slide

  29. 10
    style

    View Slide

  30. - name: Install Nginx.
    yum: name=nginx state=installed

    View Slide

  31. - name: copy something
    template:
    src: "templates/config.j2"
    dest: "/home/{{ deploy_user }}/config"
    owner: "{{ deploy_user }}"
    group: "{{ deploy_user }}"
    mode: 0755

    View Slide

  32. - name: copy something
    template: >
    src="templates/config.j2"
    dest="/home/{{ deploy_user }}/config"
    owner="{{ deploy_user }}"
    group="{{ deploy_user }}"
    mode=0755

    View Slide

  33. multiline for single parameter
    =
    good

    View Slide

  34. - name: decrypt the key on the server
    sudo_user: "{{ deploy_key_user }}"
    command: >
    openssl rsa
    -in {{ deploy_key_path }}.protected
    -out {{ deploy_key_path }}
    -passin pass:{{ deploy_key_secret }}

    View Slide

  35. git.io/pK57

    View Slide

  36. View Slide

  37. ansibleworkshop.com

    View Slide

  38. Steven Ringo | stevenringo | [email protected]
    Dynamic Clarity

    View Slide