# awx\_collection/tower\_credential target a specific credential

**URL:** <https://forum.ansible.com/t/awx-collection-tower-credential-target-a-specific-credential/33287>\
**Category:** AWX Project\
**Created:** [June 30, 2020, 2:39pm UTC](https://forum.ansible.com/t/awx-collection-tower-credential-target-a-specific-credential/33287 "2020-06-30T14:39:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joacim\_Martensson](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/joacim_martensson/32/4026_2.png) [@Joacim\_Martensson](https://forum.ansible.com/u/Joacim_Martensson)\
**Post date:** [June 30, 2020, 2:39pm UTC](https://forum.ansible.com/t/awx-collection-tower-credential-target-a-specific-credential/33287/1 "2020-06-30T14:39:50Z")

</div>

Hi,

Just started trying the awx\_collection.

Using tower\_credential, is it possible target a specific credential other than by name?

I have 2 credentials with the same name (in different organizations) and is getting  
“An unexpected number of items was returned from the API (2)”

---

<div class="post-metadata">

**Author:** ![john.westcott.iv](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/john.westcott.iv/32/95_2.png) [@john.westcott.iv](https://forum.ansible.com/u/john.westcott.iv)\
**Post date:** [June 30, 2020, 3:25pm UTC](https://forum.ansible.com/t/awx-collection-tower-credential-target-a-specific-credential/33287/2 "2020-06-30T15:25:30Z")

</div>

You should be able to use an ID in the name field to specify a specific credential.  
i.e. to remove credential with ID of number 1:

- tower\_credential:  
name: 1  
state: absent

This works for all the tower\_\* modules.

Knowing this might not be the most convenient solution, work is being done to add a tower lookup plugin to help you with scenarios like this.  
For example, you might be able to do something like this in the future:

- tower\_credential:  
name: “{{ lookup(’tower\_api’, ‘credential’, query\_data={ ’name’: ‘my non-unique name’, ‘organization’: ‘Default’ }) }}”  
state: absent

The details of the implementation are not 100% right now as this is still a work in progress but keep checking back for this feature in the future.

-John

---

<div class="post-metadata">

**Author:** ![AlanCoding](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alancoding/32/301_2.png) [@AlanCoding](https://forum.ansible.com/u/AlanCoding)\
**Post date:** [June 30, 2020, 5:05pm UTC](https://forum.ansible.com/t/awx-collection-tower-credential-target-a-specific-credential/33287/3 "2020-06-30T17:05:27Z")

</div>

There is an open pull request which implements this lookup plugin here:

[https://github.com/ansible/awx/pull/7286](https://github.com/ansible/awx/pull/7286)

We are fairly close to getting it merged, and this is one of the top current priorities for the collection. If anyone has feedback on the implementation, now would be a great time to weigh in.

I also want to point out that most modules return an id value, so it’s a common pattern to call the first module, tower\_credential, with `register: result` and then pass `{{ [result.id](http://result.id) }}` into the next module. The logic to recognize ids is also there for the many-related fields, so if you use the tower\_job\_template module, you should be able to pass `credentials: [23, 42, 349]`, for example.

---

<div class="post-metadata">

**Author:** ![Joacim\_Martensson](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/joacim_martensson/32/4026_2.png) [@Joacim\_Martensson](https://forum.ansible.com/u/Joacim_Martensson)\
**Post date:** [July 1, 2020, 8:30am UTC](https://forum.ansible.com/t/awx-collection-tower-credential-target-a-specific-credential/33287/4 "2020-07-01T08:30:54Z")

</div>

Hi,

I am able to get the id from the tower\_credential but then using that id value in tower\_role it does not work.

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\>  
tower\_output.id: ‘61’

TASK [Give team access to credential] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> changed=false  
invocation:  
module\_args:  
credential: ‘61’  
msg: Failed to update role, credential not found in credentials

tisdag 30 juni 2020 kl. 19:05:59 UTC+2 skrev [arom...@redhat.com](mailto:arom...@redhat.com):

---

<div class="post-metadata">

**Author:** ![Joacim\_Martensson](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/joacim_martensson/32/4026_2.png) [@Joacim\_Martensson](https://forum.ansible.com/u/Joacim_Martensson)\
**Post date:** [July 1, 2020, 9:54am UTC](https://forum.ansible.com/t/awx-collection-tower-credential-target-a-specific-credential/33287/5 "2020-07-01T09:54:07Z")

</div>

Also when using this id when targeting a credential like John suggested doesn’t work. A new credential with name “id number” is created.

Can it be that I’m using ansible.tower collection v3.7.1 from Automation Hub and Ansible Tower 3.7.1.  
Maybe Red Hat is just behind the commits in awx and it does not work because of that.

/Jocke

onsdag 1 juli 2020 kl. 10:30:55 UTC+2 skrev Joacim Mårtensson:

---

<div class="post-metadata">

**Author:** ![AlanCoding](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alancoding/32/301_2.png) [@AlanCoding](https://forum.ansible.com/u/AlanCoding)\
**Post date:** [July 1, 2020, 10:50am UTC](https://forum.ansible.com/t/awx-collection-tower-credential-target-a-specific-credential/33287/6 "2020-07-01T10:50:36Z")

</div>

Did you pass the organization parameter to the tower\_credential module? That should disambiguate them if it’s only a matter of 1 name in 2 organizations.

I was thinking about related objects as ids, but sounds like this doesn’t actually fit your situation.

---

<div class="post-metadata">

**Author:** ![Joacim\_Martensson](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/joacim_martensson/32/4026_2.png) [@Joacim\_Martensson](https://forum.ansible.com/u/Joacim_Martensson)\
**Post date:** [July 1, 2020, 11:23am UTC](https://forum.ansible.com/t/awx-collection-tower-credential-target-a-specific-credential/33287/7 "2020-07-01T11:23:56Z")

</div>

Hi,

Yes for tower\_credential I specified organization.  
In tower\_role I cant as then it thinks Ill altering the role for the organization instead of the team.

/Jocke

onsdag 1 juli 2020 kl. 12:50:58 UTC+2 skrev [arom...@redhat.com](mailto:arom...@redhat.com):

---

<div class="post-metadata">

**Author:** ![AlanCoding](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alancoding/32/301_2.png) [@AlanCoding](https://forum.ansible.com/u/AlanCoding)\
**Post date:** [July 1, 2020, 12:02pm UTC](https://forum.ansible.com/t/awx-collection-tower-credential-target-a-specific-credential/33287/8 "2020-07-01T12:02:28Z")

</div>

Okay, I got turned around with tower\_role and tower\_credential. Your problem is specifying a related object for the tower\_role module.

So then does the task that doesn’t work look like:

tower\_role:  
credential: 5  
user: Joacim  
state: present  
role: use

I think we probably do have a bug, where the tower\_role module, specifically, doesn’t try the given value as a primary key. If this is correct, then one of us should file an issue for it.

---

<div class="post-metadata">

**Author:** ![Joacim\_Martensson](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/joacim_martensson/32/4026_2.png) [@Joacim\_Martensson](https://forum.ansible.com/u/Joacim_Martensson)\
**Post date:** [July 1, 2020, 12:25pm UTC](https://forum.ansible.com/t/awx-collection-tower-credential-target-a-specific-credential/33287/9 "2020-07-01T12:25:33Z")

</div>

Hi,

Yeah sorry about the mix up.

I have issues when trying to target things with tower\_role and tower\_credential.  
Using an id would have helped but that does not work.

First I create two credentials with the same name but in different organizations

- name: Create new machine credentials  
tower\_credential:  
name: testcred  
organization: testorg1

credential\_type: Machine  
state: present  
inputs:  
ssh\_key\_data: “{{ mykey }}”  
register: tower\_output1

- name: Create new machine credentials  
tower\_credential:  
name: testcred  
organization: testorg2

credential\_type: Machine  
state: present  
inputs:  
ssh\_key\_data: “{{ mykey }}”  
register: tower\_output2

The I tried using tower\_role to give a team access to the first credential

- name: Give team access to credential

tower\_role:  
credential: testcred

role: use  
team: testteam

This ends up in:

fatal: [localhost]: FAILED! =\> changed=false  
msg: An unexpected number of items was returned from the API (2)

Then I tried using the id instead

tower\_role:  
credential: “{{ tower\_output1.id }}”

This gives:

fatal: [localhost]: FAILED! =\> changed=false  
invocation:  
module\_args:  
credential: ‘61’

…

msg: Failed to update role, credential not found in credentials

I’m also trying to use the id to target a specific credential to change the organization, like this:

- name: Change organization of the first credential  
tower\_credential:  
name: “{{ tower\_output1.id }}”  
organization: testorg2

credential\_type: Machine  
state: present  
inputs:  
ssh\_key\_data: “{{ mykey }}”

This ends up with a new credential named integer value of ’ tower\_output1.id’ in the new organization.

Thank you  
Jocke

onsdag 1 juli 2020 kl. 14:02:50 UTC+2 skrev [arom...@redhat.com](mailto:arom...@redhat.com):
