# Ansible search string in stdout\_lines and user later as variable

**URL:** <https://forum.ansible.com/t/ansible-search-string-in-stdout-lines-and-user-later-as-variable/28046>\
**Category:** Ansible Project\
**Created:** [July 13, 2018, 6:17pm UTC](https://forum.ansible.com/t/ansible-search-string-in-stdout-lines-and-user-later-as-variable/28046 "2018-07-13T18:17:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ankur\_Gupta](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@Ankur\_Gupta](https://forum.ansible.com/u/Ankur_Gupta)\
**Post date:** [July 13, 2018, 6:17pm UTC](https://forum.ansible.com/t/ansible-search-string-in-stdout-lines-and-user-later-as-variable/28046/1 "2018-07-13T18:17:56Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Daria\_Serkova](https://avatars.discourse-cdn.com/v4/letter/d/a587f6/32.png) [@Daria\_Serkova](https://forum.ansible.com/u/Daria_Serkova)\
**Post date:** [July 15, 2018, 11:52am UTC](https://forum.ansible.com/t/ansible-search-string-in-stdout-lines-and-user-later-as-variable/28046/2 "2018-07-15T11:52:59Z")

</div>

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](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”…

---

<div class="post-metadata">

**Author:** ![elavarde](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@elavarde](https://forum.ansible.com/u/elavarde)\
**Post date:** [July 16, 2018, 8:10am UTC](https://forum.ansible.com/t/ansible-search-string-in-stdout-lines-and-user-later-as-variable/28046/3 "2018-07-16T08:10:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ankur\_Gupta](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@Ankur\_Gupta](https://forum.ansible.com/u/Ankur_Gupta)\
**Post date:** [July 17, 2018, 12:36pm UTC](https://forum.ansible.com/t/ansible-search-string-in-stdout-lines-and-user-later-as-variable/28046/4 "2018-07-17T12:36:18Z")

</div>

Thank you Eric. it works for me.
