Search code examples
ansibleansible-ad-hoc

Error Recognition in Ansible Ad-hoc Commands


Is there a way to capture the stdout and stderr separately in bash, maybe as a tuple, for ansible ad-hoc commands? Something like: stdout, stderr= ansible -i hosts -m shell -a "command"


Solution

  • You could use option -t to log output in JSON format. For example if I execute

    ansible -m shell -a "echo test" -t tmp localhost
    

    then in the file ./tmp/localhost I'll get this output:

    {
      "changed": true,
      "cmd": "echo test",
      "delta": "0:00:00.006099",
      "end": "2020-04-14 11:43:01.878959",
      "rc": 0,
      "start": "2020-04-14 11:43:01.872860",
      "stderr": "",
      "stderr_lines": [],
      "stdout": "test",
      "stdout_lines": [
        "test"
      ]
    }
    

    You can then parse stdout and stderr.