Issue with running .bash_profile by ansible

2019-08-08 05:07发布

问题:

I want to export multiple variables through ansible. I have this task in my playbook

- name: Set the MIRA_ROOT and Binding_Address in Profile
  lineinfile: dest=/root/.bash_profile insertbefore='^export PATH' line="  {{item.line}}"
  with_items:
      - { line: 'export JAVA_HOME=/usr/java/jdk{{jdk_version}}' }
      - { line: 'export MIRA_ROOT=/opt/data/rg01/mira' }

How to run .bash_profile?I have written this code to run .bash_profile but it could not work

- name: Run the bash_profile
  shell: source /root/.bash_profile

回答1:

What's your goal? Do you want to permanently set those vars or only for your ansible play? In any case .bash_profile probably is not the best solution.

Ansible initiates a new ssh connection for every task. If you'd source your .bash_profile in one task to set an environment var, in the next task it wouldn't be available.

If you want to set it permanently, you can write it to /etc/environment, with the lineinfile module just like you did.

If you only want to set it for the tasks of the Ansible play, you can set it in your playbook:

- hosts: ...
  environment:
    JAVA_HOME: /usr/java/jdk{{jdk_version}}
    MIRA_ROOT: /opt/data/rg01/mira
  tasks: ...
  roles: ...


标签: ansible