Need help on formating stdout_lines output

496 views
Skip to first unread message

Suresh Karpurapu

unread,
Apr 29, 2020, 6:44:14 PM4/29/20
to Ansible Project
Hi All,

Would anyone please help me formatting mountchk.result. I am able to get the required output with item.stdout_lines[0],item.stdout_lines[1],item.stdout_lines[2] but host3 has 6 entries. So, please help me in filtering 


mountchk.results:
=================
host1:
     "stdout": "nfsflr01:/app01\n/app\n/etc/fstab",
     "stdout_lines": [
                "nfsflr01:/app01",
                "/app",
                "/etc/fstab"

host2: 
    "stdout": "nfsflr02:/app02\n/app02\n/etc/fstab",
    "stdout_lines": [
        "nfsflr02:/app02",
        "/app02",
        "/etc/fstab"
host3:
     "stdout": "nfsflr03:/app03\n/app03\n/etc/fstab\nnfsflr03:/app03\n/app03\n/etc/rc.willows",
     "stdout_lines": [
         "nfsflr03:/app03",
         "/app03",
         "/etc/fstab",
         "nfsflr03:/app03",
         "/app03",
         "/etc/rc.willows"

Required filtering:
==================
host1,nfsflr01:/app01,/app,/etc/fstab
host2,nfsflr02:/app02,/app02,/etc/fstab
host3,nfsflr03:/app03,/app03,/etc/fstab
host3,nfsflr03:/app03,/app03,/etc/rc.willows

Suresh Karpurapu

unread,
Apr 30, 2020, 1:23:56 PM4/30/20
to ansible...@googlegroups.com
Can anyone help me on this request?

Regards,
Suresh

--
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/e931b86e-2c7d-4293-a280-6690818bc94e%40googlegroups.com.

Kai Stian Olstad

unread,
May 2, 2020, 5:02:46 AM5/2/20
to ansible...@googlegroups.com
On Wed, Apr 29, 2020 at 03:44:14PM -0700, Suresh Karpurapu wrote:
> Would anyone please help me formatting mountchk.result. I am able to get
> the required output with
> item.stdout_lines[0],item.stdout_lines[1],item.stdout_lines[2] but host3
> has 6 entries. So, please help me in filtering

You can use with_sequence or range in a for loop.

vars:
myvar:
- one
- two
- three
- four
- five
- six
tasks:
- debug: msg="{{ myvar[item | int] }},{{ myvar[item | int + 1] }},{{ myvar[item | int + 2] }}"
with_sequence: start=0 end={{ (myvar | length) - 1 }} stride=3

- debug:
msg: |
{% for i in range(0, myvar | length, 3) %}
{{ myvar[i] }},{{ myvar[i + 1] }},{{ myvar[i + 2] }}
{% endfor %}

--
Kai Stian Olstad

Suresh Karpurapu

unread,
May 4, 2020, 5:28:16 AM5/4/20
to ansible...@googlegroups.com
Hi Sai Stian,

This syntax would be helpful but i may be using in wrong way as i am getting below errors. Please help on fixing the issue.

  - name: debug with with sequence
    debug:
      msg:"{{ volchk[item.stdout | int] }},{{ volchk[item.stdout | int + 1] }},{{ volchk[item.stdout | int + 2] }}"
    with_sequence: start=0 end={{ (volchk | length)-1 }} stride=3
error:
FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeBytes object' has no attribute 'stdout'\n\nThe error appears to be in '/suresh/suresh_playbooks/nfsvolmigration/roles/mountcheck/tasks/volcheck.yml': line 44, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n    ignore_errors: true\n  - name: debug with with sequence\n    ^ here\n"}

Debug "volchk.result" output:
===============
host1:
     "stdout": "nfsflr01:/app01\n/app\n/etc/fstab",
     "stdout_lines": [
                "nfsflr01:/app01",
                "/app",
                "/etc/fstab"

host2: 
    "stdout": "nfsflr02:/app02\n/app02\n/etc/fstab",
    "stdout_lines": [
        "nfsflr02:/app02",
        "/app02",
        "/etc/fstab"
host3:
     "stdout": "nfsflr03:/app03\n/app03\n/etc/fstab\nnfsflr03:/app03\n/app03\n/etc/rc.willows",
     "stdout_lines": [
         "nfsflr03:/app03",
         "/app03",
         "/etc/fstab",
         "nfsflr03:/app03",
         "/app03",
         "/etc/rc.willows"

Regards,
Suresh



FAILED! => {"msg": "'dict object' has no attribute 'stdout'"}

--
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.

Suresh Karpurapu

unread,
May 4, 2020, 5:16:18 PM5/4/20
to ansible...@googlegroups.com
Here is my playbook, i would like to display all lines in stdout_lines in below format without miss. I am able to display only first 3 lines but host03 has more than 3 lines even it may be more lines for other hosts. Would anyone please help me?

Required output fle:
==================
host1,nfsflr01:/app01,/app,na,/etc/fstab
host2,nfsflr02:/app02,/app02,na,/etc/fstab
host3,nfsflr03:/app03,/app03,na,/etc/fstab
host3,nfsflr03:/app03,/app03,na,/etc/rc.willows
host4,nfsflr04:/app04,na,na,na


---
  - name: nullify the ansible log file
    local_action: copy content="" dest="{{ playbook_dir }}/logs/volmigration.log"
    ignore_errors: true
    run_once: True
  - name: nullify the file content
    local_action: copy content="" dest="{{ playbook_dir }}/logs/volcheck.txt"
    ignore_errors: true
    run_once: True
  - name: Making the txt header information to volcheck.txt file
    local_action:
      module: lineinfile
      path: "{{ playbook_dir }}/logs/volcheck.txt"
      line: "host,python,old_path,mnt_path,new_path,map_path,csv_path"
      state: present
      create: yes
    run_once: True
  - name: Fetch the volume mounted status
    shell: |-
      {% if ansible_os_family == "Solaris" %}
      grep -ws "{{ item.0 }}" /etc/vfstab|awk '{print $1}'
      grep -ws "{{ item.0 }}" /etc/vfstab|awk '{print $3}'
      grep -wls "{{ item.0 }}" /etc/vfstab
      grep -wls "{{ item.0 }}" /etc/auto*|head -1|xargs grep -w "{{ item.0 }}"|awk '{print $2}'
      grep -wls "{{ item.0 }}" /etc/auto*|head -1|xargs grep -w "{{ item.0 }}"|awk '{print $1}'
      grep -wls "{{ item.0 }}" /etc/auto*|head -1
      {%- else %}
      grep -rws "{{ item.0 }}" /etc/fstab|awk '{print $1}'
      grep -rws "{{ item.0 }}" /etc/fstab|awk '{print $2}'
      grep -wls "{{ item.0 }}" /etc/fstab
      grep -rls "{{ item.0 }}" /etc/auto*|head -1|xargs grep -w "{{ item.0 }}"|awk '{print $3}'
      grep -rls "{{ item.0 }}" /etc/auto*|head -1|xargs grep -w "{{ item.0 }}"|awk '{print $1}'
      grep -rwls "{{ item.0 }}" /etc/auto*|head -1
      grep -rwls "{{ item.0 }}" /etc/rc*|head -1|xargs grep -w "{{ item.0 }}"|awk '{print $6}'
      grep -rwls "{{ item.0 }}" /etc/rc*|head -1|xargs grep -w "{{ item.0 }}"|awk '{print $7}'
      grep -rwls "{{ item.0 }}" /etc/rc*|head -1
      {%- endif %}
    with_together:
      - "{{ var1 }}"
    register: volchk
    args:
      warn: false
    ignore_errors: true
  - name: Making the final volcheck.txt file
    local_action:
      module: lineinfile
      path: "{{ playbook_dir }}/logs/volcheck.txt"
      line: |-
        {% if item.stdout|length>0 %}
        {{ inventory_hostname }},{{ ansible_python_interpreter }},{{ item.stdout_lines[0] }},{{ item.stdout_lines[1] }},NA,{{ item.stdout_lines[2] }},{{ item.item.0 }},
        {%- else %}
        {{ inventory_hostname }},{{ ansible_python_interpreter }},NA,NA,NA,NA,{{ item.item.0 }},
        {%- endif %}
      insertafter: EOF
      state: present
      create: yes
    with_items: "{{ volchk.results }}"
  - name: mark the entry of /etc/fstab and /etc/vfstab as fstab
    replace:
      path: "{{ playbook_dir }}/logs/volcheck.txt"
      regexp: "{{ item.stdout_lines[2] }}"
      replace: "fstab"
    when: item.stdout|length>0 and (item.stdout_lines[2] | regex_search("/etc/fstab") or item.stdout_lines[2] | regex_search("/etc/vfstab"))
    with_items: "{{ volchk.results }}"
    delegate_to: localhost
  - debug:
      msg: "Final volume check file is located at {{ playbook_dir }}/logs/volcheck.txt"
    delegate_to: localhost
    run_once: true
  - name: Send the final report email
    local_action:
      module: mail
      host: host10.google.com
      port: 25
      to: sur...@gmail.com
      subject: Ansible volume pre-check report
      attach: "{{ playbook_dir }}/logs/volcheck.txt"
    run_once: true
...

Debug:
Debug "volchk.result" output:
===============
host1:
     "stdout": "nfsflr01:/app01\n/app\n/etc/fstab",
     "stdout_lines": [
                "nfsflr01:/app01",
                "/app",
                "/etc/fstab"

host2: 
    "stdout": "nfsflr02:/app02\n/app02\n/etc/fstab",
    "stdout_lines": [
        "nfsflr02:/app02",
        "/app02",
        "/etc/fstab"
host3:
     "stdout": "nfsflr03:/app03\n/app03\n/etc/fstab\nnfsflr03:/app03\n/app03\n/etc/rc.willows",
     "stdout_lines": [
         "nfsflr03:/app03",
         "/app03",
         "/etc/fstab",
         "nfsflr03:/app03",
         "/app03",
         "/etc/rc.willows"

Suresh Karpurapu

unread,
May 5, 2020, 2:56:11 PM5/5/20
to ansible...@googlegroups.com
would anyone please help me on this request?

Regards,
Suresh

Neha Pithadiya

unread,
May 6, 2020, 8:00:10 AM5/6/20
to Ansible Project
Can you paste the content of "volchk" list .. Also try using item.stdout_lines instead of item.stdout in your debug task.

 
On Wednesday, May 6, 2020 at 12:26:11 AM UTC+5:30, Suresh Karpurapu wrote:
would anyone please help me on this request?

Regards,
Suresh

To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Suresh Karpurapu

unread,
May 6, 2020, 9:07:48 AM5/6/20
to ansible...@googlegroups.com
thanks Neha, Kai

I could fix the issue by using range with stdout.split(). Even, it is working with stdout_lines as well

Once again thanks to all for helping me on this request.

Regards,
Suresh

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/6311bf4f-8be9-41c7-b7b0-41137190b2f2%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages