Skip to content

Instantly share code, notes, and snippets.

@zeitounator
Last active February 2, 2023 18:31
Show Gist options
  • Save zeitounator/c6c0e27a60c2a3ea1e430353153f19a3 to your computer and use it in GitHub Desktop.
Save zeitounator/c6c0e27a60c2a3ea1e430353153f19a3 to your computer and use it in GitHub Desktop.
Using .bashrc with shell from ansible
docker run -d --rm --name test centos:7 bash -c "while true; do sleep 2000; done"
---
all:
hosts:
test:
ansible_connection: docker
---
- name: run bash function from shell
hosts: test
gather_facts: false
tasks:
- name: make sure function is in bashrc
blockinfile:
dest: ~/.bashrc
block: |-
my_test_func() {
echo hello test func
}
- name: run the function from shell after sourcing .bashrc
shell: source ~/.bashrc && my_test_func
register: func_result
- name: check result
debug:
var: func_result.stdout
$ ansible --version
ansible 2.8.4
config file = None
configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.6/dist-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.6.8 (default, Aug 20 2019, 17:12:48) [GCC 8.3.0]
$ ansible-playbook -i inventory.yml playbook.yml
PLAY [run bash function from shell] *******************************************************************************************************************************************************************************
TASK [make sure function is in bashrc] ****************************************************************************************************************************************************************************
changed: [test]
TASK [run the function from shell after sourcing .bashrc] *********************************************************************************************************************************************************
changed: [test]
TASK [check result] ***********************************************************************************************************************************************************************************************
ok: [test] => {
"func_result.stdout": "hello test func"
}
PLAY RECAP ********************************************************************************************************************************************************************************************************
test : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment