uncowandy
uncowandy

Reputation: 105

Save Ansible Output to a Variable

For the following Ansible Console Output:

TASK [Run Ping Command] *********************************
Changed: [HostName]

TASK [Add Ping Command Results to Log] *********************************
ok: [HostName]

How would I save the output of Changed: [HostName] and ok: [HostName] to a variable?

I tried using register: but it just saves the output of the command instead of what is printed on the Ansible Console (Changed, ok, skipping..)

Upvotes: 0

Views: 674

Answers (1)

Carlos Monroy Nieblas
Carlos Monroy Nieblas

Reputation: 2283

The register would do the trick, those text messages are displayed based on the values received from the result of the task

Assuming that you have the task:

- name: Run Ping Command
  ansible.builtin.ping:
  register: ping_result

the variable ping_result will have the result of the task

- name: Task changed
  ansible.builtin.debug:
    msg: "task changed"
  when:
    - not ping_result.skipped | default(false)
    - ping_result.changed | default(false)

- name: Task failed
  ansible.builtin.debug:
    msg: "task failed"
  when:
    - not ping_result.skipped | default(false)
    - ping_result.failed | default(false)

- name: Task succeeded
  ansible.builtin.debug:
    msg: "task succeeded"
  when:
    - not ping_result.skipped | default(false)
    - (not ping_result.failed) | default(false)

- name: Task skipped
  ansible.builtin.debug:
    msg: "task skipped"
  when:
    - ping_result.skipped | default(false)

Upvotes: 1

Related Questions