# Regarding the issue of using ansible to execute the "source" command

**URL:** <https://forum.ansible.com/t/regarding-the-issue-of-using-ansible-to-execute-the-source-command/44337>\
**Category:** Get Help\
**Tags:** ansible, kubernetes\
**Created:** [August 21, 2025, 4:29am UTC](https://forum.ansible.com/t/regarding-the-issue-of-using-ansible-to-execute-the-source-command/44337 "2025-08-21T04:29:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pptfz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/pptfz/32/8536_2.png) [@pptfz](https://forum.ansible.com/u/pptfz)\
**Post date:** [August 21, 2025, 4:29am UTC](https://forum.ansible.com/t/regarding-the-issue-of-using-ansible-to-execute-the-source-command/44337/1 "2025-08-21T04:29:36Z")

</div>

I have three machines, and each of them has Helm installed. The installation path is `/usr/local/bin`.

I added the configuration for automatic completion of the helm command in the `~/.bashrc` file.

```auto
grep helm ~/.bashrc 
source <(/usr/local/bin/helm completion bash)

```

Executing commands in the shell terminal takes effect.

```auto
source ~/.bashrc

```

However, it doesn’t work when executed using Ansible.

```auto
$ ansible all -m shell -a 'source ~/.bashrc'
k8s-node01 | CHANGED | rc=0 >>
/root/.bashrc: line 33: /usr/local/bin/helm: No such file or directory
k8s-node02 | CHANGED | rc=0 >>
/root/.bashrc: line 33: /usr/local/bin/helm: No such file or directory
k8s-master01 | CHANGED | rc=0 >>

```

How should this problem be solved?

---

<div class="post-metadata">

**Author:** ![Lyle\_McKarns](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lyle_mckarns/32/8142_2.png) [@Lyle\_McKarns](https://forum.ansible.com/u/Lyle_McKarns)\
**Post date:** [August 21, 2025, 12:30pm UTC](https://forum.ansible.com/t/regarding-the-issue-of-using-ansible-to-execute-the-source-command/44337/2 "2025-08-21T12:30:38Z")

</div>

What user is ansible using to connect?

Where is that users’ home directory?

Is there a `.bashrc` file there?

But I think the best question is, what problem are you trying to solve by having autocompletion available to ansible? Its not like you’d be tab-completing commands via ansible.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [August 21, 2025, 2:21pm UTC](https://forum.ansible.com/t/regarding-the-issue-of-using-ansible-to-execute-the-source-command/44337/3 "2025-08-21T14:21:47Z")

</div>

Ansible, by default uses `/bin/sh`, not bash, even if `/bin/sh` is implemented via bash it is normally in ‘posix compatibility mode’ and not all bash features will be available.

Also note that each task is run in isolation so `source` in one task, won’ t affect subsequent tasks.

---

<div class="post-metadata">

**Author:** ![Lyle\_McKarns](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lyle_mckarns/32/8142_2.png) [@Lyle\_McKarns](https://forum.ansible.com/u/Lyle_McKarns)\
**Post date:** [August 21, 2025, 2:22pm UTC](https://forum.ansible.com/t/regarding-the-issue-of-using-ansible-to-execute-the-source-command/44337/4 "2025-08-21T14:22:37Z")

</div>

> [@bcoca](#):
>
> Ansible, by default uses `/bin/sh`, not bash,

Ohh, good catch there, I hadn’t considered that bit.

---

<div class="post-metadata">

**Author:** ![pptfz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/pptfz/32/8536_2.png) [@pptfz](https://forum.ansible.com/u/pptfz)\
**Post date:** [August 22, 2025, 2:07am UTC](https://forum.ansible.com/t/regarding-the-issue-of-using-ansible-to-execute-the-source-command/44337/5 "2025-08-22T02:07:31Z")

</div>

It is the `root` user. The home directory of the root user is `/root` , and there is the file `/root/.bashrc`. What I want to achieve is to execute `source ~/.bashrc` in the terminal alone, which can be done successfully. However, when using Ansible to execute this command, it does not work.

---

<div class="post-metadata">

**Author:** ![pptfz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/pptfz/32/8536_2.png) [@pptfz](https://forum.ansible.com/u/pptfz)\
**Post date:** [August 22, 2025, 2:11am UTC](https://forum.ansible.com/t/regarding-the-issue-of-using-ansible-to-execute-the-source-command/44337/6 "2025-08-22T02:11:21Z")

</div>

That is to say, it is impossible to achieve the same effect as executing `source ~/.bashrc` alone in the terminal through Ansible. Executing commands like `ansible all -m shell -a 'source ~/.bashrc` is not feasible.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [August 22, 2025, 2:28am UTC](https://forum.ansible.com/t/regarding-the-issue-of-using-ansible-to-execute-the-source-command/44337/7 "2025-08-22T02:28:47Z")

</div>

Not impossible, you just need to make sure you execute bash, also this is just not very useful as the next task won’t reuse the same session nor shell.

On top of that, autocompletion of other shell utilities makes no sense in the case of ansible as it has no access to the remote shell when constructing tasks.

---

<div class="post-metadata">

**Author:** ![pptfz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/pptfz/32/8536_2.png) [@pptfz](https://forum.ansible.com/u/pptfz)\
**Post date:** [August 22, 2025, 2:38am UTC](https://forum.ansible.com/t/regarding-the-issue-of-using-ansible-to-execute-the-source-command/44337/8 "2025-08-22T02:38:44Z")

</div>

Then I’ll look for other methods. If this kind of writing is used, it will definitely not work. `ansible all -m shell -a 'source ~/.bashrc'`

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [August 22, 2025, 2:44am UTC](https://forum.ansible.com/t/regarding-the-issue-of-using-ansible-to-execute-the-source-command/44337/9 "2025-08-22T02:44:21Z")

</div>

again, you are not using bash:

```auto
 -a 'executable=/bin/bash cmd="source ~/.bashrc"'

```

even then, while it will execute, it will not do anything meaningful

---

<div class="post-metadata">

**Author:** ![pptfz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/pptfz/32/8536_2.png) [@pptfz](https://forum.ansible.com/u/pptfz)\
**Post date:** [August 22, 2025, 3:13am UTC](https://forum.ansible.com/t/regarding-the-issue-of-using-ansible-to-execute-the-source-command/44337/10 "2025-08-22T03:13:41Z")

</div>

Thank you for your reply. I think my idea was somewhat incorrect. After I executed the command `ansible all -b -m lineinfile -a "path=~/.bashrc line='source <(helm completion bash)' state=present"`, it only needed to be manually executed in the terminal or re-login next time for it to take effect.
