How to migrate from ansible vault to hashicorp vault

Ansible Vault

Today, all our project passwords are stored in ansible vault files, on file per environnement, stored in a « passwords » directory

They are loaded at the beginning of the main playbook :

- hosts: all:!localhost
  any_errors_fatal: true
  gather_facts: true
    - include_vars: password/password_{{ env }}.yml
      connection: local
      no_log: True
        - password
        - always

Usually, to add or modify a password, we have to fetch the ansible vault key from a remote server in the environment ; copy it into our workspace, then run the command

ansible-vault edit --vault-id passwords/password_dev.yml

Then we can edit the password, and re-commit / push the new vaulted password file to the git repository.

Hashicorp Vault

We are planning to migrate from ansible vault to hashicorp vault. This will simplify password management / re-generation with the great vault-ui.


To simplify the migration to Hashicorp Vault I’ve developped a tiny script to :

  • decrypt all ansible vault password files
  • convert each password file to json
  • import each password file into hashicorp vault
  • replace all passwords with the ansible lookup  hashivault into the yaml file.

The requirement to this scripts are :

  • jq : for json parsing
  • curl : for hashicorp vault api authentication
  • j2y : for converting yaml into json
  • vault binary : for password import into hashicorp vault
  • sed : for yaml replacement
  • All ansible vault keys are in current directory (named « .vault.<env>« )

here is the script :


export VAULT_ADDR=
export VAULT_ROLE_ID=0acee709-b16c-4abd-20a3-12159d6c8b36
export VAULT_SECRET_ID=fcf81d75-bd31-6e8f-239b-fc09c5dd744e

for i in $(find . -name 'password_*.yml'); do
  file=$(echo $i | cut -d '/' -f 2)
  env=$(echo $file | sed 's/password_//' | cut -d '.' -f 1)
  if [ -f ".vault.$env" ]; then
      cp $i $i.decrypt
      ansible-vault decrypt --vault-id .vault.$nna.$env $i.decrypt
      cp $i $i.decrypt
  # converting to json $i.decrypt
  j2y -r $i.decrypt > password_$env.json
  rm -f $i.decrypt

  # Authenticate to $VAULT_ADDR
  data="{\"role_id\": \"${VAULT_ROLE_ID}\", \"secret_id\": \"${VAULT_SECRET_ID}\" }"
  export VAULT_TOKEN=$(curl -s -k -X POST ${VAULT_ADDR}/v1/auth/approle/login -d "$data" | jq '.auth.client_token')

  vault write secret/ida/$nna/$env @password_$env.json

  # converting yml file with lookup hashi_vault
  cp $i.decrypt $
  sed -i "s!\([^:]*\):.*!\1: \"{{ lookup('hashi_vault', 'secret=secret/app/$env:\1') }}\"!" $
  dos2unix $

Now I just have to run the script and all my passwords are migrated.


To use the ansible lookup hashivault plugin the environment variables VAULT_ADDR and VAULT_TOKEN must be exported.

All our playbooks are launched from jenkins, So we just have to connect to hashicorp vault from Jenkins before ansible playbook.

To do that we use the Hashicorp Vault Jenkins Plugin that add a new Jenkins Hashicorp Vault credentialtials scoped to our application folder. Then before each build, the vault plugin will export the required variables.

Finally the ansible lookup plugin will fetch the passwords directly from hashicrop vault at runtime 🙂


Prometheus : Monitor docker services with grafana

Here is a little tutorial to setup Prometheus monitoring for a local docker daemon and host metrics with docker-compose and :

  • prometheus node exporter for system metrics
  • cadvisor for docker metrics

First create a prometheus.yml config file like this :

    - static_configs:
      - targets:
#       - alertmanager:9093
 # - "first_rules.yml"
 # - "second_rules.yml"
  - job_name: 'prometheus'
      - targets: ['localhost:9090']
      - files:
        - '/etc/prometheus/file_sd_configs.yml'

And a file service discovery config file_sd_configs.yml:

- targets:
  - grafana:3000
  - prom-node-exporter:9100
  - cadvisor:8080

