[ansible-project] Need help on json query call in ansible

31 views
Skip to first unread message

Suresh Karpurapu

unread,
Apr 15, 2020, 9:52:04 AM4/15/20
to Ansible Project
Hi Ansible Guru's

I am using the CSV file as below for nfs volume migration as we have 1000 servers with 100 volumes. Hence, i would like to make to host inventory using add_host file instead using inventory file. However, the loop is failing when i use add_host module. Hence, i tried using json_query as suggested by vladimar, and it is working for debug output but same is not working for mount module as i would like to update /etc/fstab and mount the volumes using this playbook. would you suggest the logic?

Playbook and CSV details:
=========================

# cat mounts.csv
host,remote_path,mnt_path,python
host1,nfsflr01:/volahcstg_www_masup_stg_data_01,/myasup/stg/data,/usr/bin/python2.6
host2,nfsflr01:/volahcstg_www_masup_stg_data_01,/myasup/stg/data,/usr/bin/python2.6
# cat mounts.yml
---
- name: mount the nfsshare in client side
  hosts: localhost
  gather_facts: false
  become: yes
  tasks:
    - name: reading volume info from csv
      read_csv:
        path: "{{ playbook_dir }}/mounts.csv"
      register: sources
    - name: Grouping host and volume information
      add_host:
        name: "{{ item.0 }}"
        groups: nfsgroup
        var1: "{{ item.1|json_query('[].remote_path') }}"
        var2: "{{ item.1|json_query('[].mnt_path') }}"
        ansible_python_interpreter: "{{ item.1.0.python }}"
      loop: "{{ sources.list|groupby('host') }}"
- name: list the volumes
  hosts: nfsgroup
  become: yes
  gather_facts: false
  tasks:
    - name: debug output
      debug:
        msg:
          - "{{ inventory_hostname }}"
          - "{{ var1 }}"
          - "{{ var2 }}"
- name: mounting the volume in the fstab file
  hosts: nfsgroup
  gather_facts: false
  become: yes
  tasks:
    - name: mounting the volume in the fstab file
      mount:
        fstype: nfs
        opts: "rw,bg,hard,rsize=65536,wsize=65536,vers=3,actimeo=0,nointr,suid,timeo=600,tcp"
        dump: "0"
        passno: "0"
        src: "{{ var1 }}"
        path: "{{ var2 }}"
        state: mounted
      delegate_to: "{{ inventory_hostname }}"
...

================================================================================================================================================
Playbook Verbose Output:
================================================================================================================================================
            "host": "host1",
            "mnt_path": "/myasup/stg/data",
            "python": "/usr/bin/python2.6",
            "remote_path": "nfsflr01:/volahcstg_www_masup_stg_data_01"
        },
        {
            "host": "host2",
            "mnt_path": "/myasup/stg/data",
            "python": "/usr/bin/python2.6",
            "remote_path": "nfsflr01:/volahcstg_www_masup_stg_data_01"
        }
    ]
}

TASK [Grouping host and volume information] *****************************************************************************************************************************
task path: /suresh/suresh_playbooks/mounts.yml:11
creating host via 'add_host': hostname=host1
changed: [localhost] => (item=[u'host1', [{u'python': u'/usr/bin/python2.6', u'host': u'host1', u'mnt_path': u'/myasup/stg/data', u'remote_path': u'nfsflr01:/volahcstg_www_masup_stg_data_01'}]]) => {
    "add_host": {
        "groups": [
            "nfsgroup"
        ],
        "host_name": "host1",
        "host_vars": {
            "ansible_python_interpreter": "/usr/bin/python2.6",
            "var1": [
                "nfsflr01:/volahcstg_www_masup_stg_data_01"
            ],
            "var2": [
                "/myasup/stg/data"
            ]
        }
    },
    "ansible_loop_var": "item",
    "changed": true,
    "item": [
        "host1",
        [
            {
                "host": "host1",
                "mnt_path": "/myasup/stg/data",
                "python": "/usr/bin/python2.6",
                "remote_path": "nfsflr01:/volahcstg_www_masup_stg_data_01"
            }
        ]
    ]
}
creating host via 'add_host': hostname=host2
changed: [localhost] => (item=[u'host2', [{u'python': u'/usr/bin/python2.6', u'host': u'host2', u'mnt_path': u'/myasup/stg/data', u'remote_path': u'nfsflr01:/volahcstg_www_masup_stg_data_01'}]]) => {
    "add_host": {
        "groups": [
            "nfsgroup"
        ],
        "host_name": "host2",
        "host_vars": {
            "ansible_python_interpreter": "/usr/bin/python2.6",
            "var1": [
                "nfsflr01:/volahcstg_www_masup_stg_data_01"
            ],
            "var2": [
                "/myasup/stg/data"
            ]
        }
    },
    "ansible_loop_var": "item",
    "changed": true,
    "item": [
        "host2",
        [
            {
                "host": "host2",
                "mnt_path": "/myasup/stg/data",
                "python": "/usr/bin/python2.6",
                "remote_path": "nfsflr01:/volahcstg_www_masup_stg_data_01"
            }
        ]
    ]
}
META: ran handlers
META: ran handlers

