python - ansible playbook:不可接受的字符#x001b:不允许使用特殊字符
问题描述
我正在尝试运行 ansible-playbook 并得到以下异常:
TASK [deploy aws auth configmap] ***********************************************
task path: /Kubernetes-in-Production-Best-Practices/Chapter04/ansible/tasks/aws-auth.yaml:1
The full traceback is:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/ansible/executor/task_executor.py", line 116, in run
items = self._get_loop_items()
File "/usr/local/lib/python3.9/site-packages/ansible/executor/task_executor.py", line 250, in _get_loop_items
items = templar.template(self._task.loop)
File "/usr/local/lib/python3.9/site-packages/ansible/template/__init__.py", line 822, in template
return [self.template(
File "/usr/local/lib/python3.9/site-packages/ansible/template/__init__.py", line 822, in <listcomp>
return [self.template(
File "/usr/local/lib/python3.9/site-packages/ansible/template/__init__.py", line 789, in template
result = self.do_template(
File "/usr/local/lib/python3.9/site-packages/ansible/template/__init__.py", line 1057, in do_template
res = j2_concat(rf)
File "<template>", line 15, in root
File "/usr/local/lib/python3.9/site-packages/ansible/template/__init__.py", line 257, in wrapper
return list(ret)
File "/usr/local/lib/python3.9/site-packages/yaml/__init__.py", line 127, in load_all
loader = Loader(stream)
File "/usr/local/lib/python3.9/site-packages/yaml/loader.py", line 34, in __init__
Reader.__init__(self, stream)
File "/usr/local/lib/python3.9/site-packages/yaml/reader.py", line 74, in __init__
self.check_printable(stream)
File "/usr/local/lib/python3.9/site-packages/yaml/reader.py", line 143, in check_printable
raise ReaderError(self.name, position, ord(character),
yaml.reader.ReaderError: unacceptable character #x001b: special characters are not allowed
in "<unicode string>", position 281
fatal: [localhost]: FAILED! => {
"msg": "Unexpected failure during module execution.",
"stdout": ""
}
这是返回异常的命令:
ansible-playbook -i \
../../inventories/packtclusters/ \
-e "worker_iam_role_arn=$(terraform output worker_iam_role_arn) \
cluster_name=$(terraform output cluster_full_name) \
aws_default_region=$(terraform output aws_region)" \
../../cluster.yaml
这是发生异常的“aws-auth.yaml”文件:
- name: deploy aws auth configmap
k8s:
definition: "{{ item }}"
kubeconfig: "{{ k8s_kubeconfig }}"
state: "{{ k8s_manifests_state }}"
force: "{{ k8s_force }}"
loop:
- "{{ lookup('template', k8s_manifests_base_dir + 'auth/aws-auth.yaml') | from_yaml_all | list }}"
register: k8s_result
until: k8s_result is success
retries: 3
delay: 2
no_log: "{{ k8s_no_log }}"
这是我安装ansible的步骤:
virtualenv $HOME/Envs/ansible-k8s-workspace
source $HOME/Envs/ansible-k8s-workspace/bin/activate
pip install 'ansible<3.0' openshift pyyaml requests 'kubernetes<12.0'
以下是技术资料:
Operating System: MacOS X
Python 3.9.2
pip 21.0.1 from /Envs/ansible-k8s-workspace/lib/python3.9/site-packages/pip (python 3.9)
ansible 2.10.6
config file = None
configured module search path = ['/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.9.2 (default, Feb 24 2021, 13:26:09) [Clang 12.0.0 (clang-1200.0.32.29)]
此外,这是该命令的完整调试输出:
ansible-playbook -i \
../../inventories/packtclusters/ \
-e "worker_iam_role_arn=$(terraform output worker_iam_role_arn) \
cluster_name=$(terraform output cluster_full_name) \
aws_default_region=$(terraform output aws_region)" \
../../cluster.yaml -vvv
ansible-playbook 2.10.6
config file = None
configured module search path = ['/Users/skliarm/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
executable location = /usr/local/bin/ansible-playbook
python version = 3.9.2 (default, Feb 24 2021, 13:26:09) [Clang 12.0.0 (clang-1200.0.32.29)]
No config file found; using defaults
host_list declined parsing /Users/skliarm/git/kubernetes/Kubernetes-in-Production-Best-Practices/Chapter04/ansible/inventories/packtclusters/hosts as it did not pass its verify_file() method
script declined parsing /Users/skliarm/git/kubernetes/Kubernetes-in-Production-Best-Practices/Chapter04/ansible/inventories/packtclusters/hosts as it did not pass its verify_file() method
auto declined parsing /Users/skliarm/git/kubernetes/Kubernetes-in-Production-Best-Practices/Chapter04/ansible/inventories/packtclusters/hosts as it did not pass its verify_file() method
Parsed /Users/skliarm/git/kubernetes/Kubernetes-in-Production-Best-Practices/Chapter04/ansible/inventories/packtclusters/hosts inventory source with ini plugin
statically imported: /Users/skliarm/git/kubernetes/Kubernetes-in-Production-Best-Practices/Chapter04/ansible/tasks/aws-auth.yaml
redirecting (type: modules) ansible.builtin.k8s to community.kubernetes.k8s
statically imported: /Users/skliarm/git/kubernetes/Kubernetes-in-Production-Best-Practices/Chapter04/ansible/tasks/namespaces.yaml
redirecting (type: modules) ansible.builtin.k8s to community.kubernetes.k8s
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.
PLAYBOOK: cluster.yaml *********************************************************
1 plays in ../../cluster.yaml
PLAY [deploy k8s add-ons] ******************************************************
META: ran handlers
TASK [deploy aws auth configmap] ***********************************************
task path: /Users/skliarm/git/kubernetes/Kubernetes-in-Production-Best-Practices/Chapter04/ansible/tasks/aws-auth.yaml:1
The full traceback is:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/ansible/executor/task_executor.py", line 116, in run
items = self._get_loop_items()
File "/usr/local/lib/python3.9/site-packages/ansible/executor/task_executor.py", line 250, in _get_loop_items
items = templar.template(self._task.loop)
File "/usr/local/lib/python3.9/site-packages/ansible/template/__init__.py", line 822, in template
return [self.template(
File "/usr/local/lib/python3.9/site-packages/ansible/template/__init__.py", line 822, in <listcomp>
return [self.template(
File "/usr/local/lib/python3.9/site-packages/ansible/template/__init__.py", line 789, in template
result = self.do_template(
File "/usr/local/lib/python3.9/site-packages/ansible/template/__init__.py", line 1057, in do_template
res = j2_concat(rf)
File "<template>", line 15, in root
File "/usr/local/lib/python3.9/site-packages/ansible/template/__init__.py", line 257, in wrapper
return list(ret)
File "/usr/local/lib/python3.9/site-packages/yaml/__init__.py", line 127, in load_all
loader = Loader(stream)
File "/usr/local/lib/python3.9/site-packages/yaml/loader.py", line 34, in __init__
Reader.__init__(self, stream)
File "/usr/local/lib/python3.9/site-packages/yaml/reader.py", line 74, in __init__
self.check_printable(stream)
File "/usr/local/lib/python3.9/site-packages/yaml/reader.py", line 143, in check_printable
raise ReaderError(self.name, position, ord(character),
yaml.reader.ReaderError: unacceptable character #x001b: special characters are not allowed
in "<unicode string>", position 281
fatal: [localhost]: FAILED! => {
"msg": "Unexpected failure during module execution.",
"stdout": ""
}
PLAY RECAP *********************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
完整的源代码可以在这里找到:https ://github.com/PacktPublishing/Kubernetes-in-Production-Best-Practices/tree/master/Chapter04
解决方案
我终于找到了问题所在。似乎您不能在 MacOS 命令行中使用 $() 子命令。
如果我替换这个:
ansible-playbook -i \
../../inventories/packtclusters/ \
-e "worker_iam_role_arn=$(terraform output worker_iam_role_arn) \
cluster_name=$(terraform output cluster_full_name) \
aws_default_region=$(terraform output aws_region)" \
../../cluster.yaml -vvv
有了这个:
ansible-playbook -i \
../../inventories/packtclusters/ \
-e "worker_iam_role_arn=test \
cluster_name=test \
aws_default_region=test" \
../../cluster.yaml -vvv
然后它工作。
它实际上抛出了另一个异常,但它是因为测试参数。
PS所以,转义#x001b字符实际上出现在命令行中,在解析$(terraform output cluster_full_name)子命令后,但是ansible调试输出错误地指向了错误的文件,这让我感到困惑并导致我走错了路。
推荐阅读
- javascript - document.getElementById('blocks').value = blocks; 给出错误
- flutter - Flutter 更改 Scaffold 抽屉图标
- c++ - 为什么这个快速排序实现会给出一个奇怪的输出
- hash - 一次调用处理视频文件中的多个流
- java - resteasy 如何从 META-INF/services/javax.ws.rs.ext.Providers 文件中选择它的提供者?
- javascript - 如何在Javascript中获取一年中的一天
- java - 如何将房间数据库中的列表数据添加到android中的警报对话框
- python - 如何检查 OptionMenu 的选项是什么?
- search - 数字报纸图书馆的搜索引擎是如何工作的?
- python - 计算两个笛卡尔坐标的距离