Ansible premiers pas

Ansible look env config order
  1. ANSIBLE_CONFIG (environment variable if set)
  2. ansible.cfg (in the current directory)
  3. ~/.ansible.cfg (in the home directory)
  4. /etc/ansible/ansible.cfg




site.yml is the default name for a playbook

'main.yml' : This is the default filename for a file containing Ansible Tasks, or Handlers

A role should encapsulate all the things that have to happen to make a thing work

Roles can have dependencies, which will require that another role be applied first


In general, a role consists of the following subdirectories, "files", "handlers", "meta", "tasks" and "templates".

files/ contains files that will be copied to the target with the copy: module.

handlers/ contains YAML files which contain 'handlers' little bits of config that can be triggered with the notify: action inside a task. Usually just handlers/main.yml - See http://docs.ansible.com/playbooks_intro.html#handlers-running-operations-on-change for more information on what handlers are for.

meta/ contains YAML files containing role dependencies.  Usually just meta/main.yml

tasks/ contains YAML files containing a list of named steps which Ansible will execute in order on a target.  Usually tasks/main.yml

templates/ contains Jinja2 template files, which can be used in a task with the template: module to interpolate variables in the template, then copy the template to a location on the target.  Files in this directory often end .j2 by convention.



All available modules :

http://docs.ansible.com/list_of_all_modules.html


the "with_items:" action that some modules support

- name: install default packages
    apt: pkg={{ item }} state=installed
    with_items:
      - aptitude
      - vim
      - supervisor
      - python-dev
      - htop
      - screen

 

Groups

When running ansible on hosts you have a few options to select groups:

  1. AND: group1:&group2 Execute on hosts that only belong to both groups
  2. OR: group1:group1 Execute on all hosts in group1 and group2
  3. NOT: !group1 Execute on all groups except for group1
  4. Wild Card
  5. Regular Expression

Difference between command & shell module?

With the shell module you can leverage the shell environment e.g: environment variables, etc.

Gather_facts c’est la possibilité d’utiliser des variables d’environnement ansible

Find latest file in directory

- name: Get files in a folder
  find:
    paths: "/var/www/html/wwwroot/somefolder/"
  register: found_files

- name: Get latest file
  set_fact:
    latest_file: "{{ found_files.files | sort(attribute='mtime',reverse=true) | first }}"

Check and diff

  • --check will tell you which changes would be made, without actually making them. (Not all modules support this)
  • --diff shows you the differences between the old and new files

It is common to use both flags. Try changing the text in front.html, and then running this command:

ansible-playbook web.yml --check --diff

4.5 Handlers

Sometimes when you make a configuration change it's necessary to restart the service. Ansible supports this though "handlers".

...

- name: install index page
      copy: src=front.html dest=/var/www/html/index.html backup=yes
      notify: restart apache2
  handlers:
    - name: restart apache2
      service: name=apache2 state=restarted

4.6 Tags

  
..
- name: install index page
      copy: src=front.html dest=/var/www/html/index.html backup=yes
      tags: configure
      notify: restart apache2
  handlers:
    - name: restart apache2
      service: name=apache2 state=restarted
ansible-playbook web.yml -t configure

Commentaires