Difference between revisions of "Register Output with Ansible"

From Knowledge Center
Jump to: navigation, search
(Created page with " Register the output of a command with Ansible, and write it to a debug stdout '''The Playbook:''' <pre> --- - hosts: rpi-04 user: pi tasks: - name: echo test...")
 
Line 39: Line 39:
 
</pre>
 
</pre>
  
 +
 +
Also see [https://docs.ansible.com/ansible/latest/modules/vultr_account_info_module.html#examples] for example with dicts:
 +
 +
<pre>
 +
- name: Get Vultr account infos
 +
  vultr_account_info:
 +
  register: result
 +
 +
- name: Print the infos
 +
  debug:
 +
    var: result.vultr_account_info
 +
</pre>
 +
 +
Running the example:
 +
 +
<pre>
 +
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
 +
    }
 +
}
 +
</pre>
 +
 +
With the above, conditionals can be used:
 +
 +
<pre>
 +
  - 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)
 +
</pre>
  
 
[[Category:Ansible]]
 
[[Category:Ansible]]

Revision as of 11:17, 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)