mardi 10 mars 2015

Activation du cache Ansible par fichier JSON

Lorsque vous lancez Ansible, vous avez déjà dû remarquer que le démarrage peut prendre un certain temps : il s'agit de la phase de récupération des informations sur la machine. Ceci se manifeste au début du lancement d'un playbook sous la forme suivante :

GATHERING FACTS ******************************************* 
ok: [localhost]

Pendant cette phase, Ansible va collecter tout un tas d'indicateur qui vont caractériser votre machine. On va y retrouver pêle-mêle le type de processeur, les disques de la machine, les adresses IP etc. Seul petit problème cette phase prend quelques secondes (voir plus certaines fois) et peut devenir vite agaçante dans le cas où vous mettez au point vos procédures d'installation.

Heureusement, les petits gars d'Ansible ont introduit un petit mécanisme de cache de ces éléments (des facts dans le jargon Ansiblien). Vous pouvez effectivement stocker ça dans un serveur redis.

Comme je n'ai pas trop l'habitude d'utiliser redis et que je n'avais pas envie de passer du temps à faire l'installation de ce nouvel élément sur mon poste perso, je me suis posé la question s'il ne serait pas possible d'utiliser quelque chose de plus simple ... Et la réponse est oui, avec des fichiers au format JSON.

Activation du cache via fichier JSON

Ouvrons notre fichier /etc/ansible/ansible.cfg et allons modifier la valeur du paramètre gathering pour le passer à la valeur smart :

# smart - gather by default, but don't regather if already gathered
# implicit - gather by default, turn off with gather_facts: False
# explicit - do not gather by default, must say gather_facts: True
gathering = smart

Si vous lancez maintenant Ansible, vous ne verrez pas trop de différence. Ce paramètre peut néanmoins être avantageux si vous lancez plusieurs rôles sur les mêmes machines. Dans ce cas, Ansible ne récupérera les caractéristiques de vos machines qu'une seule fois.

Allons un peu plus loin et modifions maintenant les valeurs de fact_caching :

# if set to a persistant type (not 'memory', for example 'redis') fact values
# from previous runs in Ansible will be stored.  This may be useful when
# wanting to use, for example, IP information from one group of servers
# without having to talk to them in the same playbook run to get their
# current IP information.
# fact_caching = memory
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible

Nous avons activé le stockage des fichiers d'inventaire dans le répertoire /tmp/ansible. Chaque serveur verra ses caractéristiques stockées au format JSON.

A noter l'existence d'un paramètre fact_caching_timeout qui vous permettra de contrôler la fraîcheur de vos inventaires.

Différence de vitesse d'exécution dû au cache

Voyons maintenant l'impact des performances avec le test tout simple suivant :

---
# Playbook de test

- name: "Test"
  hosts: "localhost"
  tasks:
    - debug: msg=Lancement réussi

On ne fait qu'afficher un message. Ansible procédera néanmoins à la phase d'inventaire ce qui nous permettra de mesurer ce temps.

Premier lancement

Lançons une première fois notre playbook à froid :

$ time ansible-playbook test.yml 

PLAY [Test] ************************************************* 

GATHERING FACTS ********************************************* 
ok: [localhost]

TASK: [debug msg=Lancement réussi] ************************** 
ok: [localhost] => {
    "msg": "Lancement"
}

PLAY RECAP ************************************************** 
localhost    : ok=2    changed=0    unreachable=0    failed=0   


real    0m2.300s
user    0m1.531s
sys     0m0.708s

Un coup d'oeil dans le répertoire /tmp/ansible :

$ ls /tmp/ansible/
localhost

Ansible a créé un fichier de cache. Un coup d'oeil nous permet de voir qu'il s'agit d'un fichier au format JSON.

Lancement avec cache

Maintenant que nous disposons d'un cache, mesurons à nouveau ce temps d'exécution :

$ time ansible-playbook test.yml 

PLAY [Test] ************************************************* 

TASK: [debug msg=Lancement réussi] ************************** 
ok: [localhost] => {
    "msg": "Lancement"
}

