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.
On Tuesday, May 23, 2023 at 12:31:58 PM UTC-7 Sameer Modak wrote:
> 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
>
>
> On Wednesday, May 24, 2023 at 12:33:53 AM UTC+5:30 Abhijeet Kasurde wrote:
>
>> 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"
>> }
>>
>> On Tue, May 23, 2023 at 11:06 AM Sameer Modak <[email protected]>
>> wrote:
>>
>>> 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
>>>
>>>
>>>
>>> On Tuesday, May 23, 2023 at 7:27:19 PM UTC+5:30 Abhijeet Kasurde wrote:
>>>
>>>> files_found_path is an array/list. So you need files_found_path[0].split
>>>>
>>>> On Tue, May 23, 2023 at 5:42 AM Sameer Modak <[email protected]>
>>>> wrote:
>>>>
>>>>> 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.
>>>>>
>>>>> --
>>>>> 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 [email protected].
>>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/ansible-project/9e09186d-7715-49d7-ad37-b8f41feebac7n%40googlegroups.com
>>>>>
>>>>> <https://groups.google.com/d/msgid/ansible-project/9e09186d-7715-49d7-ad37-b8f41feebac7n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>>
>>>>
>>>>
>>>> --
>>>> Thanks,
>>>> Abhijeet Kasurde
>>>>
>>> --
>>> 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 [email protected].
>>>
>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/ansible-project/2b726fc8-85da-4b86-b851-55b4ebec0f40n%40googlegroups.com
>>>
>>> <https://groups.google.com/d/msgid/ansible-project/2b726fc8-85da-4b86-b851-55b4ebec0f40n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>
>>
>> --
>> Thanks,
>> Abhijeet Kasurde
>>
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/9367b4f9-2b6d-4fc5-b0df-bd5e6052b545n%40googlegroups.com.