Ansible search string in stdout_lines and user later as variable

I have the following output of a command output

`

“failed”: false,
“rc”: 0,
“start”: “2018-07-13 18:05:48.354496”,
“stderr”: “”,
“stderr_lines”: ,
“stdout”: “"[2018-07-13 13:25:01]: Updating version file",\n"[2018-07-13 13:25:01]: Backing up host ssh keys",\n"[2018-07-13 13:25:01]: Temporarily moving certain configuration files",\n"[2018-07-13 13:25:02]: Creating temporary AMI",\n"[2018-07-13 13:25:02]: Waiting for AMI to become available (ami-1cc8fb63)",\n"[2018-07-13 13:27:48]: Launching temporary instance with temporary AMI to cleanup configuration files",\n"[2018-07-13 13:28:05]: Temporary instance (i-01a8cf89ab30b8b81) launched, waiting for cleanup to complete",\n"[2018-07-13 13:35:07]: Creating new post-swupgrade ami from temporary instance",\n"[2018-07-13 13:35:08]: Waiting for new AMI (ami-0acdfe75) to be available",\n"[2018-07-13 13:36:54]: Sending updated AMI (ami-0acdfe75) to all nodes",\n"[2018-07-13 13:36:54]: Updated HSNs",\n"[2018-07-13 13:36:54]: Successfully created backup AMI!",\n"[2018-07-13 13:36:54]: Terminating temporary instance i-01a8cf89ab30b8b81",\n"[2018-07-13 13:36:55]: Deleting temporary ami ami-1cc8fb63",\n"[2018-07-13 13:36:55]: Waiting for instance (i-087eb340fe3214dc9) to come back online",\n"[2018-07-13 13:36:55]: Restoring temporarily moved configuration files"”,
“stdout_lines”: [
“"[2018-07-13 13:25:01]: Updating version file",”,
“"[2018-07-13 13:25:01]: Backing up host ssh keys",”,
“"[2018-07-13 13:25:01]: Temporarily moving certain configuration files",”,
“"[2018-07-13 13:25:02]: Creating temporary AMI",”,
“"[2018-07-13 13:25:02]: Waiting for AMI to become available (ami-1ca8fd33)",”,
“"[2018-07-13 13:27:48]: Launching temporary instance with temporary AMI to cleanup configuration files",”,
“"[2018-07-13 13:28:05]: Temporary instance (i-01a8cf899r30b8b81) launched, waiting for cleanup to complete",”,
“"[2018-07-13 13:35:07]: Creating new post-swupgrade ami from temporary instance",”,
“"[2018-07-13 13:35:08]: Waiting for new AMI (ami-0acdfe75) to be available",”,
“"[2018-07-13 13:36:54]: Sending updated AMI (ami-0acdfe75) to all nodes",”,
“"[2018-07-13 13:36:54]: Updated HSNs",”,
“"[2018-07-13 13:36:54]: Successfully created backup AMI!",”,
“"[2018-07-13 13:36:54]: Terminating temporary instance i-01a8cf899r30b8b81",”,
“"[2018-07-13 13:36:55]: Deleting temporary ami ami-1ac4fb6c",”,
“"[2018-07-13 13:36:55]: Waiting for instance (i-017eb340de3210qz9) to come back online",”,
“"[2018-07-13 13:36:55]: Restoring temporarily moved configuration files"”
]
}
}

`

I want to search the ami-1ca8fd33 in stdout_lines and save as a variable.

Is any way to achive this ?

