首页 > 解决方案 > 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

标签: pythonpipansiblevirtualenv

解决方案


我终于找到了问题所在。似乎您不能在 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调试输出错误地指向了错误的文件,这让我感到困惑并导致我走错了路。


推荐阅读