Difference between revisions of "Register Output with Ansible"

From Knowledge Center
Jump to: navigation, search
 
Line 73: Line 73:
 
       msg: "Account balance low: {{ result.vultr_account_info.balance|int }}"
 
       msg: "Account balance low: {{ result.vultr_account_info.balance|int }}"
 
     when: (result.vultr_account_info.balance|int > vultr_common.min_balance)
 
     when: (result.vultr_account_info.balance|int > vultr_common.min_balance)
 +
</pre>
 +
 +
Register output from a API response:
 +
 +
<pre>
 +
  - name: echo test
 +
    shell: "curl -s https://reqres.in/api/users/2"
 +
    register: echo_json
 +
 +
  - name: print test
 +
    debug:
 +
      var: echo_json.stdout
 +
</pre>
 +
 +
Results in:
 +
 +
<pre>
 +
ok: [localhost] => {
 +
    "echo_json.stdout": {
 +
        "ad": {
 +
            "company": "StatusCode Weekly",
 +
            "text": "A weekly newsletter focusing on software development, infrastructure, the server, performance, and the stack end of things.",
 +
            "url": "http://statuscode.org/"
 +
        },
 +
        "data": {
 +
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg",
 +
            "email": "janet.weaver@reqres.in",
 +
            "first_name": "Janet",
 +
            "id": 2,
 +
            "last_name": "Weaver"
 +
        }
 +
    }
 +
}
 +
 
</pre>
 
</pre>
  

Latest revision as of 11:50, 1 May 2020

Register the output of a command with Ansible, and write it to a debug stdout

The Playbook:

---
- hosts: rpi-04
  user: pi
  tasks:
    - name: echo test
      command: "echo ruan"
      register: echo_output

    - debug: msg="{{echo_output.stdout}}"

Test Run:

 $ ansible-playbook -i hosts ap_echo.yml

PLAY [rpi-04] *****************************************************************

GATHERING FACTS ***************************************************************
ok: [192.168.1.118]

TASK: [echo test] *************************************************************
changed: [192.168.1.118]

TASK: [debug msg="{{echo_output.stdout}}"] ************************************
ok: [192.168.1.118] => {
    "msg": "ruan"
}

PLAY RECAP ********************************************************************
192.168.1.118              : ok=3    changed=1    unreachable=0    failed=0


Also see [1] for example with dicts:

- name: Get Vultr account infos
  vultr_account_info:
  register: result

- name: Print the infos
  debug:
    var: result.vultr_account_info

Running the example:

TASK [Print the infos] **************************************************************************************************************************************************************
ok: [localhost] => {
    "result.vultr_account_info": {
        "balance": -11.24,
        "last_payment_amount": -10.0,
        "last_payment_date": "2020-03-12 14:31:59",
        "pending_charges": 0.07
    }
}

With the above, conditionals can be used:

  - name: "Account balance requirement check"
    fail:
      msg: "Account balance low: {{ result.vultr_account_info.balance|int }}"
    when: (result.vultr_account_info.balance|int > vultr_common.min_balance)

Register output from a API response:

  - name: echo test
    shell: "curl -s https://reqres.in/api/users/2"
    register: echo_json

  - name: print test
    debug:
      var: echo_json.stdout

Results in:

ok: [localhost] => {
    "echo_json.stdout": {
        "ad": {
            "company": "StatusCode Weekly",
            "text": "A weekly newsletter focusing on software development, infrastructure, the server, performance, and the stack end of things.",
            "url": "http://statuscode.org/"
        },
        "data": {
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg",
            "email": "janet.weaver@reqres.in",
            "first_name": "Janet",
            "id": 2,
            "last_name": "Weaver"
        }
    }
}