Hi Ankur Gupta
Theoretically you could register this output to variable and use variable.stdout_lines (see https://github.com/ansible/ansible-examples/blob/master/language_features/register_logic.yml)
but even in this case you’ll need to wrap the line to some “template searcher”…

Hi,

I did put the same content as coming from your command in the file match_strings.txt and wrote a playbook match_strings.yml:

`

  • hosts: localhost
    gather_facts: false
    tasks:
  • command: cat match_strings.txt
    register: match_var
  • debug: var=match_var
  • set_fact:
    ami_id: “{{ match_var.stdout | regex_search(‘(?!Waiting for AMI to become available \()ami-[^)]*’) }}”
  • debug: var=ami_id

`

the result then looks like:

`
$ ansible-playbook match_strings.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match ‘all’

PLAY [localhost] ****************************************************************************************************************************************************************************************************

TASK [command] ******************************************************************************************************************************************************************************************************
changed: [localhost]

TASK [debug] ********************************************************************************************************************************************************************************************************
ok: [localhost] => {
“match_var”: {
“changed”: true,
“cmd”: [
“cat”,
“match_strings.txt”
],
“delta”: “0:00:00.002018”,
“end”: “2018-07-16 10:08:03.920649”,
“failed”: false,
“rc”: 0,
“start”: “2018-07-16 10:08:03.918631”,
“stderr”: “”,
“stderr_lines”: ,
“stdout”: “"[2018-07-13 13:25:01]: Updating version file",\n"[2018-07-13 13:25:01]: Backing up host ssh keys",\n"[2018-07-13 13:25:01]: Temporarily moving certain configuration files",\n"[2018-07-13 13:25:02]: Creating temporary AMI",\n"[2018-07-13 13:25:02]: Waiting for AMI to become available (ami-1cc8fb63)",\n"[2018-07-13 13:27:48]: Launching temporary instance with temporary AMI to cleanup configuration files",\n"[2018-07-13 13:28:05]: Temporary instance (i-01a8cf89ab30b8b81) launched, waiting for cleanup to complete",\n"[2018-07-13 13:35:07]: Creating new post-swupgrade ami from temporary instance",\n"[2018-07-13 13:35:08]: Waiting for new AMI (ami-0acdfe75) to be available",\n"[2018-07-13 13:36:54]: Sending updated AMI (ami-0acdfe75) to all nodes",\n"[2018-07-13 13:36:54]: Updated HSNs",\n"[2018-07-13 13:36:54]: Successfully created backup AMI!",\n"[2018-07-13 13:36:54]: Terminating temporary instance i-01a8cf89ab30b8b81",\n"[2018-07-13 13:36:55]: Deleting temporary ami ami-1cc8fb63",\n"[2018-07-13 13:36:55]: Waiting for instance (i-087eb340fe3214dc9) to come back online",\n"[2018-07-13 13:36:55]: Restoring temporarily moved configuration files"”,
“stdout_lines”: [
“"[2018-07-13 13:25:01]: Updating version file",”,
“"[2018-07-13 13:25:01]: Backing up host ssh keys",”,
“"[2018-07-13 13:25:01]: Temporarily moving certain configuration files",”,
“"[2018-07-13 13:25:02]: Creating temporary AMI",”,
“"[2018-07-13 13:25:02]: Waiting for AMI to become available (ami-1cc8fb63)",”,
“"[2018-07-13 13:27:48]: Launching temporary instance with temporary AMI to cleanup configuration files",”,
“"[2018-07-13 13:28:05]: Temporary instance (i-01a8cf89ab30b8b81) launched, waiting for cleanup to complete",”,
“"[2018-07-13 13:35:07]: Creating new post-swupgrade ami from temporary instance",”,
“"[2018-07-13 13:35:08]: Waiting for new AMI (ami-0acdfe75) to be available",”,
“"[2018-07-13 13:36:54]: Sending updated AMI (ami-0acdfe75) to all nodes",”,
“"[2018-07-13 13:36:54]: Updated HSNs",”,
“"[2018-07-13 13:36:54]: Successfully created backup AMI!",”,
“"[2018-07-13 13:36:54]: Terminating temporary instance i-01a8cf89ab30b8b81",”,
“"[2018-07-13 13:36:55]: Deleting temporary ami ami-1cc8fb63",”,
“"[2018-07-13 13:36:55]: Waiting for instance (i-087eb340fe3214dc9) to come back online",”,
“"[2018-07-13 13:36:55]: Restoring temporarily moved configuration files"”
]
}
}

TASK [set_fact] *****************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] ********************************************************************************************************************************************************************************************************
ok: [localhost] => {
“ami_id”: “ami-1cc8fb63”
}

PLAY RECAP **********************************************************************************************************************************************************************************************************
localhost : ok=4 changed=1 unreachable=0 failed=0

`

Hope this helps,
Eric

Thank you Eric. it works for me.