# Extracting an IP address from a shell command's return using a regex

**URL:** <https://forum.ansible.com/t/extracting-an-ip-address-from-a-shell-commands-return-using-a-regex/26780>\
**Category:** Ansible Project\
**Created:** [February 19, 2018, 9:48am UTC](https://forum.ansible.com/t/extracting-an-ip-address-from-a-shell-commands-return-using-a-regex/26780 "2018-02-19T09:48:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![J-P\_Methot](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@J-P\_Methot](https://forum.ansible.com/u/J-P_Methot)\
**Post date:** [February 19, 2018, 9:48am UTC](https://forum.ansible.com/t/extracting-an-ip-address-from-a-shell-commands-return-using-a-regex/26780/1 "2018-02-19T09:48:13Z")

</div>

Hi,

I’ve been stuck on this issue for a few days and I’m still unable to make it work, even though it should be doable. Essentially, I’m doing a shell command which gives me the following return (please don’t suggest to use an ansible module for this, the ansible module and shade don’t support choosing the subnet when associating a floatingip)

1. “stdout\_lines”: [

2. “Created a new floatingip:”,

3. “±--------------------±-------------------------------------+”,

4. “| Field | Value |”,

5. “±--------------------±-------------------------------------+”,

6. “| created\_at | 2018-02-19T09:32:44Z |”,

7. “| description | |”,

8. “| fixed\_ip\_address | 10.80.16.60 |”,

9. “| floating\_ip\_address | 192.168.0.1 |”,

10. “| floating\_network\_id | uuid |”,

11. “| id | uuid |”,

12. “| port\_id | uuid |”,

13. “| project\_id | uuid |”,

14. “| revision\_number | 1 |”,

15. “| router\_id | uuid |”,

16. “| status | DOWN |”,

17. “| tenant\_id | uuid |”,

18. “| updated\_at | 2018-02-19T09:32:44Z |”,

19. "±--------------------±-------------------------------------+

Essentially, I want to extract the floating\_ip\_address line. So, I’ve been using a commonly recognized regex for IP address and, right now, I’ve reached the point where I have the following line:

1. 
  - name: extract the ip line

2. set\_fact:

3. regexcomp: “{{ rawfloating.stdout\_lines | regex\_search(‘floating\_ip\_address \| \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b’) }}”

Now ansible tells me that it’s expecting a string or buffer. I’m not sure what to do anymore. Anyone has any ideas?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 19, 2018, 10:00am UTC](https://forum.ansible.com/t/extracting-an-ip-address-from-a-shell-commands-return-using-a-regex/26780/2 "2018-02-19T10:00:35Z")

</div>

rawfloating.stdout\_lines is a list not a sting, you need to use rawfloating.stdout

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 19, 2018, 5:46pm UTC](https://forum.ansible.com/t/extracting-an-ip-address-from-a-shell-commands-return-using-a-regex/26780/3 "2018-02-19T17:46:27Z")

</div>

You need to use select to go over the list and get you the 'correct  
line' as it is a 'list of lines', then you can extract the info from  
that line.

Or just use stdout instead of stdout\_lines.
