I'm using ansbile_runner Python module as bellow:
import ansible_runner
r = ansible_runner.run(private_data_dir='/tmp/demo', playbook='test.yml')
When I execute the above code, it will show the output without printing in Python. What I want is to save the stdout content into a Python variable for further text processing.
Did you read the manual under https://ansible-runner.readthedocs.io/en/stable/python_interface/ ? There is an example where you add another parameter, which is called
output_fdand that could be a file handler instead ofsys.stdout.Sadly, this is a parameter of the
run_commandfunction and the documentation is not very good. A look into the source code at https://github.com/ansible/ansible-runner/blob/devel/ansible_runner/interface.py could help you.According to the implementation details in https://github.com/ansible/ansible-runner/blob/devel/ansible_runner/runner.py it looks like, the
run()function always prints to stdout.According to the interface, there is a boolean flag in
run(json_mode=TRUE)that stores the response in JSON (I expect inrinstead of stdout) and there is another boolean flagquiet.I played around a little bit. The relevant option to avoid output to stdout is
quiet=Trueas run() attribute.Ansible_Runner catches the output and writes it to a file in the artifacts directory. Every run() command produces that directory as described in https://ansible-runner.readthedocs.io/en/stable/intro/#runner-artifacts-directory-hierarchy. So there is a file called stdout in the artifact directory. It contains the details. You can read it as JSON.
But also the returned object contains already some relevant data. Here is my example
So this outputs all processed hosts and informs about failures per host. Concrete more output can be found in the stdout file in artifact directory.