2

I've been trying to parse email addresses from the below json data:

"msg": [
    {
        "fqdn": "www.westero.local",
        "function": "",
        "maintenance_notes": ""
    },        
{
        "fqdn": "mail.fem.local",
        "function": "database",
        "maintenance_notes": "notify [email protected] and [email protected]"
    },
    {
        "fqdn": "net.runner.local",
        "function": "web",
        "maintenance_notes": "wait for [email protected] or [email protected]"
    }
]

With the below Ansible task, I am only able to parse the first email address:

  # extract emails from maintenance_notes
- name: Parse email addresses
  set_fact:
    email_addys: "{{ json_data | json_query('[*].maintenance_notes') | map('regex_search', '([a-zA-Z0-9._-]+@[a-zA-Z0-9_-]+.[a-zA-Z0-9_-]+)') | unique | select | list }}"

Is there a better way to extract the addresses so I would end up with the following data?

[email protected]
[email protected]
[email protected]
[email protected]
6
  • um, er, WHY are you parsing for an email address when the JSON has fqdn right there for you to grab the value for each such property? Commented Oct 1 at 6:20
  • @RLDailey what do you mean and how would one do what you're saying? Commented Oct 1 at 6:36
  • 2
    @RLDailey, it is because there are two different point of views and logic. The maintenance notes are for whom is to notify, users of the systems, in case of a maintenance window. That's a kind business logic with a human point of view. It is probably different from the FQDN, administrator and operators of the system, which have a other point of view and knowledge of the infrastructure. Therefore the disadvantageous JSON structure and content. The questioner has most probably no control over that and can't change it. Commented Oct 1 at 6:43
  • The sample data I needed to parse is part of a largerr dataset and I just needed to extract the email address information from "maintenance_notes". Thanks for responding @U880D and I apologize and thank you for fixing the formatting. I don't post here often but I'll be more vigilant with formatting my posts next time. Commented Oct 1 at 7:00
  • @U880D -- thank you for clarifying that. i missed the difference. [blush] Commented Oct 1 at 7:58

1 Answer 1

1

Using regex_findall and flattening the result seems to work:

# extract emails from maintenance_notes
- name: Parse email addresses
  set_fact:
    email_addys: "{{ json_data | json_query('[*].maintenance_notes') | map('regex_findall', '([a-zA-Z0-9._-]+@[a-zA-Z0-9_-]+.[a-zA-Z0-9_-]+)')| select | flatten | unique | list }}"

It returns the following:

ok: [localhost] => {
    "msg": [
        "[email protected]",
        "[email protected]",
        "[email protected]",
        "[email protected]"
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.