0

I have following file that I need to source in multiple plays in a playbook.

---
- hosts: all
  tasks:
   - shell: source /foo/bar && export API_VER=4 && blah123
   - shell: source /foo/bar && export API_VER=4 && blah456
   - shell: source /foo/bar && export API_VER=4 && blah678

I am trying to find a way to avoid repeating source /foo/bar, also note that /foo/bar is very large file, as I cannot add individually using environments: option as it will be tedious.

Also, I don't want to add the source file content in user profile or bashrc file. as this sourcing is used only at the time of running the playbook.

Any ideas?

smc
  • 2,175
  • 3
  • 17
  • 30
  • Either use an [include](https://docs.ansible.com/ansible/2.9/modules/include_module.html) or write a [role](https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html) would be what would come into my mind. – β.εηοιτ.βε Nov 15 '20 at 20:37
  • 1
    Ansible does not use a persistent shell session when running tasks, so there's no way to export into the session once and keep those vars around. There is no facility in ansible to modify the global environment via sourcing a script. I would add the content to user profile, bashrc, etc/environment at the beginning of the playbook, and remove it at the end. – Casey Nov 19 '20 at 07:34

0 Answers0