use ansible-runner as Python module Interface to Ansible error: RunnerConfig' object has no attribute 'command'

Question:

I use ansible 2.9.13 and ansible-runner 1.4.6

Below is my Python code:

from ansible_runner import Runner, RunnerConfig

f = open('/home/george/dev/beeops/ansible_private/keyfile', 'r')
key = f.read()
rc = RunnerConfig(private_data_dir='/home/george/dev/beeops/ansible_private',
                  playbook='test.yml',
                  inventory='127.0.0.1',
                  ssh_key=key,)
r = Runner(config=rc)
r.run()

An error occurred while I was executing this codeļ¼Œ

  File "/home/george/venv/bops/lib/python3.7/site-packages/ansible_runner/runner.py", line 114, in run
    command = self.config.command
AttributeError: 'RunnerConfig' object has no attribute 'command'

Can someone help me see what the problem is?
Thanks to all the friends who responded!

Asked By: Geroge

||

Answers:

Looks like the direct runner execution is not fully documented. When reading the code, a call to rc.prepare() should be done before calling r.run().

from ansible_runner import Runner, RunnerConfig

f = open('/home/george/dev/beeops/ansible_private/keyfile', 'r')
key = f.read()
rc = RunnerConfig(private_data_dir='/home/george/dev/beeops/ansible_private',
                  playbook='test.yml',
                  inventory='127.0.0.1',
                  ssh_key=key,)
rc.prepare()
r = Runner(config=rc)
r.run()

All this is done for you when using the helper interfaces as documented.

Answered By: zigarn
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.