Playbookに
to_yaml
または to_nice_yaml
が存在していた時に ansible-lint
をかけるとエラーが発生する事象の解決方法についてメモっておきます。
環境
項目 | バージョン |
---|---|
ansible | 2.9.4 |
ansible-lint | 4.2.0 |
PyYAML | 5.3 |
Python | 3.6.8 |
事象
以下のPlaybookを元にansible-lintを実行してみます。
1 2 3 4 5 6 7 8 9 10 11 12 |
--- - name: example playbook hosts: localhost gather_facts: no vars: hoge: fuga: msg tasks: - name: debug debug: msg: "{{ hoge | to_yaml }}" |
実行すると以下のエラーが発生します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
(venv) [root@7fee8435da07 ~]# ansible-lint example.yml Traceback (most recent call last): File "/root/venv/bin/ansible-lint", line 11, in <module> sys.exit(main()) File "/root/venv/lib64/python3.6/site-packages/ansiblelint/__main__.py", line 187, in main matches.extend(runner.run()) File "/root/venv/lib64/python3.6/site-packages/ansiblelint/__init__.py", line 267, in run for child in ansiblelint.utils.find_children(arg, self.playbook_dir): File "/root/venv/lib64/python3.6/site-packages/ansiblelint/utils.py", line 163, in find_children for child in play_children(basedir, item, playbook[1], playbook_dir): File "/root/venv/lib64/python3.6/site-packages/ansiblelint/utils.py", line 214, in play_children fail_on_undefined=False) File "/root/venv/lib64/python3.6/site-packages/ansiblelint/utils.py", line 182, in template **dict(kwargs, fail_on_undefined=fail_on_undefined)) File "/root/venv/lib64/python3.6/site-packages/ansiblelint/utils.py", line 73, in ansible_template return templar.template(varname, **kwargs) File "/root/venv/lib64/python3.6/site-packages/ansible/template/__init__.py", line 611, in template ) for v in variable] File "/root/venv/lib64/python3.6/site-packages/ansible/template/__init__.py", line 611, in <listcomp> ) for v in variable] File "/root/venv/lib64/python3.6/site-packages/ansible/template/__init__.py", line 623, in template disable_lookups=disable_lookups, File "/root/venv/lib64/python3.6/site-packages/ansible/template/__init__.py", line 623, in template disable_lookups=disable_lookups, File "/root/venv/lib64/python3.6/site-packages/ansible/template/__init__.py", line 578, in template disable_lookups=disable_lookups, File "/root/venv/lib64/python3.6/site-packages/ansible/template/__init__.py", line 837, in do_template res = j2_concat(rf) File "<template>", line 13, in root File "/root/venv/lib64/python3.6/site-packages/ansible/plugins/filter/core.py", line 66, in to_yaml transformed = yaml.dump(a, Dumper=AnsibleDumper, allow_unicode=True, default_flow_style=default_flow_style, **kw) File "/root/venv/lib64/python3.6/site-packages/yaml/__init__.py", line 290, in dump return dump_all([data], stream, Dumper=Dumper, **kwds) File "/root/venv/lib64/python3.6/site-packages/yaml/__init__.py", line 278, in dump_all dumper.represent(data) File "/root/venv/lib64/python3.6/site-packages/yaml/representer.py", line 27, in represent node = self.represent_data(data) File "/root/venv/lib64/python3.6/site-packages/yaml/representer.py", line 58, in represent_data node = self.yaml_representers[None](self, data) File "/root/venv/lib64/python3.6/site-packages/yaml/representer.py", line 231, in represent_undefined raise RepresenterError("cannot represent an object", data) yaml.representer.RepresenterError: ('cannot represent an object', AnsibleUndefined) |
原因はPyYAMLのバージョンにあるようです。
解決方法
PyYAMLのバージョンを 3.13
に下げると解決されます。
1 2 3 |
(venv) [root@7fee8435da07 ~]# pip install --upgrade PyYAML==3.13 (venv) [root@7fee8435da07 ~]# ansible-lint example.yml |