unable to extract version out of registered output

101 views
Skip to first unread message

Sameer Modak

unread,
May 23, 2023, 8:41:59 AM5/23/23
to Ansible Project
Hello Team,

Code

name: debug grepout

    debug:

#      msg: "this is the version {{ vername.files.path }}"

      var: files_found_path

    tags: vern

Output:

files_found_path": [

        "/opt/confluent-7.2.5/kafka/share/java/kafka/kafka_2.13-7.2.5-ccs.jar"

    ]


 Above code is working but after that it failing with below code, i need 7.2.5 before -css in above output but failing to get that tried files_found_path[0].split(‘-‘) as well

 - name: get version

    set_fact:

      versionn: "{{ files_found_path.split(‘-‘)[0] }}"

TASK [get version] ***************************************************************************************************************************************************************************

fatal: [host1: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'split'. 'list object' has no attribute 'split'\n\nThe error appears to be in '/Users/sameer_modak/ansibledemo/testing.yml': line 54, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: get version\n    ^ here\n"}

fatal: [host2]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'split'. 'list object' has no attribute 'split'\n\nThe error appears to be in '/Users/sameer_modak/ansibledemo/testing.yml': line 54, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: get version\n    ^ here\n"}

fatal: [host3]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'split'. 'list object' has no a



Thanks in advance.

Abhijeet Kasurde

unread,
May 23, 2023, 9:57:19 AM5/23/23
to ansible...@googlegroups.com
files_found_path is an array/list. So you need files_found_path[0].split

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9e09186d-7715-49d7-ad37-b8f41feebac7n%40googlegroups.com.


--
Thanks,
Abhijeet Kasurde

Sameer Modak

unread,
May 23, 2023, 2:06:00 PM5/23/23
to Ansible Project
i did try that as well but no luck as i have already mentioned in my previous conversation.

below is the complete code, output i want is 7.2.5 from jar line. thats it. But i m missing something may taking long route when simple solution is there

  - name: take command grep output

    shell: 

      cmd: "ps -ef | grep -i kafka | grep -i server.prop | awk -F'-cp' '{print $2}' | awk -F':' '{print $1}'"

    register: grepout

    tags: verchk1

  


  - name: setfact and pring 

    set_fact:

      pathd: "{{ (grepout.stdout.split('\n')[0] | replace('bin/../', '') | replace('*','') | replace(' /','/')) }}" 

#       pathd: "{% if  'kafka' in (grepout.stdout.split('\n')[0] | replace('bin/../', '') | replace('*','') | replace(' /','/')) %}'hi'{%else}'bye'{% endif %}}" 

#      - pathd1: "{% if 'kafka' in pathd %}pathd{% else %}''{% endif %}"

  

  - name: setfact for 2nd var

    set_fact:

      pathd1: "{% if 'kafka' in pathd %}{{pathd}}{% else %}''{% endif %}" 


  - name: pathd ls

    find:

      paths: "{{pathd1}}"

      patterns: "kafka_*ccs.jar"

#      use_regex: true

    register: vername


  - name : get the complete path

    set_fact:

#      files_found_path: "{{ vername.files.path[1] }}"

      files_found_path: "{{ vername.files | map(attribute='path') | list }}"


  - name: debug grepout

    debug:

#      msg: "this is the version {{ vername.files.path }}"

      var: files_found_path

    tags: vern


#  - name: get version

#    set_fact:

#      versionn: "{{ files_found_path.split('-')[1] }}"


#  - name: debug grepout

#    debug:

#      msg: "this is the version {{ vername.files.path }}"

#      var: versionn

#    tags: vern



Abhijeet Kasurde

unread,
May 23, 2023, 3:03:53 PM5/23/23
to ansible...@googlegroups.com
Assuming you are getting 'files_found_path' as

TASK [debug] ****************************************************************************
ok: [localhost] => {
    "msg": [
        "/opt/confluent-7.2.5/kafka/share/java/kafka/kafka_2.13-7.2.5-ccs.jar"
    ]
}

I would write my playbook as -

    - debug:
        msg: "{{ files_found_path[0] | regex_replace(kafka_ver_regex, '\\2') }}"
      vars:
        kafka_ver_regex: (.*kafka_\d\.\d\d-)(\d\.\d\.\d)(-.*\.jar)

That would give me -

TASK [debug] ****************************************************************************
ok: [localhost] => {
    "msg": "7.2.5"
}



--
Thanks,
Abhijeet Kasurde

Sameer Modak

unread,
May 23, 2023, 3:31:58 PM5/23/23
to Ansible Project
Thanks (faar aabhari) Abhijeet for your reply. but also i have to store that in variable so i ll have to use set_fact ryt or is there any simpler way in one line because if you see my code i had to use so many set_fact to 1st get one value then again store in another value. 


One more point i have to highlight is that when i get output of  my shell command jar path is not always the 1st item in list it sometimes 2nd so this is bit annoying .

for example,

 - name: take command grep output

    shell:

      cmd: "ps -ef | grep -i kafka | grep -i server.prop | awk -F'-cp' '{print $2}' | awk -F':' '{print $1}'"

    register: grepout

    tags: verchk1 


here output is not always constant

sometimes

"grepout.stdout": " /usr/bin/../share/java/kafka/*\n' '{print $2}' | awk -F'"

and sometimes 

"grepout.stdout": '{print $2}' | awk -F'" \n' " /usr/bin/../share/java/kafka/*"

    

This creates problems 


Abhijeet Kasurde

unread,
May 24, 2023, 12:54:05 AM5/24/23
to Ansible Project
No problem,

You can use set_fact instead of debug task.

When you get a list of elements from the shell command, you can use `loop` or `with_items` to iterate over them and set the fact.
Reply all
Reply to author
Forward
0 new messages