How to filter ipaddress and hostname and store it to different variable

34 views
Skip to first unread message

Pandu jh

unread,
Jun 21, 2019, 10:54:23 AM6/21/19
to Ansible Project
I have a variable that contains short hostname, FQDN and IP addresses.
I need to filter sort hostname/FQDNs and it should store to a variable and also IP addresses should store to an another variable(this am able to do using ipaddr)
Please help me to filter hostnames and store to a variable.

system:  >
  1.1.1.1
   myhost

Dick Visser

unread,
Jun 21, 2019, 7:03:09 PM6/21/19
to ansible...@googlegroups.com
You should be able to use a regular expression to filter hostnames

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/5c7f4d91-3427-44fb-a63c-ed42681b8403%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Pandu jh

unread,
Jun 22, 2019, 1:18:59 AM6/22/19
to Ansible Project
Ok Thanks.

Pandu jh

unread,
Jun 22, 2019, 5:43:47 AM6/22/19
to Ansible Project
I tried below tasks but it's giving error on regex task.
Can you help me to get the pattern to filter only the item starts with the alphabet.

      - set_fact:
          ip_list:  "{{ ip_addr.split(\n) | ipaddr }}"
      - debug:  var=ip_list
      - set_fact:
          host_list:  "{{ ip_addr.split(\n) |  regex_search('^[a-z].*')  }}"
      - debug:  var=host_list




On Saturday, 22 June 2019 04:33:09 UTC+5:30, Dick Visser wrote:
You should be able to use a regular expression to filter hostnames
On Fri, 21 Jun 2019 at 16:54, Pandu jh <jhp...@gmail.com> wrote:
I have a variable that contains short hostname, FQDN and IP addresses.
I need to filter sort hostname/FQDNs and it should store to a variable and also IP addresses should store to an another variable(this am able to do using ipaddr)
Please help me to filter hostnames and store to a variable.

system:  >
  1.1.1.1
   myhost

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Dick Visser

unread,
Jun 22, 2019, 6:45:45 AM6/22/19
to ansible...@googlegroups.com
What is the error, and what does the 'ip_addr' variable look like?
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e228de95-7f97-40a8-8b96-7a50eef53855%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Pandu jh

unread,
Jun 22, 2019, 4:05:52 PM6/22/19
to Ansible Project
My variable looks below.

ip_addr: |
1.12.6.9
myhost
Myhost.example.com

Dick Visser

unread,
Jun 22, 2019, 11:41:50 PM6/22/19
to ansible...@googlegroups.com
And the error is...?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Pandu jh

unread,
Jun 23, 2019, 2:08:23 PM6/23/19
to Ansible Project
The error is 

TASK [set_fact] *****************************************************************************************************
fatal: [lab1]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{ ip_addr.split(\n) |  regex_search('^[a-z].*')  }}): expected string or buffer"}
        to retry, use: --limit @/playbooks/var_check.retry

On Sunday, 23 June 2019 09:11:50 UTC+5:30, Dick Visser wrote:
And the error is...?

On Sat, 22 Jun 2019 at 22:06, Pandu jh <jhp...@gmail.com> wrote:
My variable looks below.

ip_addr: |
  1.12.6.9
  myhost
  Myhost.example.com

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Dick Visser

unread,
Jun 24, 2019, 2:44:02 PM6/24/19
to ansible...@googlegroups.com
On Sun, 23 Jun 2019 at 20:08, Pandu jh <jhp...@gmail.com> wrote:
>
> The error is
>
> TASK [set_fact] *****************************************************************************************************
> fatal: [lab1]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{ ip_addr.split(\n) | regex_search('^[a-z].*') }}): expected string or buffer"}

You didn't quote the newline.
But, you don't really need that as the default is to split by white space.

For the literal IPv4 address it's indeed easy, as you can just use the
ipaddr filter.
For the hostname you can use the select() test, in combination with
some regular expression that needs to match.
Depending on how formal/exact you want to define "a hostname", the
regex can simple or really complex.
A simple example:

tasks:
- set_fact:
ip_list: "{{ ip_addr.split() | ipaddr }}"
- debug: var=ip_list
- set_fact:
host_list: "{{ ip_addr.split() | select('match',
'^([a-zA-Z]+(-[a-zA-Z]+)*\\.)+[a-zA-Z]{2,}$') | list }}"
- debug: var=host_list



This should give you some results.


Dick

Pandu jh

unread,
Jun 25, 2019, 7:16:07 AM6/25/19
to Ansible Project

Below regex is working perfectly for my requirement. Thanks a lot, Dick for your help.

      - set_fact:
          ip_list:  "{{  ip_addr.split() | ipaddr }}"
      - debug:  var=ip_list

      - set_fact:
          host_list:  "{{ ip_addr.split() | select('match','^([a-zA-Z].*)') | list }}"
      - debug:  var=host_list
Reply all
Reply to author
Forward
0 new messages