PLAY [list the volumes] *************************************************************************************************************************************************
META: ran handlers

TASK [debug output] *****************************************************************************************************************************************************
task path: /suresh/suresh_playbooks/mounts.yml:24
ok: [host2] => {
    "msg": [
        "host2",
        [
            "nfsflr01:/volahcstg_www_masup_stg_data_01"
        ],
        [
            "/myasup/stg/data"
        ]
    ]
}
ok: [host1] => {
    "msg": [
        "host1",
        [
            "nfsflr01:/volahcstg_www_masup_stg_data_01"
        ],
        [
            "/myasup/stg/data"
        ]
    ]
}
META: ran handlers
META: ran handlers

PLAY [mounting the volume in the fstab file] ****************************************************************************************************************************
META: ran handlers

TASK [mounting the volume in the fstab file] *******

<host2> (0, '', '')
fatal: [host2]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "backup": false,
            "boot": true,
            "dump": "0",
            "fstab": null,
            "fstype": "nfs",
            "opts": "rw,bg,hard,rsize=65536,wsize=65536,vers=3,actimeo=0,nointr,suid,timeo=600,tcp",
            "passno": "0",
            "path": "['/myasup/stg/data']",
            "src": "['nfsflr01:/volahcstg_www_masup_stg_data_01']",
            "state": "mounted"
        }
    },
    "msg": "Error mounting ['/myasup/stg/data']: mount: can't get address for ['nfsflr01\n"

fatal: [host1]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "backup": false,
            "boot": true,
            "dump": "0",
            "fstab": null,
            "fstype": "nfs",
            "opts": "rw,bg,hard,rsize=65536,wsize=65536,vers=3,actimeo=0,nointr,suid,timeo=600,tcp",
            "passno": "0",
            "path": "['/myasup/stg/data']",
            "src": "['nfsflr01:/volahcstg_www_masup_stg_data_01']",
            "state": "mounted"
        }
    },
    "msg": "Error mounting ['/myasup/stg/data']: mount: can't get address for ['nfsflr01\n"
}

PLAY RECAP **************************************************************************************************************************************************************
localhost        : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
host1            : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
host2            : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0





Result on target servers fstab file
===================================

# grep 'nfsflr01:/volahcstg_www_masup_stg_data_01' /etc/fstab
nfsflr01:/volahcstg_www_masup_stg_data_01   /myasup/stg/data  nfs vers=3,rw,nosuid,bg,hard,rsize=32768,wsize=32768,nointr,tcp,timeo=600  0 0
['nfsflr01:/volahcstg_www_masup_stg_data_01',\040'nasdurahc01spd02:/volahcstg_www_masup_stg_app_01'] ['/myasup/stg/data',\040'/myasup/stg/app'] nfs rw,bg,hard,rsize=65536,wsize=65536,vers=3,actimeo=0,nointr,suid,timeo=600,tcp 0 0

Stefan Hornburg (Racke)