Now it’s time to start all services with a docker-compose file.

I’m still using the great traefik for reverse proxy and ssl let’s encrypt certificate generation.

So here is my docker-compose :

version: '2'
    image: traefik
    hostname: traefik
    container_name: traefik
    command: -c /etc/traefik/traefik.toml
      - "80:80"
      - "443:443"
      - "/data/traefik/traefik.toml:/etc/traefik/traefik.toml"
      - "/data/traefik/acme.json:/etc/traefik/acme.json"
      - "/data/traefik/ssl:/etc/traefik/ssl"
      - "/var/run/docker.sock:/var/run/docker.sock"

    image: prom/prometheus
    hostname: prometheus
    container_name: prometheus
      - "/data/docker/volumes/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml"
       - "/data/docker/volumes/prometheus/file_sd_configs.yml:/etc/prometheus/file_sd_configs.yml"
      - ""
    image: prom/node-exporter
    hostname: prom-node-exporter
    container_name: prom-node-exporter
      - "traefik.enable=false"
    image: google/cadvisor:latest
    hostname: cadvisor
    container_name: cadvisor
      - "/:/rootfs:ro"
      - "/var/run:/var/run:rw"
      - "/sys:/sys:ro"
      - "/var/lib/docker/:/var/lib/docker:ro"
      - "traefik.enable=false"
    hostname: grafana
    container_name: grafana
    image: grafana/grafana
      - GF_INSTALL_PLUGINS=grafana-clock-panel,grafana-piechart-panel
      - /data/grafana:/var/lib/grafana
      - ""

Then I needed to create a prometheus datasource in grafana

And finally I’ve just imported some public grafana dashboards. 

And voila 🙂

Genymobile Screen Copy

Juste un petit post pour parler de ma découverte du jour : scrcpy !

Un petit outil qui permet de faire du miroir d’écran Android sur son PC via USB (adb).

Pour cela, il suffit simplement de télécharger la dernière version de scrcpy, d’activer le debug USB dans les options développeur de son téléphone et de lancer scrcpy !

C’est magique !

Merci Genymobile, ça peut être bien pratique 🙂


Ansible 2.5 grafana modules


At work, we needed to automatize grafana installation and grafana provisionning (datasources, plugins and dashboards).

So I’ve created 3 new ansible modules that will be released with the next version of ansible 2.5

The first module is grafana_datasource. If you have to create a lot of different datsources for your grafana instance in multiples organisations, I suggest you to use it.

with a single ansible task, you can create all your datasources. For example if I want to create multiple datasources :

 - name: create elasticsearch datasource
     name: "{{ }}"
     grafana_url: "{{ grafana_url }}"
     grafana_user: "{{ grafana_user }}"
     grafana_password: "{{ grafana_password }}"
     ds_type: "{{ item.type }}"
     url: "{{ item.url }}"
     database: "{{ item.database }}"
     basic_auth_user: "{{ item.basic_auth_user | default('') }}"
     basic_auth_password: "{{ item.basic_auth_password | default('') }}"
     esVersion: "{{ item.es_version | default(5) }}"
     time_field": "{{ item.time_field | default('@timestamp') }}"
     state: present
  with_items: "{{ grafana_datasources }}"

where the grafana_datasources variable is :

  - name: "es_index1"
    ds_type: "elasticsearch"
    url: ""
    database: "index_[]"
    basic_auth_user: "grafana"
    basic_auth_password: "{{ grafana_es_password }}"
    es_version: 56
  - name: "influxdb"
    ds_type: "influxdb"
    url: ""
    database: "telegraf"


The second module is grafana_plugin. With this one, you can automate the installation and the upgrade of all your grafana plugins. For example :

 - name: install - update Grafana piechart panel plugin
     name: grafana-piechart-panel
     version: latest

And the last one is grafana_dashboard. This one is very great because it allow you to import or backup all your existing dashboards.

  - name: import grafana dashboard foo
     grafana_api_key: XXXXXXXXXXXX
     state: present
     message: "updated by ansible"
     overwrite: true
     path: /path/to/dashboards/foo.json

 - name: export dashboard
     grafana_api_key: XXXXXXXXXXXX
     state: export
     slug: foo
     path: /path/to/dashboards/foo.json

