Ansible: 10 tips and tricks

Ansible: 10 tips and tricks

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

D02b5e0b9a348fa8d9bd2cac025eead3?s=128

Steven Ringo

March 12, 2015
Tweet

Transcript

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

    2015 Dynamic Clarity
  2. define variables with a JSON web service 1

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

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

  5. multi-stage environments 2

  6. !"" inventory # !"" _common # # $"" inventory #

    !"" production # # !"" group_vars # # # $"" all # # !"" host_vars # # $"" inventory -> ../_common/inventory # $"" staging # !"" group_vars # # $"" all # !"" host_vars # $"" inventory -> ../_common/inventory
  7. ansible_connection=local for local connections, use 3

  8. single self-contained repo 4

  9. remove global config
 (/usr/local/etc/ansible/hosts) keep SSH config in Ansible project

    check out repo and run!
  10. cloud-local control host 5

  11. co-located on same VPC / VLAN

  12. ~zero latency no public SSH repo access required

  13. buildkite.com* for deployment 6

  14. install agent on Ansible control host

  15. None
  16. use roles 7

  17. don’t use roles, use include

  18. roles can’t strictly be mixed with tasks

  19. pre_tasks: - remove host from load balancer pool roles: -

    rails tasks: - do stuff post_tasks: - return load balancer back to pool
  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
  21. roles/ nginx/ files/ templates/ tasks/ handlers/ vars/ defaults/ meta/

  22. $ ansible-galaxy init

  23. Ruby* modules 8

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

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

  26. *any executable that can read a JSON formatted file (input)

    and returns JSON to stdout (output)
  27. instant offline access to documentation 9

  28. Dash Alfred +

  29. 10 style

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

  31. - name: copy something template: src: "templates/config.j2" dest: "/home/{{ deploy_user

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

    owner="{{ deploy_user }}" group="{{ deploy_user }}" mode=0755
  33. multiline for single parameter = good

  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 }}
  35. git.io/pK57

  36. None
  37. ansibleworkshop.com

  38. Steven Ringo | stevenringo | steven@dynamicclarity.com Dynamic Clarity