unread,
Apr 15, 2020, 2:42:21 PM4/15/20
to ansible...@googlegroups.com
On 4/15/20 3:52 PM, Suresh Karpurapu wrote:
> Hi Ansible Guru's
>
> I am using the CSV file as below for nfs volume migration as we have 1000 servers with 100 volumes. Hence, i would like
> to make to host inventory using add_host file instead using inventory file. However, the loop is failing when i use
> add_host module. Hence, i tried using json_query as suggested by vladimar, and it is working for debug output but same
> is not working for mount module as i would like to update /etc/fstab and mount the volumes using this playbook. would
> you suggest the logic?
>

var1 and var2 are lists, and not strings which are expected by the mount module.

Try var1[0] and var2[0] instead.

Regards
Racke
> --
> 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 <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/28178b9a-5e29-4fff-ada4-6f368fe33079%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/28178b9a-5e29-4fff-ada4-6f368fe33079%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Suresh Karpurapu

unread,
Apr 16, 2020, 6:40:35 AM4/16/20
to ansible...@googlegroups.com
Thanks Racke, it's working for if host has single volume but same is failing if the host has multiple volumes. Basically, loop is getting failed. Please suggest if there any alternative ways

# cat mounts.csv
host,remote_path,mnt_path,python
host1,nfsflr01:/volahcstg_www_masup_stg_data_01,/myasup/stg/data,/usr/bin/python2.6
host1,nfsflr02:/volahcstg_www_masup_stg_app_01,/myasup/stg/app,/usr/bin/python2.6
host2,nfsflr01:/volahcstg_www_masup_stg_data_01,/myasup/stg/data,/usr/bin/python2.6
host2,nfsflr02:/volahcstg_www_masup_stg_app_01,/myasup/stg/app,/usr/bin/python2.6


Result:


TASK [Grouping host and volume information] *****************************************************************************************************************************
task path: /suresh/suresh_playbooks/mounts.yml:12

creating host via 'add_host': hostname=host1
changed: [localhost] => (item=[u'host1', [{u'python': u'/usr/bin/python2.6', u'host': u'host1', u'mnt_path': u'/myasup/stg/data', u'remote_path': u'nfsflr01:/volahcstg_www_masup_stg_data_01'}, {u'python': u'/usr/bin/python2.6', u'host': u'host1', u'mnt_path': u'/myasup/stg/app', u'remote_path': u'nfsflr02:/volahcstg_www_masup_stg_app_01'}]]) => {

    "add_host": {
        "groups": [
            "nfsgroup"
        ],
        "host_name": "host1",
        "host_vars": {
            "ansible_python_interpreter": "/usr/bin/python2.6",
            "var1": [
                "nfsflr01:/volahcstg_www_masup_stg_data_01",
                "nfsflr02:/volahcstg_www_masup_stg_app_01"
            ],
            "var2": [
                "/myasup/stg/data",
                "/myasup/stg/app"
            ]

        }
    },
    "ansible_loop_var": "item",
    "changed": true,
    "item": [
        "host1",
        [
            {
                "host": "host1",
                "mnt_path": "/myasup/stg/data",
                "python": "/usr/bin/python2.6",
                "remote_path": "nfsflr01:/volahcstg_www_masup_stg_data_01"
            },
            {
                "host": "host1",
                "mnt_path": "/myasup/stg/app",
                "python": "/usr/bin/python2.6",
                "remote_path": "nfsflr02:/volahcstg_www_masup_stg_app_01"
            }
        ]
    ]

}
creating host via 'add_host': hostname=host2
changed: [localhost] => (item=[u'host2', [{u'python': u'/usr/bin/python2.6', u'host': u'host2', u'mnt_path': u'/myasup/stg/data', u'remote_path': u'nfsflr01:/volahcstg_www_masup_stg_data_01'}, {u'python': u'/usr/bin/python2.6', u'host': u'host2', u'mnt_path': u'/myasup/stg/app', u'remote_path': u'nfsflr02:/volahcstg_www_masup_stg_app_01'}]]) => {

    "add_host": {
        "groups": [
            "nfsgroup"
        ],
        "host_name": "host2",
        "host_vars": {
            "ansible_python_interpreter": "/usr/bin/python2.6",
            "var1": [
                "nfsflr01:/volahcstg_www_masup_stg_data_01",
                "nfsflr02:/volahcstg_www_masup_stg_app_01"
            ],
            "var2": [
                "/myasup/stg/data",
                "/myasup/stg/app"
            ]

        }
    },
    "ansible_loop_var": "item",
    "changed": true,
    "item": [
        "host2",
        [
            {
                "host": "host2",
                "mnt_path": "/myasup/stg/data",
                "python": "/usr/bin/python2.6",
                "remote_path": "nfsflr01:/volahcstg_www_masup_stg_data_01"
            },
            {
                "host": "host2",
                "mnt_path": "/myasup/stg/app",
                "python": "/usr/bin/python2.6",
                "remote_path": "nfsflr02:/volahcstg_www_masup_stg_app_01"
            }
        ]
    ]

}
META: ran handlers
META: ran handlers


