gpt4 book ai didi

bash - 错误!拆分参数失败,要么是不平衡的 jinja2 block ,要么是引用 :

转载 作者:行者123 更新时间:2023-12-03 23:55:57 25 4
gpt4 key购买 nike

我有一个在 bash shell 中工作的脚本。我把它放在 Ansible 中,它导致剧本出现异常。

- name: add mnt to ignore_filesystems
shell: |
grep ignore_filesystem {{ cdm_path }} | awk -F'=' '{print $2}' | egrep '\^\\/mnt\*'
if [ $? -eq 0 ]; then
echo 'do nothing';
else
echo 'no';
items=$(grep ignore_filesystem {{ cdm_path }} | awk -F'=' '{print $2}')
if [ ${#items} -gt 1 ]; then
echo 'making the change'
sed -ie '/ignore_filesystem/ s~$~|^\\/mnt*~' {{ cdm_path }}
else
echo 'making the change'
sed -ie '/ignore_filesystem/ s~ignore_filesystem = *~ignore_filesystem = ^\\/mnt*~' {{ cdm_path }}
fi
fi
register: ignore_filesystems
changed_when: '"making the change" in ignore_filesystems.stdout'

尝试执行剧本时,我收到以下消息:
ERROR! failed at splitting arguments, either an unbalanced jinja2 block or quotes: grep ignore_filesystem {{ cdm_path }} | awk -F'=' '{print $2}' | egrep '\^\\/mnt\*'
if [ $? -eq 0 ]; then
echo 'do nothing';
else
echo 'no';
items=$(grep ignore_filesystem {{ cdm_path }} | awk -F'=' '{print $2}')
if [ ${#items} -gt 1 ]; then
echo 'making the change'
sed -ie '/ignore_filesystem/ s~$~|^\\/mnt*~' {{ cdm_path }}
else
echo 'making the change'
sed -ie '/ignore_filesystem/ s~ignore_filesystem = *~ignore_filesystem = ^\\/mnt*~' {{ cdm_path }}
fi
fi

The error appears to have been in '/home/username/provisioning/provisioning/edit_nimbus.yml':
line 179, column 5, but may be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- name: add mnt to ignore_filesystems
^ here

关于为什么这可能会失败的任何想法?

================================================== ========================

更新:

试过 > 以及 |并使所有行具有相同的缩进级别。还是一样的错误
  - name: add mnt to ignore_filesystems
shell: |
grep 'ignore_filesystem' {{ cdm_path }}| awk -F'=' '{print $2}' | egrep '\^\\/mnt\*';
if [ $? -eq 0 ]; then
echo 'do nothing';
else
echo 'no';
items=$(grep ignore_filesystem {{ cdm_path }} | awk -F'=' '{print $2}');
if [ ${#items} -gt 1 ]; then
echo 'making the change';
sed -ie '/ignore_filesystem/ s~$~|^\\/mnt*~' {{ cdm_path }};
else
echo 'making the change';
sed -ie '/ignore_filesystem/ s~ignore_filesystem = *~ignore_filesystem = ^\\/mnt*~' {{ cdm_path }};
fi;
fi
register: ignore_filesystems
changed_when: '"making the change" in ignore_filesystems.stdout'

最佳答案

所以结果证明这不是缩进的问题,而是 ${#items} 的解释方式。我能够使用以下代码来解决这个问题:

- name: add mnt to ignore_filesystems
shell: |
grep 'ignore_filesystem' {{ cdm_path }} | awk -F'=' '{print $2}' | egrep '\^\\/mnt\*';
if [ $? -eq 0 ]; then
echo 'do nothing';
else
echo 'no';
items=$(sudo grep ignore_filesystem {{ cdm_path }} | awk -F'=' '{print $2}');
len=$(echo -n $items | wc -m)
if [ ${len} -gt 1 ]; then
echo 'making the change';
echo 'appending...';
sed -ie '/ignore_filesystem/ s~$~|^\\/mnt*~' {{ cdm_path }};
else
echo 'making the change';
echo 'replacing';
sudo sed -ie '/ignore_filesystem/ s~ignore_filesystem = *~ignore_filesystem = ^\\/mnt*~' {{ cdm_path }};
fi
fi
register: ignore_filesystems
changed_when: '"making the change" in ignore_filesystems.stdout'

关于bash - 错误!拆分参数失败,要么是不平衡的 jinja2 block ,要么是引用 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47340255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com