2017-04-12 160 views
0

我使用ansible在ubuntu bashrc中添加永久环境变量。永久设置环境变量

enviornment_variables: 
    PRODUCTION: 
    MONGO_IP: 0.0.0.0 
    MONGO_PORT: 27017 
    ELASTIC_IP: localhost 
    ELASTIC_PORT: 9200 

怎么能使用task我出口: 我已经在prod_vars文件中定义这些设置吗?我kniow约lineinfile模块,但我不想再重复每一个的环境变量

- name: set env in the bashrc files 
    lineinfile: dest=/home/user/.bashrc line='export MONGO_IP=enviornment_variables[PRODUCTION][MONGO_IP]' 

也是上面的命令给synatx错误?

+0

不要这样做。相反,源文件(例如'.bashrc.d')使用Ansible中的模板在'.bashrc.d'下创建一个文件。 – techraf

+1

我该怎么做?也为什么'.bashrc.d'。? – anekix

回答

1

而不是使用lineinfile模块,使用blockinfile模块。 所以这样的事情应该工作: - name: Adding to environment variables for user blockinfile: path: /home/user/.bashrc insertafter: EOF block: | export {{ item.key }}={{ item.val }} marker: "# {mark} {{ item.key }}" with_dict: "{{ enviornment_variables['PRODUCTION'] }}"

PS:在“环境”的拼写错误字面上花了20多分钟,我确定!