# Ansible pass result of a task as an input to another task in role

**URL:** <https://forum.ansible.com/t/ansible-pass-result-of-a-task-as-an-input-to-another-task-in-role/36101>\
**Category:** Ansible Project\
**Created:** [December 10, 2021, 2:38pm UTC](https://forum.ansible.com/t/ansible-pass-result-of-a-task-as-an-input-to-another-task-in-role/36101 "2021-12-10T14:38:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![RajG](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@RajG](https://forum.ansible.com/u/RajG)\
**Post date:** [December 10, 2021, 2:38pm UTC](https://forum.ansible.com/t/ansible-pass-result-of-a-task-as-an-input-to-another-task-in-role/36101/1 "2021-12-10T14:38:20Z")

</div>

This is my Ansible playbook …

roles/myrole ├── defaults │ └── main.yml ├── tasks │ ├── add\_user\_to\_group.yml │ ├── create\_user.yml │ └── main.yml ├── templates │ ├── add\_user\_to\_group\_data.j2 │ └── create\_user\_data.j2 └── vars └── main.yml

**role/myrole/tasks/create\_user.yml**

— - name: Creating User … uri: url: [https://www.example.com/api/users](https://www.example.com/api/users) method: POST body: “{{ create\_user\_data }}” body\_format: json status\_code: 200 return\_content: yes headers: Accept: application/json Content-Type: application/json x-api-key: my-api-key-here register: result - name: Saving User ID to a Variable set\_fact: user\_create\_id: result: “{{ result.json | community.general.json\_query(‘id’) }}” - name: Printing User ID of the User Created ansible.builtin.debug: msg: “{{ user\_create\_id }}”

tasks/add\_user\_to\_group.yml

— - name: Adding User to Group … uri: url: [https://www.example.com/api/usergroups/123456/members](https://www.example.com/api/usergroups/123456/members) method: POST body: “{{ add\_user\_to\_group\_data }}” body\_format: json status\_code: 200 return\_content: yes headers: Accept: application/json Content-Type: application/json x-api-key: my-api-key-here register: result

**templates/create\_user\_data.j2**

{ “username”:“john”, “[email":"john@gmail.com](mailto:email%22:%22john@gmail.com)”, “firstname”:“John”, “lastname”:“Smith” }

**templates/add\_user\_to\_group\_data.j2**

{ “op”: “add”, “type”: “user”, “id”: “{{ user\_create\_id }}” }

**vars/main.yml**

create\_user\_data: “{{ lookup(‘template’, ‘create\_user\_data.j2’) }}” add\_user\_to\_group\_data: “{{ lookup(‘template’, ‘add\_user\_to\_group\_data.j2’) }}”

Output of task create\_user.yml printing the ID

ok: [localhost] =\> { “msg”: { “result”: “71b248b35ea374433153bbe4” } }

I run the playbook basefile.yml which has the role myrole.

I’m trying to pass the ID from the result of the task create\_user.yml as an input variable to the another task add\_user\_to\_group.yml, especially in the template add\_user\_to\_group\_data.j2.

I unable to pass the variable {{ create\_user\_id }} to add\_user\_to\_group\_data.j2. If possible, we can create another task and call both the tasks passing some variables or to template file, but don’t know how.

How do I pass the json output field value from a task as an input to another task in the role?, any help is appreciated!.

Thanks in advance!