Hope thoses new ansible modules will be usefull for someone 🙂

If you have some suggestion of missing feature in this modules, you can comment in this article or make a pull requests in ansible github repo.

HAPROXY : client certificate validation

Today at the office, the security team ask me to secure our reverse proxy by adding a client certificate validation to only trust the client host CN.

So here is my method to verify the client certificate CN according to the expected one :

frontend frontend_foo
  mode tcp
  bind *:443 ssl crt /etc/ssl/certs/ ca-file /etc/ssl/certs/autorite_chain_haproxy.pem crl-file /etc/ssl/certs/crl-bundle_haproxy.pem verify required ca-ignore-err all crt-ignore-err all
 default_backend backend_foo

backend backend_foo
  mode tcp
  option httpchk

  acl cert_from_trusted_client ssl_c_s_dn(CN) -m reg ^trusted\.client\.(site1|site2)\.company\.(com|fr)$
  tcp-response inspect-delay 2s
  tcp-response content reject unless cert_from_trusted_client

  server srv_load01 check ssl crt /etc/ssl/certs/ ca-file /etc/ssl/certs/autorite_chain_haproxy.pem verify required

With this configuration, only hosts with a certificate with a CN like  « » , « », « », « » can connect to the revperse proxy.

Hope this will help someone 😛

Marcus Bière

Aperogeek c’est de l’actu Geek, mais aussi de l’actu Bières !

Du coup je vais vous présenter aujourd’hui une brasserie que j’aprécie particulièrement : Marcus Bière, la bière de la drôme !

Situé dans le petit village de Saou au coeur de la drôme, Marcus Bière est une brasserie artisanale qui fait de la super bonne bière !

Voici quelques petites photos histoire de vous donner envie d’y aller 🙂

Comme vous le voyez le coin est très agréable. En plus c’est juste a côté de chez beau papa, donc j’y vais régulièrement 😛

Meetup Grafana Lyon

J’ai créé le groupe Meetup Grafana Lyon, et je compte organiser un premier meetup Grafana prochainement !

Le rendez vous est fixé au mardi 12 Septembre dans un pub que je connais bien : L’antidote ^_^’


Au niveau des présentations, il y aura :

  • Automatisation de la gestion des datasources grafana avec un module ansible (moi)
  • Monitoring Docker avec Telegraf + InfluxDB + Grafana (moi)
  • monitorer un cluster Cassandra avec graphite_exporter / node_exporter, prometheus et grafana (Christophe Schmitz)

N’hésitez pas à vous inscrire, il reste de la place !


EDIT : le Meetup s’est bien passé. voici les liens vers les slides que j’ai présenté :

Kapacitor : Alerting for your timeseries

I already talk about monitoring docker with Telegraf, InfluxDB and Grafana. It’s nice, we have pretty dashboards, but it doesn’t do alerting ! Unless you sit in front of your screen all the day, you will not be warned when a container is crashing or when a friend connect on your Teamspeak channel !

Fortunaletly, in the TICK Stack of Influxdata, there is the « K » of Kapacitor.

Kapacitor is an Open source framework for processing, monitoring, and alerting on time series data.

To do that, Kapacitor use TickScripts, small scripts written in a custom DSL Langage, very simple to understand and deploy.

For exmple, if you want to send a warning level alert on your Slack Channel, when the CPU usage of one of your servers is greater than 70%, and a critical level alert when above 85% :

    |eval(lambda: 100.0 - "mean")
        .message('{{ .Level}}: {{ .Name }}/{{ index .Tags "host" }} has high cpu usage: {{ index .Fields "used" }}')
        .warn(lambda: "used" > 70.0)
        .crit(lambda: "used" > 85.0)

        // Slack

With this kind of DSL langage, we can create any rule we want. By requesting InfluxDB with InfluxQL queries then aggregating metrics by host or any tag, adding a filter based on any criteria (between 8am. and 7pm. from monday to friday for example), etc..

