【问题标题】:How to apply check in ansible to verify wether environment path already exists or not?如何在ansible中应用检查以验证环境路径是否已经存在?
【发布时间】:2017-02-15 09:33:04
【问题描述】:

我有一个使用 ansible 命令的 yml 文件。在文件中,我将服务位置附加到现有环境路径,如下所示:

 -name: retreiving the path
  raw: $ENV:PATH
  register: path_out

- name: adding the services to the environment path
  raw: SETX /M PATH "$ENV:PATH;{{item.service_path}}" 
  with_items:
    "{{ services }}"

因此,每次执行脚本时,即使先前添加了相同的服务路径,它也会将服务路径附加到现有环境路径,这会一次又一次地创建相同服务路径的附加。因此,我想要检查一下是否已经添加了 service_path,它不会在最后再次附加它。我尝试了以下但没有帮助:

- name: adding the services to the environment path
  raw: SETX /M PATH "$ENV:PATH;{{item.service_location}}" 
  with_items:
    "{{ services }}"
  when : ansible_env.PATH;{{item.service_location}}.exists is not defined

它根本不起作用请帮助如何检查?平台是我正在工作的窗口

【问题讨论】:

    标签: windows ansible


    【解决方案1】:

    您不应修改 PATH 变量本身。而是修改 /etc/environment 中的 PATH 或设置每个任务的 PATH(如果它仅在此处使用)。

    一个很好的例子可以在这里找到:https://coderwall.com/p/ynvi0q/updating-path-with-ansible-system-wide

    - name: add {{extra_path}} to path
      lineinfile: >
      dest=/etc/environment
      state=present
      backrefs=yes
      regexp='PATH=(["]*)((?!.*?{{extra_path}}).*?)(["]*)$'
      line="PATH=\1\2:{{extra_path}}\3"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 1970-01-01
      相关资源
      最近更新 更多