用于Vagrant的Ansible配置器中的$ PATH环境变量

时间:2014-10-14 23:16:10

标签: ubuntu vagrant ansible

当使用Ansible作为Vagrant的配置程序时,我在/etc/environment中定义的变量没有被选中。我找不到文档说明vagrant如何构建它的环境变量。

我应该修改哪个文件,以便Ansible供应商能够获取PATH环境的更新?

// In my foo role/task...

- name: check path
  command: msg="{{ print $PATH }}"

结果

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

我想修改我的$PATH,以便Ansible供应商和用户登录我的Vagrant框时可以使用它。

相关问题:

当前/ etc / environment配置:

vagrant@localhost:~$ cat /etc/environment 
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/bin/composer:/home/vagrant/.composer/vendor/bin"

roles/ansible_dev的内容:

- name: check path
  shell: echo $PATH
  register: PATH
- debug: var=PATH
# outputs /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

- name: update path
  shell: source /etc/environment;export PATH

# outputs the same, note my .composer directory still missing at the end
- name: check path AGAIN
  command: echo $PATH
  register: PATH2
- debug: var=PATH2

测试

vagrant up我正在vagrant provision --provision-with=ansible进行测试。

TASK: [ansible_dev | check path] ********************************************** 
changed: [localhost]

TASK: [ansible_dev | debug var=PATH] ****************************************** 
ok: [localhost] => {
    "PATH": {
        "changed": true, 
        "cmd": "echo $PATH", 
        "delta": "0:00:00.002239", 
        "end": "2014-10-15 03:04:34.604944", 
        "invocation": {
            "module_args": "echo $PATH", 
            "module_name": "shell"
        }, 
        "rc": 0, 
        "start": "2014-10-15 03:04:34.602705", 
        "stderr": "", 
        "stdout": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 
        "stdout_lines": [
            "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        ]
    }
}

TASK: [ansible_dev | update path] ********************************************* 
changed: [localhost]

TASK: [ansible_dev | check path AGAIN] **************************************** 
changed: [localhost]

TASK: [ansible_dev | debug var=PATH2] ***************************************** 
ok: [localhost] => {
    "PATH2": {
        "changed": true, 
        "cmd": [
            "echo", 
            "$PATH"
        ], 
        "delta": "0:00:00.003051", 
        "end": "2014-10-15 03:04:34.814571", 
        "invocation": {
            "module_args": "echo $PATH", 
            "module_name": "command"
        }, 
        "rc": 0, 
        "start": "2014-10-15 03:04:34.811520", 
        "stderr": "", 
        "stdout": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 
        "stdout_lines": [
            "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        ]
    }
}

2 个答案:

答案 0 :(得分:2)

所以我认为使用ansible来配置/etc/environment很好,当盒子完全配置并且你通过ssh登录时,你在那里做的更改应该在那些登录shell会话中生效。我的猜测是,ansible只进行一次初始ssh连接,根据初始/etc/environment和各种bash配置文件加载shell环境,从而为您提供初始PATH。然后您的剧本会更改/etc/environment,但不会自动重新加载当前会话。您可以幸运地使用命令来执行exec $SHELL,之后您的Playbook播放应该有新的PATH。如果仍然无效,请尝试source /etc/environment;export PATH

答案 1 :(得分:0)

对我来说,这个安全的任务效果最好:

- name: Export language
  command: /usr/sbin/update-locale LANG={{ locale }} LC_ALL={{ locale }} LANGUAGE={{ locale }}