Mike I
Mike I

Reputation: 5

Extract IP addresses from a text file with other values

Let's say you have a variable file on your localhost. We can call it values.txt, and its contents are:

isjwidywiudywdiuwqoq10.110.195.108xxsxww/ed/swqqwfdfwef8.8.8.8

You want to extract only the IP address values from all of this junk. I have made multiple attempts at this using the file lookup and the ipaddr filter. What I'm doing looks like this:

- name: extract IPs only
  debug: 
    msg: "{{ query('file', 'values.txt') | ipaddr }}"

However, this does not work! How can I go about doing this to where I get only the IP's?

Upvotes: 0

Views: 1234

Answers (2)

codeaprendiz
codeaprendiz

Reputation: 3205

Here's one example

# // Ansible playbook to  Extract IP addresses from a text file

- hosts: localhost
  gather_facts: false
  tasks:
  - name: Extract IP addresses from a text file
    set_fact:
      ips: "{{ lookup('file', '/tmp/tmpdir/test.txt') | regex_findall('(?<!\\d)(?:\\d{1,3}\\.){3}\\d{1,3}(?!\\d)')   }}" 
    register: result

  - debug:
      msg: "{{ result }}"
  • Sample run
╰─ cat test.txt           
hello
12.14.34.45asdfadsf234234
asdfasldfkj23.34.45.23alsdjfwerk
fafn aslkdfj vahsdifadk 23.43.34.232 asldkvvnasdfads


╰─ ansible-playbook test.yaml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

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

TASK [Extract IP addresses from a text file] ****************************************************************************************************************************************************
ok: [localhost]

TASK [debug] ************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "ansible_facts": {
            "ips": [
                "12.14.34.45",
                "23.34.45.23",
                "23.43.34.232"
            ]
        },
        "changed": false,
        "failed": false
    }
}

PLAY RECAP **************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


Upvotes: 0

larsks
larsks

Reputation: 312680

You can do this with a regular expression filter. To extract all the ip addresses from a string, it sounds like you want to find everything that matches [0-9]+\.[0-9]+\.[0-9]+\.[0-9]). To extract all matches of an expression from a string, Ansible provides us with the regex_findall filter.

For example:

- hosts: localhost
  gather_facts: false
  tasks:
    - debug:
        msg: >-
          {{ "isjwidywiudywdiuwqoq10.110.195.108xxsxww/ed/swqqwfdfwef8.8.8.8" | regex_findall("[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+") }}

The above playbook will output:

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "10.110.195.108",
        "8.8.8.8"
    ]
}

Upvotes: 2

Related Questions