# Following #11240 : "The async task did not return valid JSON"

**URL:** <https://forum.ansible.com/t/following-11240-the-async-task-did-not-return-valid-json/18596>\
**Category:** Ansible Developer\
**Created:** [June 24, 2015, 5:48pm UTC](https://forum.ansible.com/t/following-11240-the-async-task-did-not-return-valid-json/18596 "2015-06-24T17:48:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arthur\_Clement](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@Arthur\_Clement](https://forum.ansible.com/u/Arthur_Clement)\
**Post date:** [June 24, 2015, 5:48pm UTC](https://forum.ansible.com/t/following-11240-the-async-task-did-not-return-valid-json/18596/1 "2015-06-24T17:48:00Z")

</div>

Hi,

I had the #11240 bug with async tasks ([https://github.com/ansible/ansible/issues/11240](https://github.com/ansible/ansible/issues/11240)) with 2.0-devel so I pulled & rebased my ansible installation.

Now I got this error with async tasks :

fatal: [coreos-01]: FAILED! =\> {“msg”: “The async task did not return valid JSON: No JSON object could be decoded”, “failed”: true, “changed”: false}

my task :  
-name: Mount mongo LVM  
command: sudo systemctl start data-mongodb-01.mount  
ignore\_errors: yes  
async: 15

I just have to remove the async parameter and wait for the timeout it the task fails.

git version :

commit 4fbd4ae18b39883152f790bf2e59fdfdff973bc7

Regards,

Arthur

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 26, 2015, 3:28am UTC](https://forum.ansible.com/t/following-11240-the-async-task-did-not-return-valid-json/18596/2 "2015-06-26T03:28:51Z")

</div>

can you run with -vvvv?

IIRC coreos version of python was 'special' so we might need to make  
additional adjustments.

---

<div class="post-metadata">

**Author:** ![Arthur\_Clement](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@Arthur\_Clement](https://forum.ansible.com/u/Arthur_Clement)\
**Post date:** [June 26, 2015, 8:43am UTC](https://forum.ansible.com/t/following-11240-the-async-task-did-not-return-valid-json/18596/3 "2015-06-26T08:43:13Z")

</div>

Hi,

Without async :  
[https://gist.github.com/arthur-c/31b239361eb21c857247](https://gist.github.com/arthur-c/31b239361eb21c857247)

With async:  
[https://gist.github.com/arthur-c/8904f89862de7b177295](https://gist.github.com/arthur-c/8904f89862de7b177295)

I bootstraped ansible on CoreOs with defunctzombie.coreos-bootstrap.

Arthur

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 27, 2015, 2:12pm UTC](https://forum.ansible.com/t/following-11240-the-async-task-did-not-return-valid-json/18596/4 "2015-06-27T14:12:11Z")

</div>

how long does the task take w/o async?

---

<div class="post-metadata">

**Author:** ![Ricardo\_Carrillo\_Cru](https://avatars.discourse-cdn.com/v4/letter/r/87869e/32.png) [@Ricardo\_Carrillo\_Cru](https://forum.ansible.com/u/Ricardo_Carrillo_Cru)\
**Post date:** [August 21, 2015, 11:43am UTC](https://forum.ansible.com/t/following-11240-the-async-task-did-not-return-valid-json/18596/5 "2015-08-21T11:43:50Z")

</div>

Hi there

I’m also getting this behaviour.  
Running a simple command with async, I get “The async task did not return valid JSON ansible”.  
Running the same command on the remote box works just fine and takes about 5 minutes to run.

This is the play:

- name: Trigger ‘jenkins-jobs update’ to populate Jenkins jobs  
command: jenkins-jobs update /etc/jenkins\_jobs/config --delete-old  
async: 3600  
poll: 0

And this is the output with -vvvv:

[http://paste.openstack.org/show/423349/](http://paste.openstack.org/show/423349/)

Regards

---

<div class="post-metadata">

**Author:** ![Uli\_Martens](https://avatars.discourse-cdn.com/v4/letter/u/45deac/32.png) [@Uli\_Martens](https://forum.ansible.com/u/Uli_Martens)\
**Post date:** [August 31, 2015, 1:40pm UTC](https://forum.ansible.com/t/following-11240-the-async-task-did-not-return-valid-json/18596/6 "2015-08-31T13:40:14Z")

</div>

Hello everybody,

> I’m also getting this behaviour.  
> Running a simple command with async, I get “The async task did not return valid JSON ansible”.  
> Running the same command on the remote box works just fine and takes about 5 minutes to run.

I’m also running into this problem. Right now, I’ve traced it back to some problem with the new ‘become’ logic putting additional prompts into the stdout, so that stdout isn’t valid JSON any more. You can see this by applying the following hack to the latest devel checkout:

diff --git a/lib/ansible/executor/task\_executor.py b/lib/ansible/executor/task\_executor.py  
index fdc57b8…4849686 100644  
— a/lib/ansible/executor/task\_executor.py  
+++ b/lib/ansible/executor/task\_executor.py  
@@ -322,7 +322,7 @@ class TaskExecutor:  
try:  
result = json.loads(result.get(‘stdout’))  
except (TypeError, ValueError) as e:

- return dict(failed=True, msg=“The async task did not return valid JSON: %s” % str(e))

- return dict(failed=True, msg=“The async task did not return valid JSON: %s, \>%s\<” % (str(e), result.get(‘stdout’)))

if self.\_task.poll \> 0:  
result = self.\_poll\_async\_result(result=result, templar=templar)

When run with this adhoc debugging hack, I’m getting the following output from the task:

fatal: [testhost]: FAILED! =\> {“changed”: false, “failed”: true, “msg”: “The async task did not return valid JSON: No JSON object could be decoded, \>BECOME-SUCCESS-uuqnvruwvstqxuyjaewoolahhkowahip\r\n{"started": 1, "results\_file": "/root/.ansible\_async/956808343353.10137", "ansible\_job\_id": "956808343353.10137"}\r\n\<”}

Now I only have to understand if that become-successkey has to be there or how to strip it…

Uli / youam

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [August 31, 2015, 3:35pm UTC](https://forum.ansible.com/t/following-11240-the-async-task-did-not-return-valid-json/18596/7 "2015-08-31T15:35:05Z")

</div>

This has not fundamentally changed, the previous SUDO-SUCCESS key  
worked in exactly the same way. We were probably cleaning that up  
before.

---

<div class="post-metadata">

**Author:** ![Uli\_Martens](https://avatars.discourse-cdn.com/v4/letter/u/45deac/32.png) [@Uli\_Martens](https://forum.ansible.com/u/Uli_Martens)\
**Post date:** [August 31, 2015, 8:59pm UTC](https://forum.ansible.com/t/following-11240-the-async-task-did-not-return-valid-json/18596/8 "2015-08-31T20:59:52Z")

</div>

Me again.

[..]

> Right now, I’ve traced it back to some problem with the new ‘become’ logic putting additional prompts into the stdout, so that stdout isn’t valid JSON any more. You can see this by applying the following hack to the latest devel checkout:

[..]

> Now I only have to understand if that become-successkey has to be there or how to strip it…

Turns out, for “regular” tasks, the “stdout” gets set/filtered in lib/ansible/plugins/action/ **init**.py, \_execute\_module() by this code:

> data = json.loads(self.\_filter\_leading\_non\_json\_lines(res.get(‘stdout’, ‘’)))

where the \_filter\_leading\_non\_json\_lines function states:

> Used to avoid random output from SSH at the top of JSON output, like messages from
> 
> tcagetattr, or where dropbear spews MOTD on every single command (which is nuts).
> 
> need to filter anything which starts not with ‘{’, ‘[’, ', ‘=’ or is an empty line.
> 
> filter only leading lines since multiline JSON is valid.

Apparently, this unintentionally (?) also stripped out the BECOME\_SUCCESS message - except for async tasks.

Calling the same wrapper fixes my problem. The code has some special cases for skipping stuff if we’re going to use sudo while still connecting as root, which I am (rescue system, root’s the only user there is), so the actual problem might lie elsewhere, though. This feels like a really bad hack, and I’m by far not sure whether my hack breaks more than it fixed, so I’m not going to push this upstream.

— a/lib/ansible/executor/task\_executor.py  
+++ b/lib/ansible/executor/task\_executor.py  
@@ -320,7 +320,7 @@ class TaskExecutor:

# the async\_wrapper module returns dumped JSON via its stdout

# response, so we parse it here and replace the result

try:

- result = json.loads(result.get(‘stdout’))

- result = json.loads(self.\_handler.\_filter\_leading\_non\_json\_lines(result.get(‘stdout’)))  
except (TypeError, ValueError) as e:  
return dict(failed=True, msg=“The async task did not return valid JSON: %s” % str(e))

hope this helps someone and/or helps diagnosing the problem.

Uli / youam

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [September 1, 2015, 3:07pm UTC](https://forum.ansible.com/t/following-11240-the-async-task-did-not-return-valid-json/18596/9 "2015-09-01T15:07:18Z")

</div>

not sure that is the correct fix as we should be striping the become  
output when verifying become success, but I don't know enough of how  
async is implemented in relation to privilege escalation to make a  
firm statement.
