Ansible 将文件夹中具有特定文件扩展名的多个文件更改为另一个扩展名
在包含具有不同扩展名的文件的文件夹中(Ansible 将文件夹中具有特定文件扩展名的多个文件更改为另一个扩展名,ansible,Ansible,在包含具有不同扩展名的文件的文件夹中(*.rules和*.rules.yml),我需要根据特定条件更改文件扩展名: *.rules=>*.rules.yml,或 *.rules.yml=>*.rules 在shell中,我可以按如下方式执行: Case # 1 for file in ./*.rules; do mv "$file" "${file%.*}.rules.yml" ; done # from *.rules to *.rules.yml Case # 2 for file i
*.rules
和*.rules.yml
),我需要根据特定条件更改文件扩展名:
*.rules
=>*.rules.yml
,或*.rules.yml
=>*.rules
Case # 1
for file in ./*.rules; do mv "$file" "${file%.*}.rules.yml" ; done
# from *.rules to *.rules.yml
Case # 2
for file in ./*.rules.yml ; do mv "$file" "${file%.*.*}.rules" ; done
# from *.rules.yml to *.rules
有没有想过要做同样的事情
任何帮助都将不胜感激:)假设您遇到的困难是YAML报价,您可能会在“管道文字”中遇到更好的运气: 人们还会注意到,我使用了更传统的
basename
,而不是尝试“巧妙”的变量扩展技巧,因为它应该与任何posix shell一起运行
或者,如果您的目标系统使用dash、zsh、ksh或其他任何东西,您也可以在希望ansible使用的shell中明确说明:
tasks:
- shell: echo "hello from bash"
args:
executable: /bin/bash
谢谢你的帮助,马修·丹尼尔。它工作得很好 最终工作方案将作为参考附上:
- name: Run in local to replace suffix in a folder
hosts: 127.0.0.1
connection: local
vars:
- tmpRulePath: "rules"
- version: "18.06" # change the version here to change the suffix from rules/rules.yml to rules.yml/rules
- validSuffix: "rules.yml"
- invalidSuffix: "rules"
tasks:
- name: Prepare the testing resources
shell: mkdir -p {{ tmpRulePath }}; cd {{ tmpRulePath }}; touch 1.rules 2.rules 3.rules.yml 4.rules.yml; cd -; ls {{ tmpRulePath }};
register: result
- debug:
msg: "{{ result.stdout_lines }}"
- name: Check whether it's old or not
shell: if [ {{ version }} \< '18.06' ]; then echo 'true'; else echo 'false'; fi
register: result
- debug:
msg: "Is {{ version }} less than 18.06 {{ result.stdout }}"
- name: Update validSuffix and invalidSuffix
set_fact:
validSuffix="rules"
invalidSuffix="rules.yml"
when: result.stdout == "true"
- debug:
msg: "validSuffix is {{ validSuffix }} while invalidSuffix {{ invalidSuffix }}"
- name: Replace the invalid suffix with valid
shell: |
cd {{ tmpRulePath }};
for i in *.{{ invalidSuffix }}; do
/bin/mv -v "$i" "`basename "$i" .{{ invalidSuffix }}`.{{ validSuffix }}"
done
- name: Check the latest files
shell: ls {{ tmpRulePath }}
register: result
- debug:
msg: "{{ result.stdout_lines }}"
- name: Clean up
shell: rm -rf {{ tmpRulePath }}
-name:在本地运行以替换文件夹中的后缀
主持人:127.0.0.1
连接:本地
变量:
-tmpRulePath:“规则”
-版本:“18.06”#更改此处的版本,将后缀从rules/rules.yml更改为rules.yml/rules
-validSuffix:“rules.yml”
-invalidSuffix:“规则”
任务:
-名称:准备测试资源
shell:mkdir-p{{tmpRulePath}};cd{{tmpRulePath}};触摸1.rules 2.rules 3.rules.yml 4.rules.yml;镉;ls{{tmpRulePath}};
寄存器:结果
-调试:
msg:“{result.stdout_lines}”
-名称:检查它是否旧
shell:if[{version}\<'18.06'];然后呼应“真”;否则回声“假”;fi
寄存器:结果
-调试:
msg:{{version}是否小于18.06{{result.stdout}”
-名称:更新validSuffix和invalidSuffix
设定事实:
validSuffix=“规则”
invalidSuffix=“rules.yml”
时间:result.stdout==“true”
-调试:
msg:“validSuffix为{{validSuffix}},而invalidSuffix为{{invalidSuffix}}”
-名称:将无效后缀替换为有效后缀
外壳:|
cd{{tmpRulePath}};
对于*{{invalidSuffix}}中的i;做
/bin/mv-v“$i”“`basename“$i”。{{{invalidSuffix}}}.{{validSuffix}}”
完成
-名称:检查最新文件
shell:ls{{tmpRulePath}
寄存器:结果
-调试:
msg:“{result.stdout_lines}”
-名称:清理
shell:rm-rf{{tmpRulePath}
我想你的意思是除了使用shell:
并准确地执行这些步骤之外?@MatthewLDaniel尴尬的是,实际上我不能在ansible中使用shell:(如公司政策禁止您使用,或者您的意思是它在shell:
中没有正确执行?@MatthewLDaniel对Ansible来说非常陌生,格式问题使shell无法像在终端中一样工作。目前我正在考虑其他方法来达到相同的目的,包括直接使用shell。
- name: Run in local to replace suffix in a folder
hosts: 127.0.0.1
connection: local
vars:
- tmpRulePath: "rules"
- version: "18.06" # change the version here to change the suffix from rules/rules.yml to rules.yml/rules
- validSuffix: "rules.yml"
- invalidSuffix: "rules"
tasks:
- name: Prepare the testing resources
shell: mkdir -p {{ tmpRulePath }}; cd {{ tmpRulePath }}; touch 1.rules 2.rules 3.rules.yml 4.rules.yml; cd -; ls {{ tmpRulePath }};
register: result
- debug:
msg: "{{ result.stdout_lines }}"
- name: Check whether it's old or not
shell: if [ {{ version }} \< '18.06' ]; then echo 'true'; else echo 'false'; fi
register: result
- debug:
msg: "Is {{ version }} less than 18.06 {{ result.stdout }}"
- name: Update validSuffix and invalidSuffix
set_fact:
validSuffix="rules"
invalidSuffix="rules.yml"
when: result.stdout == "true"
- debug:
msg: "validSuffix is {{ validSuffix }} while invalidSuffix {{ invalidSuffix }}"
- name: Replace the invalid suffix with valid
shell: |
cd {{ tmpRulePath }};
for i in *.{{ invalidSuffix }}; do
/bin/mv -v "$i" "`basename "$i" .{{ invalidSuffix }}`.{{ validSuffix }}"
done
- name: Check the latest files
shell: ls {{ tmpRulePath }}
register: result
- debug:
msg: "{{ result.stdout_lines }}"
- name: Clean up
shell: rm -rf {{ tmpRulePath }}