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

Ansible hors des sentiers battus

Ansible hors des sentiers battus

Ansible permet de gérer le déploiement et la configuration d’un parc de machines. Si vous avez fait d’autres choix pour remplir ce rôle, sachez qu’Ansible peut encore vous faciliter la vie de bien des manières. Venez découvrir ses multiples facettes. Vous apprendrez comment collaborer efficacement avec Vagrant, Packer, Terraform, Puppet, Chef, traverser des serveurs de rebond et d’autres trucs et astuces qui feront à coup sûr d’Ansible votre nouveau couteau suisse d’administration.

Aurélien Maury

April 21, 2016
Tweet

More Decks by Aurélien Maury

Other Decks in Technology

Transcript

  1. #DevoxxFR #ansibleRocks SSH-fu 15 # $HOME/.ssh/config Host bastion Hostname 84.39.41.33

    User admin IdentityFile ~/.ssh/bastion_key Host 192.168.47.* ProxyCommand ssh -W %h:%p bastion User admin IdentityFile ~/.ssh/zone_key Host * ControlMaster auto ControlPath ~/.ssh/mux-%r@%h:%p ControlPersist 15m
  2. #DevoxxFR #ansibleRocks SSH-fu 16 # $HOME/.ssh/config Host bastion Hostname 84.39.41.33

    User admin IdentityFile ~/.ssh/bastion_key Host 192.168.47.* ProxyCommand ssh -W %h:%p bastion User admin IdentityFile ~/.ssh/zone_key Host * ControlMaster auto ControlPath ~/.ssh/mux-%r@%h:%p ControlPersist 15m
  3. #DevoxxFR #ansibleRocks SSH-fu 17 # $HOME/.ssh/config Host bastion Hostname 84.39.41.33

    User admin IdentityFile ~/.ssh/bastion_key Host 192.168.47.* ProxyCommand ssh -W %h:%p bastion User admin IdentityFile ~/.ssh/zone_key Host * ControlMaster auto ControlPath ~/.ssh/mux-%r@%h:%p ControlPersist 15m
  4. #DevoxxFR #ansibleRocks SSH-fu 18 # $WORKSPACE/ssh.cfg Host bastion Hostname 84.39.41.33

    User admin IdentityFile ~/.ssh/bastion_key Host 192.168.47.* ProxyCommand ssh -W %h:%p -F ssh.cfg bastion User admin IdentityFile ~/.ssh/zone_key Host * ControlMaster auto ControlPath ~/.ssh/mux-%r@%h:%p ControlPersist 15m
  5. #DevoxxFR #ansibleRocks Host Inventory 20 [web_servers] 192.168.47.11 192.168.47.12 192.168.47.13 [db_servers]

    192.168.47.10 [production:children] web_servers db_servers [production:vars] ansible_ssh_user=admin ansible_ssh_private_key_file=/home/ops/.ssh/id_rsa.prod
  6. #DevoxxFR #ansibleRocks Dynamic Inventory 22 --list { "databases" : {

    "hosts" : [ "host1.example.com", "host2.example.com" ], "vars" : { "a" : true } }, "webservers" : [ "host2.example.com", "host3.example.com" ], "atlanta" : { "hosts" : [ "host1.example.com", "host4.example.com"], "vars" : { "b" : false }, "children": [ "marietta", "5points" ] }, "marietta" : [ "host6.example.com" ] }
  7. #DevoxxFR #ansibleRocks Dynamic Inventory 23 --host $HOST { "favcolor" :

    "red", "ntpserver" : "wolf.example.com", "monitoring" : "pack.example.com" }
  8. #DevoxxFR #ansibleRocks Il y a sûrement un module pour ça

    27 Cloud - Amazon, Cloudstack, Openstack, VMWare, DO
  9. #DevoxxFR #ansibleRocks Il y a sûrement un module pour ça

    27 Cloud - Amazon, Cloudstack, Openstack, VMWare, DO Clustering - consul, zookeeper
  10. #DevoxxFR #ansibleRocks Il y a sûrement un module pour ça

    27 Cloud - Amazon, Cloudstack, Openstack, VMWare, DO Clustering - consul, zookeeper Commands - shell, script, expect
  11. #DevoxxFR #ansibleRocks Il y a sûrement un module pour ça

    27 Cloud - Amazon, Cloudstack, Openstack, VMWare, DO Clustering - consul, zookeeper Commands - shell, script, expect Database - mysql, postgresql, redis, riak
  12. #DevoxxFR #ansibleRocks Il y a sûrement un module pour ça

    27 Cloud - Amazon, Cloudstack, Openstack, VMWare, DO Clustering - consul, zookeeper Commands - shell, script, expect Database - mysql, postgresql, redis, riak Files - copy, fetch, lineinfile, template, unarchive
  13. #DevoxxFR #ansibleRocks Il y a sûrement un module pour ça

    27 Cloud - Amazon, Cloudstack, Openstack, VMWare, DO Clustering - consul, zookeeper Commands - shell, script, expect Database - mysql, postgresql, redis, riak Files - copy, fetch, lineinfile, template, unarchive Messaging - rabbitmq_[binding, exchange, queue, ...]
  14. #DevoxxFR #ansibleRocks Il y a sûrement un module pour ça

    27 Cloud - Amazon, Cloudstack, Openstack, VMWare, DO Clustering - consul, zookeeper Commands - shell, script, expect Database - mysql, postgresql, redis, riak Files - copy, fetch, lineinfile, template, unarchive Messaging - rabbitmq_[binding, exchange, queue, ...] Monitoring - zabbix, nagios, sensu, monit, datadog
  15. #DevoxxFR #ansibleRocks Il y a sûrement un module pour ça

    27 Cloud - Amazon, Cloudstack, Openstack, VMWare, DO Clustering - consul, zookeeper Commands - shell, script, expect Database - mysql, postgresql, redis, riak Files - copy, fetch, lineinfile, template, unarchive Messaging - rabbitmq_[binding, exchange, queue, ...] Monitoring - zabbix, nagios, sensu, monit, datadog Network - A10, F5, Openswitch, cumulus, get_url
  16. #DevoxxFR #ansibleRocks Il y a sûrement un module pour ça

    27 Cloud - Amazon, Cloudstack, Openstack, VMWare, DO Clustering - consul, zookeeper Commands - shell, script, expect Database - mysql, postgresql, redis, riak Files - copy, fetch, lineinfile, template, unarchive Messaging - rabbitmq_[binding, exchange, queue, ...] Monitoring - zabbix, nagios, sensu, monit, datadog Network - A10, F5, Openswitch, cumulus, get_url Notification - jabber, slack, mail, irc, hipchat
  17. #DevoxxFR #ansibleRocks Il y a sûrement un module pour ça

    27 Cloud - Amazon, Cloudstack, Openstack, VMWare, DO Clustering - consul, zookeeper Commands - shell, script, expect Database - mysql, postgresql, redis, riak Files - copy, fetch, lineinfile, template, unarchive Messaging - rabbitmq_[binding, exchange, queue, ...] Monitoring - zabbix, nagios, sensu, monit, datadog Network - A10, F5, Openswitch, cumulus, get_url Notification - jabber, slack, mail, irc, hipchat Packaging - apt, yum, pip, bower, npm, homebrew
  18. #DevoxxFR #ansibleRocks Il y a sûrement un module pour ça

    27 Cloud - Amazon, Cloudstack, Openstack, VMWare, DO Clustering - consul, zookeeper Commands - shell, script, expect Database - mysql, postgresql, redis, riak Files - copy, fetch, lineinfile, template, unarchive Messaging - rabbitmq_[binding, exchange, queue, ...] Monitoring - zabbix, nagios, sensu, monit, datadog Network - A10, F5, Openswitch, cumulus, get_url Notification - jabber, slack, mail, irc, hipchat Packaging - apt, yum, pip, bower, npm, homebrew Source control - git, gitlab, hg, subversion
  19. #DevoxxFR #ansibleRocks Il y a sûrement un module pour ça

    27 Cloud - Amazon, Cloudstack, Openstack, VMWare, DO Clustering - consul, zookeeper Commands - shell, script, expect Database - mysql, postgresql, redis, riak Files - copy, fetch, lineinfile, template, unarchive Messaging - rabbitmq_[binding, exchange, queue, ...] Monitoring - zabbix, nagios, sensu, monit, datadog Network - A10, F5, Openswitch, cumulus, get_url Notification - jabber, slack, mail, irc, hipchat Packaging - apt, yum, pip, bower, npm, homebrew Source control - git, gitlab, hg, subversion System - known_host, authorized_key, user, group
  20. #DevoxxFR #ansibleRocks Il y a sûrement un module pour ça

    27 Cloud - Amazon, Cloudstack, Openstack, VMWare, DO Clustering - consul, zookeeper Commands - shell, script, expect Database - mysql, postgresql, redis, riak Files - copy, fetch, lineinfile, template, unarchive Messaging - rabbitmq_[binding, exchange, queue, ...] Monitoring - zabbix, nagios, sensu, monit, datadog Network - A10, F5, Openswitch, cumulus, get_url Notification - jabber, slack, mail, irc, hipchat Packaging - apt, yum, pip, bower, npm, homebrew Source control - git, gitlab, hg, subversion System - known_host, authorized_key, user, group Windows - trucs windows inconnus de moi
  21. #DevoxxFR #ansibleRocks Appel à un module 28 - name: un

    joli titre c’est mieux apt: pkg="tmux" state=present update_cache=yes - name: un joli titre c’est mieux apt: pkg="screen" state=present
  22. #DevoxxFR #ansibleRocks with_items 29 - name: un joli titre c’est

    mieux apt: pkg="{{ item }}" state=present update_cache=yes with_items: - tmux - screen
  23. #DevoxxFR #ansibleRocks with_items 30 - name: add several users user:

    name={{ item.name }} state=present groups={{ item.groups }} with_items: - { name: 'testuser1', groups: 'wheel' } - { name: 'testuser2', groups: 'root' }
  24. #DevoxxFR #ansibleRocks with_* 31 with_file: - file_1 - file_2 with_fileglob:

    - files/*.yml with_together: - ['a','b'] - [1,2] with_subelements, with_sequence, with_random_choice, with_indexed_items, with_dict
  25. #DevoxxFR #ansibleRocks Donnez vie à vos snippets 32 15 lignes

    de Python Faire un module Ansible ? Installer un module Ansible ?
  26. #DevoxxFR #ansibleRocks Donnez vie à vos snippets 32 15 lignes

    de Python Faire un module Ansible ? posez le dans ./library Installer un module Ansible ?
  27. #DevoxxFR #ansibleRocks Structure 34 . ├── README.md ├── defaults │

    └── main.yml --> variables par défaut ├── files --> fichiers statiques ├── handlers │ └── main.yml --> handlers ├── meta │ └── main.yml --> fiche d'info et dépendances ├── tasks │ └── main.yml --> tâches (appels de modules) ├── templates --> templates Jinja2 ├── tests │ ├── inventory │ └── test.yml └── vars └── main.yml --> variables fortes
  28. #DevoxxFR #ansibleRocks Structure 34 . ├── README.md ├── defaults │

    └── main.yml --> variables par défaut ├── files --> fichiers statiques ├── handlers │ └── main.yml --> handlers ├── meta │ └── main.yml --> fiche d'info et dépendances ├── tasks │ └── main.yml --> tâches (appels de modules) ├── templates --> templates Jinja2 ├── tests │ ├── inventory │ └── test.yml └── vars └── main.yml --> variables fortes ansible-galaxy init mon_role_amoi
  29. #DevoxxFR #ansibleRocks ansible-galaxy 36 # requirements.yml - src: yatesr.timezone -

    src: https://github.com/bennojoy/nginx - src: https://github.com/bennojoy/nginx version: master name: nginx_role - src: https://some.webserver.example.com/files/master.tar.gz name: http-role
  30. #DevoxxFR #ansibleRocks ansible-galaxy 36 # requirements.yml - src: yatesr.timezone -

    src: https://github.com/bennojoy/nginx - src: https://github.com/bennojoy/nginx version: master name: nginx_role - src: https://some.webserver.example.com/files/master.tar.gz name: http-role ansible-galaxy install -r requirements.yml
  31. #DevoxxFR #ansibleRocks YAML 38 —-- simplest_num: 42 simplest_str: "Terry Pratchett"

    # Qui lit les commentaires de toutes façons ? some_list: - "DON'T THINK OF IT AS DYING" - "JUST THINK OF IT AS" - "LEAVING EARLY TO AVOID THE RUSH" some_dict: key: "value" other_key: 10 list_of_dict: - { indentation: "is", the_key: "si si" }
  32. #DevoxxFR #ansibleRocks YAML 39 —-- simplest_num: 42 simplest_str: "Terry Pratchett"

    # Qui lit les commentaires de toutes façons ? some_list: - "DON'T THINK OF IT AS DYING" - "JUST THINK OF IT AS" - "LEAVING EARLY TO AVOID THE RUSH" some_dict: key: "value" other_key: 10 list_of_dict: - { indentation: "is", the_key: "si si" }
  33. #DevoxxFR #ansibleRocks YAML 40 —-- simplest_num: 42 simplest_str: "Terry Pratchett"

    # Qui lit les commentaires de toutes façons ? some_list: - "DON'T THINK OF IT AS DYING" - "JUST THINK OF IT AS" - "LEAVING EARLY TO AVOID THE RUSH" some_dict: key: "value" other_key: 10 list_of_dict: - indentation: "is" the_key: "si si"
  34. #DevoxxFR #ansibleRocks Facts 42 ansible -m setup localhost localhost |

    SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "192.168.42.2", "192.168.99.1" ], "ansible_all_ipv6_addresses": [ "fe80::4e8d:79ff:fee8:54fe%en1", "fe80::f0ad:11ff:fee9:fcc8%awdl0" ], "ansible_architecture": "x86_64", "ansible_awdl0": { "device": "awdl0", "flags": [ "UP", "BROADCAST", "RUNNING", "PROMISC", "SIMPLEX", "MULTICAST"
  35. #DevoxxFR #ansibleRocks Fact caching 44 # ansible.cfg [defaults] gathering =

    smart fact_caching = redis fact_caching_timeout = 7200
  36. #DevoxxFR #ansibleRocks Fact caching 45 # ansible.cfg [defaults] gathering =

    smart fact_caching = jsonfile fact_caching_connection = /tmp/facts_cache fact_caching_timeout = 7200
  37. #DevoxxFR #ansibleRocks Set_fact 46 —-- - hosts: target_group become: yes

    pre_tasks: - shell: > {{ playbook_dir }}/scripts/xml2yaml.py xml/* register: yaml_out
  38. #DevoxxFR #ansibleRocks Set_fact 46 —-- - hosts: target_group become: yes

    pre_tasks: - shell: > {{ playbook_dir }}/scripts/xml2yaml.py xml/* register: yaml_out "--- styles: - grindcore - disco - punk"
  39. #DevoxxFR #ansibleRocks Set_fact 46 —-- - hosts: target_group become: yes

    pre_tasks: - shell: > {{ playbook_dir }}/scripts/xml2yaml.py xml/* register: yaml_out "--- styles: - grindcore - disco - punk" - set_fact: table_oauth: "{{ yaml_out.stdout | from_yaml }}"
  40. #DevoxxFR #ansibleRocks Précédence 48 role defaults inventory vars inventory group_vars

    inventory host_vars playbook group_vars playbook host_vars host facts registered vars set_facts play vars play vars_prompt play vars_files role and include vars block vars (seulement pour les tâches du bloc) task vars (seulement pour la tâche) extra vars (ultime)
  41. #DevoxxFR #ansibleRocks Précédence 49 role defaults inventory vars inventory group_vars

    inventory host_vars playbook group_vars playbook host_vars host facts registered vars set_facts play vars play vars_prompt play vars_files role and include vars block vars (seulement pour les tâches du bloc) task vars (seulement pour la tâche) extra vars (ultime)
  42. #DevoxxFR #ansibleRocks Vagrant provisioner remote 55 config.vm.provision "ansible" do |ansible|

    ansible.groups = { "web_servers" => ["vm_one", "vm_two"], "db_servers" => ["vm_three"], "production:children" => [ "web_servers", "db_servers" ], "all_groups:children" => ["production"] } ansible.playbook = "upgrade_stack.yml" end
  43. #DevoxxFR #ansibleRocks 56 config.vm.provision "ansible" do |ansible| ansible.groups = {

    "web_servers" => ["vm_one", "vm_two"], "db_servers" => ["vm_three"], "production:children" => [ "web_servers", "db_servers" ], "all_groups:children" => ["production"] } ansible.playbook = "upgrade_stack.yml" end Vagrant provisioner remote
  44. #DevoxxFR #ansibleRocks 57 config.vm.provision "ansible" do |ansible| ansible.groups = {

    "web_servers" => ["vm_one", "vm_two"], "db_servers" => ["vm_three"], "production:children" => [ "web_servers", "db_servers" ], "all_groups:children" => ["production"] } ansible.playbook = "upgrade_stack.yml" end Vagrant provisioner remote
  45. #DevoxxFR #ansibleRocks Packer : Elevator pitch 60 Boot VM •

    OpenStack • AWS • Docker • Qemu • GCE • …
  46. #DevoxxFR #ansibleRocks Packer : Elevator pitch 60 Boot VM Provision

    • OpenStack • AWS • Docker • Qemu • GCE • … • Puppet • Saltstack • Chef • Ansible • Shell • …
  47. #DevoxxFR #ansibleRocks Packer : Elevator pitch 60 Boot VM Provision

    Snapshot • OpenStack • AWS • Docker • Qemu • GCE • … • Puppet • Saltstack • Chef • Ansible • Shell • …
  48. #DevoxxFR #ansibleRocks build-me.json 61 "provisioners": [
 {
 "type": "file",
 "source":

    "ansible/requirements.yml",
 "destination": "/tmp/requirements.yml"
 },
 {
 "type": "shell",
 "scripts": [
 "scripts/ansible-seed.sh"
 ]
 },
 {
 "type": "ansible-local",
 "playbook_dir": "ansible",
 "playbook_file": "ansible/bootstrap.yml"
 } }
  49. #DevoxxFR #ansibleRocks build-me.json 62 "provisioners": [
 {
 "type": "file",
 "source":

    "ansible/requirements.yml",
 "destination": "/tmp/requirements.yml"
 },
 {
 "type": "shell",
 "scripts": [
 "scripts/ansible-seed.sh"
 ]
 },
 {
 "type": "ansible-local",
 "playbook_dir": "ansible",
 "playbook_file": "ansible/bootstrap.yml"
 } }
  50. #DevoxxFR #ansibleRocks build-me.json 63 "provisioners": [
 {
 "type": "file",
 "source":

    "ansible/requirements.yml",
 "destination": "/tmp/requirements.yml"
 },
 {
 "type": "shell",
 "scripts": [
 "scripts/ansible-seed.sh"
 ]
 },
 {
 "type": "ansible-local",
 "playbook_dir": "ansible",
 "playbook_file": "ansible/bootstrap.yml"
 } }
  51. #DevoxxFR #ansibleRocks build-me.json 64 "provisioners": [
 {
 "type": "file",
 "source":

    "ansible/requirements.yml",
 "destination": "/tmp/requirements.yml"
 },
 {
 "type": "shell",
 "scripts": [
 "scripts/ansible-seed.sh"
 ]
 },
 {
 "type": "ansible-local",
 "playbook_dir": "ansible",
 "playbook_file": "ansible/bootstrap.yml"
 } }
  52. #DevoxxFR #ansibleRocks Points de greffe 69 # playbook.yml - shell:

    cat terraform.tfstate register: raw_tfstate - set_facts: tfstate: "{{ raw_tfstate.stdout | from_json }}"
  53. #DevoxxFR #ansibleRocks Points de greffe 69 # playbook.yml - shell:

    cat terraform.tfstate register: raw_tfstate - set_facts: tfstate: "{{ raw_tfstate.stdout | from_json }}" tfstate.modules[1].resources['aws_route53_record.elb_cgate'].primary.attributes.zone_id
  54. #DevoxxFR #ansibleRocks Points de greffe 70 # main.tf resource "aws_route53_record"

    "monitor" { zone_id = "${var.network.route53_zone_id}" name = "monitor" type = "A" ttl = "300" records = ["${aws_instance.server.private_ip}"] }
  55. #DevoxxFR #ansibleRocks Points de greffe 70 # main.tf resource "aws_route53_record"

    "monitor" { zone_id = "${var.network.route53_zone_id}" name = "monitor" type = "A" ttl = "300" records = ["${aws_instance.server.private_ip}"] } output "monitor_zone_id" { value = "${aws_route53_record.monitor.zone_id}" }
  56. #DevoxxFR #ansibleRocks Points de greffe 70 # main.tf resource "aws_route53_record"

    "monitor" { zone_id = "${var.network.route53_zone_id}" name = "monitor" type = "A" ttl = "300" records = ["${aws_instance.server.private_ip}"] } output "monitor_zone_id" { value = "${aws_route53_record.monitor.zone_id}" } terraform output monitor_zone_id ZSQ642E3K7JC5
  57. #DevoxxFR #ansibleRocks Points de greffe 71 # playbook.yml - shell:

    cat terraform.tfstate register: raw_tfstate - set_facts: tfstate: "{{ raw_tfstate.stdout | from_json }}" tfstate.modules[1].resources['aws_route53_record.elb_cgate'].primary.attributes.zone_id
  58. #DevoxxFR #ansibleRocks Points de greffe 72 # playbook.yml - shell:

    terraform output monitor_zone_id register: sh_monitor_zid - set_facts: monitor_zid: "{{ sh_monitor_zid.stdout }}"