0

I've been trying to use the Ansible Python API as explained in their official docs here. The problem is, when I call main() Ansible returns an error saying:

the connection plugin '<class 'ansible.utils.sentinel.Sentinel'>' was not found

I'm running ansible-base version 2.10.8.

I've been stuck with this for two days now. I saw someone with the same issue over at Ansible's github page here and it seems like they don't even support their own Python API.

Can anyone suggest how I could solve this problem?

Saman Hamidi
  • 353
  • 1
  • 7
  • 20
  • 3
    `it seems they don't support their own Python API` => in the notes on the top of the documentation you l linked in your very first paragraph: `This API is intended for internal Ansible use. Ansible may make changes to this API at any time that could break backward compatibility with older versions of the API. Because of this, external use is not supported by Ansible` – Zeitounator Apr 17 '21 at 06:56
  • 1
    Since you asked for a suggestion: don't use the ansible internal python API in your external project. – Zeitounator Apr 17 '21 at 06:58
  • Yes. I am aware of that and I did try working with ansible-runner first. However, ansible-runner returns a set of nested dictionaries with ansible result inside the stdout key. It's not exactly what I need. Is this to say that using the actual API is hopeless? – Saman Hamidi Apr 17 '21 at 06:58
  • Yes well I have already tried the ansible-runner solution. – Saman Hamidi Apr 17 '21 at 06:59
  • 2
    Ok so that's what I thought: we are in an X/Y problem. Change the stdout callback to what suits your needs. Some info here https://docs.ansible.com/ansible/latest/plugins/callback.html . See also this answer: https://stackoverflow.com/a/50017860/9401096 – Zeitounator Apr 17 '21 at 06:59
  • how about using Popen to just send the Ansible command to shell? – Saman Hamidi Apr 17 '21 at 07:01
  • Also it seems as if ansible-runner does not support ANSIBLE_JSON_CALLBACK – Saman Hamidi Apr 17 '21 at 07:04
  • Then execute ansible playbook as a normal external process as you suggested. And side note: if you are "aware of that" (the api is not supported...), don't write in your question that it "seems it is not supported" when you already know you are walking on a dangerous path. – Zeitounator Apr 17 '21 at 07:06
  • Well at the moment using the API sounded like a good idea. Also it was used previously and worked pretty well so I thought it might work this time. meh. – Saman Hamidi Apr 17 '21 at 07:09

1 Answers1

3

I fixed my problem but it took me a few days and lots of debugging. For reasons which I will not discuss here (my tech-lead kind of forced me), I had to use this API. I recommend not using it at all outside Ansible development. It was a time consuming and bad experience in general.

This error is masking the real issue. If you see this exception it means something is broken somewhere else. It is usually inside task_executor.py. Check your code and make sure you have no typos and all arguments are provided. Python API seems to lack correct exception handling and hence the exception messages are somewhat confusing.

For me it was because I didn't provide context.CLIARGS with verbosity argument which is also missing from the brief example provided in the Ansible docs.

Saman Hamidi
  • 353
  • 1
  • 7
  • 20
  • 1
    Good point IMHO is pointed here https://stackoverflow.com/a/63939935/6709058 – Egor Jul 28 '21 at 12:55
  • Thank you. At the time that I asked this question, I also read this post. It was great and gave me a better idea but after a point you're really on your own. – Saman Hamidi Jul 28 '21 at 13:15