When the alert rule is ready, Kapacitor can use any alerting system, like :

  • sending an email,
  • post to slack or mattermost,
  • write in a log file,
  • send a pager duty message,
  • upscale or downscale a docker swarm/kubernetes stack
  • or simply execute a custom bash script.

Here is an example of alert generated by Kapacitor in Slack :

kapacitor alerting slack

And for thoses who doesn’t want to get their hands dirty, there is Chronograf (the « C » in TICK Stack).

chronograf dashboard

Chronograf is an open-source web application written in Go and React.js designed to visualize your monitoring data from influxDB.

We are far from a Grafana in term of features (and community), but it’s getting better every day. It allow you to explore your data very efficiently :

chronograf data explorer

My favorite feature is the web based interface to easily create alerting and automation rules for Kapacitor.chronograf kapacitor ruleOf course the web interface limits you in term of Kapacitor DSL langage (an expert mode is on the way), but you can easily, in 3 clics, create simple rules like a threshold, detect a delta during a time period or even send a alert when there is no data (deadman) !

So, theses tools are pretty youngs, but are very interestings : I’ll keep a watch on it !

Kapacitor : l’outil d’Alerting pour vos time series

J’ai déjà parlé du monitoring docker avec Telegraf, Influxdb et Grafana. C’est bien jolie, on a des beaux graphes, mais ça ne fait pas d’alerting. A moins de rester le nez devant les écrans toutes la journées, on ne sera pas prévenu en cas de crash d’un des conteneurs ou lorsqu’un pote se connecte au Teamspeak aperogeek !

Heureusement, dans la TICK Stack de influxdata, il y a le K de Kapacitor.

Kapacitor est un outil de stream processing, capable d’analyser au fil de l’eau les métriques qui arrivent dans influxDB.

Pour cela, Kapacitor utilise des TickScripts : des petits script dans un langage DSL propre a kapacitor, super simple à comprendre et mettre en place.

Par exemple pour envoyer un warning lorsque le cpu d’un des serveurs est utilisé a plus de 70%, et un gros warning à plus de 85% :

    |eval(lambda: 100.0 - "mean")
        .message('{{ .Level}}: {{ .Name }}/{{ index .Tags "host" }} has high cpu usage: {{ index .Fields "used" }}')
        .warn(lambda: "used" > 70.0)
        .crit(lambda: "used" > 85.0)

        // Slack

Avec ce type de langage, on est capable de créer n’importe quel type de règle, en interrogeant influxDB via des requêtes InfluxQL, de faire des agrégations par groupe de serveur, ou n’importe quel tag, en filtrant sur n’importe quel critère (du lundi au vendredi, entre 8h et 19h), etc..

Une fois la règle générée, Kapacitor est capable d’envoyer l’alerte n’importe où, comme :

  • envoyer une mail,
  • poster dans slack / mattermost,
  • écrire dans un fichier de log,
  • envoyer un message pager duty,
  • upscaler/downscaler une stack docker swarm/kubernetes
  • ou tout simplement exécuter un script maison.

Exemple d’alerting Slack :

kapacitor alerting slackEt pour ceux qui ne souhaitent pas mettre les mains dans le cambouis, il y a Chronograf (le « C » de TICK Stack).

chronograf dashboard

Chronograf et avant tout un outil de visualisation des métriques stockées dans influxDB. On est encore loin d’un Grafana en terme de fonctionnalités (et de communauté), mais ça avance petit à petit. Il permet d’explorer de de grapher rapidement une base influxdb :

chronograf data explorer

Enfin, le gros avantage est qu’il offre justement une interface graphique pour créer des règles Kapacitor !

chronograf kapacitor ruleAlors bien sur, on est limité dans le langage DSL, mais cela permet en 3 clics de créer des règles simples, comme un dépassement de seuil sur une période données, de détecter un écart significatif sur une plage de temps, ou encore d’alerter en cas d’absence de mesure !

Bref, ce sont des outils encore jeunes, mais très prometteurs, à surveiller !