如何使用Ansible扩展Windows路径变量 [英] how to extend windows path variable using ansible

查看:115
本文介绍了如何使用Ansible扩展Windows路径变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用win_environment,可以向Windows主机添加/删除环境变量. 但是要修改已经存在的变量,win_environment似乎没有用,因为您无法读取旧值来修改和更新变量.对吧?

Using win_environment, it is possible to add/remove environment variables to a windows host. But to modify variables that are already there, win_environment does not seem to be useful as u can't read old value to modify and update a variable. right?

推荐答案

编辑:自Ansible 2.3起,

Since Ansible 2.3, the win_path module does all the heavy lifting for you. Just give it a list of items that should be present in the path and it'll make sure they're present and in the relative order you specified.

(如果您仍在使用Ansible的旧版本,则仍然可以使用以下方法)

(if you're still using an ancient version of Ansible, the following is still the way to go)

要使其正常运行,您将希望与替换和搜索过滤器结合使用,以仅在所需值不存在的情况下进行更改.例如(这是用于Ansible 1.9):

To get this to work sanely, you'll want to combine with a replace and search filter to only make the change if the value you want isn't in there. For instance (this is for Ansible 1.9):

  - raw: echo %PATH%
    register: path_out

  - win_environment: 
      name: path
      value: "{{ path_out.stdout | regex_replace('[\r\n]*', '') + ';C:\\\\newpath' }}"
      state: present
      level: machine
    when: not (path_out.stdout | search("(?i)c:\\\\newpath"))

这比应该做的要难得多-我花了半个心思破解2.0的win_path模块,以使其更容易...

This is a lot harder than it should be- I've got half a mind to hack up a win_path module for 2.0 to make it easier...

对于2.0,raw在Powershell下运行,因此您希望使用Get-Item env:PATH.

For 2.0, raw runs under Powershell, so you'd want Get-Item env:PATH instead.

这篇关于如何使用Ansible扩展Windows路径变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