TASK [debug output] *****************************************************************************************************************************************************
task path: /suresh/suresh_playbooks/mounts.yml:25

ok: [host1] => {
    "msg": [
        "host1",
        [
            "nfsflr01:/volahcstg_www_masup_stg_data_01",
            "nfsflr02:/volahcstg_www_masup_stg_app_01"
        ],
        [
            "/myasup/stg/data",
            "/myasup/stg/app"
        ]
    ]

}
ok: [host2] => {
    "msg": [
        "host2",
        [
            "nfsflr01:/volahcstg_www_masup_stg_data_01",
            "nfsflr02:/volahcstg_www_masup_stg_app_01"
        ],
        [
            "/myasup/stg/data",
            "/myasup/stg/app"
        ]
    ]
}

TASK [mounting the volume in the fstab file] ****************************************************************************************************************************
changed: [host1 -> host1] => {
    "changed": true,
    "dump": "0",
    "fstab": "/etc/fstab",
    "fstype": "nfs",

    "invocation": {
        "module_args": {
            "backup": false,
            "boot": true,
            "dump": "0",
            "fstab": null,
            "fstype": "nfs",
            "opts": "vers=3,rw,nosuid,bg,hard,rsize=32768,wsize=32768,nointr,tcp,timeo=600",

            "passno": "0",
            "path": "/myasup/stg/data",
            "src": "nfsflr01:/volahcstg_www_masup_stg_data_01",
            "state": "mounted"
        }
    },
    "name": "/myasup/stg/data",
    "opts": "vers=3,rw,nosuid,bg,hard,rsize=32768,wsize=32768,nointr,tcp,timeo=600",
    "passno": "0",
    "src": "nfsflr01:/volahcstg_www_masup_stg_data_01"
}
<host2> (0, '', '')
changed: [host2 -> host2] => {
    "changed": true,
    "dump": "0",
    "fstab": "/etc/fstab",
    "fstype": "nfs",

    "invocation": {
        "module_args": {
            "backup": false,
            "boot": true,
            "dump": "0",
            "fstab": null,
            "fstype": "nfs",
            "opts": "vers=3,rw,nosuid,bg,hard,rsize=32768,wsize=32768,nointr,tcp,timeo=600",

            "passno": "0",
            "path": "/myasup/stg/data",
            "src": "nfsflr01:/volahcstg_www_masup_stg_data_01",
            "state": "mounted"
        }
    },
    "name": "/myasup/stg/data",
    "opts": "vers=3,rw,nosuid,bg,hard,rsize=32768,wsize=32768,nointr,tcp,timeo=600",
    "passno": "0",
    "src": "nfsflr01:/volahcstg_www_masup_stg_data_01"

}
META: ran handlers
META: ran handlers

PLAY RECAP **************************************************************************************************************************************************************
localhost        : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
host1            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
host2            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

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/98dc5533-a1e9-20c2-88c5-4d3f17b0ed55%40linuxia.de.

Suresh Karpurapu

unread,
Apr 17, 2020, 4:07:50 AM4/17/20
to ansible...@googlegroups.com
Can anyone help me on this request or please suggest if there any alternative solution for my requirement?

Regards,
Suresh
Reply all
Reply to author
Forward
0 new messages