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

20 views
Skip to first unread message

J-P Methot

unread,
Feb 19, 2018, 4:48:13 AM2/19/18
to Ansible Project
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?

Kai Stian Olstad

unread,
Feb 19, 2018, 5:01:02 AM2/19/18
to ansible...@googlegroups.com
On 19.02.2018 10:48, J-P Methot wrote:
> 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?

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

--
Kai Stian Olstad

Brian Coca

unread,
Feb 19, 2018, 12:46:41 PM2/19/18
to Ansible Project
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.





--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages