【问题标题】:Ansible windows module or command to trim or change a string variableAnsible windows 模块或命令来修剪或更改字符串变量
【发布时间】:2018-12-04 14:41:54
【问题描述】:

需要关于如何在运行 ansible playbook for windows 时从变量中删除或获取子字符串的建议或想法。

假设我有一个包含类似字符串的事实/变量

"c:apps\Directory\sub_directoty"

现在我需要操作/修剪这个字符串并获得一个新的事实/变量作为字符串:

"c:apps\Directory\"

我需要使用 Powershell 命令吗?

我的目的是,有一个 Windows 服务从不同的目录运行,我需要在服务指向的目录上安装更改。

我阅读和捕获服务路径的剧本任务通过以下方式完成:

tasks:
- name: Check if a service is installed
  win_service:
    name: ServerName 
  register: LINKServerInfo

- debug: msg="path is {{ LINKServerInfo.path }}"

- name: set linkpath  
  set_fact: linkpath="{{ LINKServerInfo.path }}"

【问题讨论】:

    标签: windows powershell ansible windows-services


    【解决方案1】:

    你可以使用“regex_replace”Ansible Filters

    对于你的情况,

     `---
     - hosts: localhost
       gather_facts: false
       vars:
         a: 'c:apps\Directory\sub_directoty'
       tasks:
        - name: Replace
          set_fact: 
            b: "{{ a | win_dirname }}"
        - debug: msg="{{ b }}" 
    

    输出:: "msg": "c:apps\\Directory"

    【讨论】:

      猜你喜欢
      • 2017-09-20
      • 2023-02-04
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 2014-10-30
      相关资源
      最近更新 更多