PLAY RECAP ************************************************** 
localhost    : ok=1    changed=0    unreachable=0    failed=0   


real    0m0.303s
user    0m0.242s
sys     0m0.044s

Nous venons de passer de 2,3 secondes à 0,3 seconde soit un gain de 2 secondes.

Pour conclure

Comme nous l'avons vu, le gain n'est pas négligeable (surtout en phase de mise au point) et surtout introduit une notion de persistance de l'information d'une exécution à l'autre. En effet, vous pouvez maintenant récupérer une information venant d'un autre serveur sans pour autant avoir à vous y connecter à chaque fois.

18 commentaires:

  1. We are appreciate with your valuable information. Visit OGEN Infosystem for professional Website Designing and PPC Services at affordable price.
    Website Designing Company in Delhi

    RépondreSupprimer
  2. still much uncertainty around what hybrid events will look like and the best practices for engaging hybrid audiences, but it's already clear that they offer new personalization opportunities. virtual edge and speaker bio example

    RépondreSupprimer
  3. Connect with the best SEO Company in Delhi, if you are looking to hire the best digital marketing service to increase your online business. PromoteDial can be the right choice for you.

    RépondreSupprimer
  4. If you want a attractive & user-friendly website then you must choose to connect with the website design company in hyderabad so that you can increase your online business.

    RépondreSupprimer
  5. Get the best flat at an affordable price. You can connect with Exotica Housing to get your dream house. We have launched many projects in Noida like Dreamville. Luxury residential projects in sector 79 Noida

    RépondreSupprimer
  6. We are the best tent supplier and manufacturer company in Dubai, UAE. We provide all types of tents, car parking shades etc. Connect with us for more information. Tent Manufacturers in UAE

    RépondreSupprimer
  7. Thank you so much for your valuable information, it help us to grow and learn things. So thank you so much again and keep up the good work
    [url=https://www.cssfounder.com/usa/web-design-company-dallas/]Web Design Company Dallas[/url]

    RépondreSupprimer
  8. Do you want to have a well-designed website that is perfect for your business then you need to contact us? We are providing the best web design service in the field of this website design, to contact us click here.
    Website design company Brasov

    RépondreSupprimer
  9. We are the best immigration service provider all over the world and our service is faster than one else. For more information please visit our site.
    Residency Visa in Malta

    RépondreSupprimer
  10. Do you want to get more online credit from your business, then you should contact CSS Founder because to get online credit you will need a good and easily designed website, and this works no one can do it better than CSS Founder, then why delay, contact immediately. And Get more online credits Thank you.
    Website design company Dnipro

    RépondreSupprimer
  11. I would like to say that this blog really convinced me to do it! Thanks, very good post. Luxury Resorts in Lansdowne

    RépondreSupprimer
  12. It is very easy to get Google Ads designed for your business, any digital marketing and Google Ads company can do it, but to bring Google Ads to the first page and a good and people-like Google Ads can only design Promotedial Google Ads Services in Shivamoga

    RépondreSupprimer
  13. CSS Founder is the best website design company that provides the best services in this digital world, if you want to increase your business through a website or grow your business digital way then contact us. Web designing company in Glaway

    RépondreSupprimer
  14. Great Information. If you are looking for Patient Care Services in Ghaziabad, then Accepthealthcare will be the best choice for you. Nursing Care Service

    RépondreSupprimer
  15. Thanks for your nice blog. contact for designing and Development of your Website at affordable price. website design company in naples Call us:- +91-8860080044 or send Email at : info@cssfounder.com

    RépondreSupprimer
  16. Such an amazing post, You can book your Visa now. Turkey visa apply Send Email: contact@instadubaivisa.com or email@yoursite.com

    RépondreSupprimer
  17. Saudi Arabia Student Visa
    Get your student visa through "Visits Visa" for Saudi Arabia. You can apply online for a visa on this website because its services are secure and fast.

    RépondreSupprimer
  18. Saudi Arabia Visa apply online
    It's quick, easy, and secure. Get your visa today and start planning your unforgettable adventure!

    RépondreSupprimer