# Passing credentials to an action plugin

**URL:** <https://forum.ansible.com/t/passing-credentials-to-an-action-plugin/3299>\
**Category:** Get Help\
**Created:** [January 13, 2024, 8:22pm UTC](https://forum.ansible.com/t/passing-credentials-to-an-action-plugin/3299 "2024-01-13T20:22:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lioradler](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lioradler/32/1653_2.png) [@lioradler](https://forum.ansible.com/u/lioradler)\
**Post date:** [January 13, 2024, 8:22pm UTC](https://forum.ansible.com/t/passing-credentials-to-an-action-plugin/3299/1 "2024-01-13T20:22:17Z")

</div>

Hello again,

So I currently work on my first action plugin and I have a noob question: what is the best practice for passing credentials to the ‘run’ class method?

Assume we have a playbook that calls the role containing the plugin:

```auto
- name: "Call a role"
  import_role:
    name: "example"

```

and inside the example role we have a task triggering the plugin:

```auto
- name: "Call plugin"
  plugin:
    username: "{{ username }}"
    password: "{{ password }}"

```

So the question is where and how can I define _username_ and _password_ without having these values exposed in the output?

---

<div class="post-metadata">

**Author:** ![jpmens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jpmens/32/5543_2.png) [@jpmens](https://forum.ansible.com/u/jpmens)\
**Post date:** [January 14, 2024, 9:08am UTC](https://forum.ansible.com/t/passing-credentials-to-an-action-plugin/3299/2 "2024-01-14T09:08:20Z")

</div>

All play variables are passed into `task_vars`, automatically, so you don’t need to specify these as plugin parameters.

Say you you have a variable `dinner` be it specified as extravars, inventory vars, etc. this is automatically passed into you action plugin:

```python
#!/usr/bin/env python
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.plugins.action import ActionBase

try:
    from __main__ import display
except ImportError:
    from ansible.utils.display import Display
    display = Display()

class ActionModule(ActionBase):

    BYPASS_HOST_LOOP = False # if True, runs once per play

    def run(self, tmp=None, task_vars=None):
        '''Run action plugin'''

        meal = task_vars['dinner']
        display.vv(meal)

        result = {
            'changed': False,
            'failed': False,
            'msg': 'okbla',
        }

        return result

```

```yaml
- hosts: alice
  tasks:
    - aplug:

```

```console
$ ansible-playbook jp.yml -e dinner=Doener -vv
...
TASK [aplug] ************************************************************************
task path: /.../ansible-tests/action_plugins/vars/jp.yml:10
Doener
ok: [alice] => {"changed": false, "msg": "okbla"}

```

---

<div class="post-metadata">

**Author:** ![lioradler](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lioradler/32/1653_2.png) [@lioradler](https://forum.ansible.com/u/lioradler)\
**Post date:** [January 17, 2024, 9:52am UTC](https://forum.ansible.com/t/passing-credentials-to-an-action-plugin/3299/3 "2024-01-17T09:52:05Z")

</div>

@jpmens thank you for the answer!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 16, 2024, 9:53am UTC](https://forum.ansible.com/t/passing-credentials-to-an-action-plugin/3299/4 "2024-02-16T09:53:04Z")

</div>

Closed on February 16, 2024, 9:53am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
