Q:

How can i get the output of ansible ad-hoc command in json, csv or other format

What is the way to get the output of Ansible ad-hoc command in json, csv or other format

A:

In ansible.cfg add:
[defaults]
stdout_callback = json

See documentation
Instead of this:
ok: [localhost] => {
    "msg": "test"
}

You will have:
{
    "plays": [
        {
            "play": {
                "id": "720000f8-9450-586c-9a68-000000000005", 
                "name": "Json Test"
            }, 
            "tasks": [
                {
                    "hosts": {
                        "localhost": {
                            "_ansible_no_log": false, 
                            "_ansible_verbose_always": true, 
                            "changed": false, 
                            "msg": "test"
                        }
                    }, 
                    "task": {
                        "id": "720000f8-9450-586c-9a68-000000000007", 
                        "name": "Debug"
                    }
                }
            ]
        }
    ], 
    "stats": {
        "localhost": {
            "changed": 0, 
            "failures": 0, 
            "ok": 1, 
            "skipped": 0, 
            "unreachable": 0
        }
    }
}

For the following playbook:
---
- name: Json Test
  hosts: localhost
  gather_facts: False

  vars: 
    test: test

  tasks:
    - name: Debug
      debug:
        msg: "{{ test  }}"

