在运行适用于windows的ansible playbook时,需要关于如何从变量中删除或获取子字符串的建议或想法。
假设我有一个包含类似的字符串的事实/变量
"c:appsDirectorysub_directoty"
现在我需要操作/修剪这个字符串,并获得一个新的事实/变量作为字符串:
"c:appsDirectory"
我需要使用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 }}"
您可以使用"regex_replace"Ansible Filters
对于您的情况,
`---
- hosts: localhost
gather_facts: false
vars:
a: 'c:appsDirectorysub_directoty'
tasks:
- name: Replace
set_fact:
b: "{{ a | win_dirname }}"
- debug: msg="{{ b }}"
输出::"msg": "c:apps\Directory"