Score:0

Using ansible loop until with stdout_lines

de flag

I am trying to use ansible loop until the condition is met. I can use until if the output is only single line, however if the output is multiple lines, I will need to use stdout_lines but fail to do so.

If output is single line:

- name: check on sync status
  shell: some command
  register: sync_status
  until: sync_status.stdout == 'SSUS'

If output is multiple lines, then I try to use stdout_lines

- name: check on sync status
  shell: some command 
  register: sync_status
  until: item.stdout_lines == 'SSUS'
  with_items: "{{ sync_status }}"

but I got variable undefined:

fatal: [xxxxxxx]: FAILED! => {
    "msg": "'sync_status' is undefined"
}

I don't want to do it on seperate task because then the sync_status is registered on previous task , and I will be comparing the old status instead of the current status.

Kindly assist.

br flag
You can't both *loop* and *register* the same variable.
br flag
The use case is not clear. What do you want to do when the condition is not met?
sloweriang avatar
de flag
basically this task is just to check the status, once the condition is met I will just proceed to next task.
Score:0
br flag

For example, given the file and the playbook

shell> cat test.txt 
XX

shell> cat playbook.yml
- hosts: localhost
  tasks:
    - ansible.builtin.command:
        cmd: cat test.txt
      register: sync_status
      until: sync_status.stdout == 'SSUS'

the task will fail

TASK [ansible.builtin.command] ********************************************
FAILED - RETRYING: ansible.builtin.command (3 retries left).
FAILED - RETRYING: ansible.builtin.command (2 retries left).
FAILED - RETRYING: ansible.builtin.command (1 retries left).
fatal: [localhost]: FAILED! => changed=true 
  attempts: 3
  cmd:
  - cat
  - test.txt
  delta: '0:00:00.003479'
  end: '2021-06-16 07:27:19.927499'
  rc: 0
  start: '2021-06-16 07:27:19.924020'
  stderr: ''
  stderr_lines: <omitted>
  stdout: XX
  stdout_lines: <omitted>

But, if you insert the string "SSUS" into the file before or during the testing the task will succeed, e.g.

TASK [ansible.builtin.command] ********************************************
FAILED - RETRYING: ansible.builtin.command (3 retries left).
FAILED - RETRYING: ansible.builtin.command (2 retries left).
changed: [localhost]

Fit the parameters to your needs.


Q: "If the output is multiple lines, I will need to use stdout_lines."

A: Test the presence of the string in the list, e.g.

    - ansible.builtin.command:
        cmd: cat test.txt
      register: sync_status
      until: "'SSUS' in sync_status.stdout_lines"
sloweriang avatar
de flag
Hi, for example, there are multiple lines, some with SSUS, some with PAIR. My conditions is to make sure all lines are met , not just some of the lines consist the keyword
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.