0

Following is the output from an ansible task

    "msg": [
        "Executing script ment.sh with following arguments",
        "Arguments passed : userName=root hostName=007 passWord=123 payload=/tmp/007.json.19.18 adminpass=abc",
        "spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@007",
        "Warning: Permanently added '007' (ECDSA) to the list of known hosts.",
        "",
        "",
        "root@007's password: ",
        "Last login: Thu Feb  9 17:52:48 2023 from 1.1.1.1",
        "",
        "\u001b]0;root@oak:~\u0007\u001b[?1034h[root@oak ~]# ",
        "Second try on password",
        "createdocker -j -r /tmp/json.19.18",
        "Enter an initial password for Web Console account (admin):",
        "Retype the password for Web Console account (admin):",
        "User 'admin' created successfully...",
        "{",
        "  \"jobId\" : \"dd370bce-03d7-4436-93b4-9a9ed7658394\",",
        "  \"status\" : \"Created\",",
        "  \"message\" : null,",
        "  \"reports\" : [ ],",
        "  \"createTimestamp\" : \"February 09, 2023 17:53:29 PM UTC\",",
        "  \"resourceList\" : [ ],",
        "  \"description\" : \"Provisioning service creation\",",
        "  \"updatedTime\" : \"February 09, 2023 17:53:29 PM UTC\",",
        "  \"jobType\" : null",
        "}",
        "",
        "Successfully created provisioning job",
        "exit",
        "\u001b]0;root@oak:~\u0007[root@oak ~]# exit",
        "logout",
        "Connection to closed."
    ]

Can someone please help me get the jobId value i.e. "dd370bce-03d7-4436-93b4-9a9ed7658394" into a variable

Akash-BLR
  • 63
  • 4
  • 1
    Since the output (... from whatever that is or however that was generated) contains a single UUID, one approach could be to just catch the UUID from text output. In example [Searching for UUIDs in text with regex](https://stackoverflow.com/a/75261545/6771046). This apporach could then be used for a simple Custom Filter Plugin `plugins/filter/extract_uuid.py` which could be called via `"{{ output.msg | extract_uuid }}"`. – U880D Feb 09 '23 at 19:05

1 Answers1

0

I got it:

- name: Store jobId in a variable
  debug:
     msg: "{{ the_output | select('match','^.*\"jobId\"\\s*:\\s*\"(.*)\".*$') | regex_replace('jobId\" :', '') | regex_replace('[^a-zA-Z0-9-]', '') }}"

Ansible returns: dd370bce-03d7-4436-93b4-9a9ed7658394

Kevin C
  • 4,851
  • 8
  • 30
  • 64