19 mai 2016

[ANSIBLE] interagir avec des webservices

Utilisation du module uri pour interagir avec des webservices HTTP ou HTTPS

Voici plusieurs exemples de l'utilisation du module uri:
    # Check that you can connect (GET) to a page and it returns a status 200
    - uri: url=http://www.example.com

    # Check that a page returns a status 200 and fail if the word AWESOME is not
    # in the page contents.
    - action: uri url=http://www.example.com return_content=yes
      register: webpage

    - action: fail
      when: "'AWESOME' not in webpage.content"


    # Create a JIRA issue
    - uri:
        url: https://your.jira.example.com/rest/api/2/issue/
        method: POST
        user: your_username
        password: your_pass
        body: "{{ lookup('file','issue.json') }}"
        force_basic_auth: yes
        status_code: 201
        body_format: json

    # Login to a form based webpage, then use the returned cookie to
    # access the app in later tasks

    - uri:
        url: https://your.form.based.auth.example.com/index.php
        method: POST
        body: "name=your_username&password=your_password&enter=Sign%20in"
        status_code: 302
        HEADER_Content-Type: "application/x-www-form-urlencoded"
      register: login

    - uri:
        url: https://your.form.based.auth.example.com/dashboard.php
        method: GET
        return_content: yes
        HEADER_Cookie: "{{login.set_cookie}}"

    # Queue build of a project in Jenkins:
    - uri:
        url: "http://{{ jenkins.host }}/job/{{ jenkins.job }}/build?token={{ jenkins.token }}"
        method: GET
        user: "{{ jenkins.user }}"
        password: "{{ jenkins.password }}"
        force_basic_auth: yes
        status_code: 201

Mon besoin initial:

pouvoir reproduire cette commande curl:
curl -XPUT http://localhost:6059/books/test.json -H "Accept: application/json" -H "Content-Type: text/plain" -d '
user=astunix
home=/home/astunix'
en format Ansible:
- name: Create catalog on ElasticSearch
  uri:
    url: "http://localhost:6059/catalogs/{{ book_name }}.json"
    method: PUT
    HEADER_Content-Type: "application/json"
    HEADER_Content-Type: "text/plain"
    body: "user=astunix\n
home=/home/astunix"
  sudo: true
Bien entendu, method peut comporter d'autres options que PUT:
  • GET
  • POST
  • PUT
  • HEAD
  • DELETE
  • OPTIONS
  • PATCH
  • TRACE
  • CONNECT
  • REFRESH

Plus d'information sur le site officiel de Ansible : http://docs.ansible.com/ansible/uri_module.html

Aucun commentaire:

Enregistrer un commentaire