実現したいこと
JSON文字列の一部に変数を使う場合でも、文字列として扱う方法を探しています。
以下の3パターンの動作を試してみます。
YAML
1- debug: 2 msg: "{u'aa': u'{{ 123 }}'}" 3 4- debug: 5 msg: "a{u'aa': u'{{ 123 }}'}" 6 7- debug: 8 msg: "{u'aa': u'123'}"
TASK [ansible-tower : debug] ************************************************************************************ ok: [yyyyyy] => { "msg": { "aa": "123" } } TASK [ansible-tower : debug] ************************************************************************************ ok: [yyyyyy] => { "msg": "a{u'aa': u'123'}" } TASK [ansible-tower : debug] ************************************************************************************ ok: [yyyyyy] => { "msg": "{u'aa': u'123'}" }
Python2形式のJSON文字列で変数を使った場合は、JSONとして解釈されています。
先頭に1文字追加してJSON形式ではなくすと、文字列として解釈されています。
変数を使用しない場合は、JSON形式でも文字列として解釈されています。
発生している問題・エラーメッセージ
具体的には、AnsibleでAnsible Towerの構築をしようとしています。
このとき、ライセンスを登録するために、tower_settingsモジュールのvalueに、Python2形式のJSON文字列を設定しようとしています。
YAML
1- name: ライセンス情報設定の準備 2 set_fact: 3 license: "{{ lookup('file', 'license.txt') }}" 4 5- name: ライセンス情報設定 6 tower_settings: 7 name: LICENSE 8 value: "{u'company_name': u'{{ license.company_name }}', u'hostname': u'{{ license.hostname }}'}"
ところが、値がJSONとして解析された後に設定されるようで、実行エラーとなりました。
TASK [ansible-tower : ライセンス情報設定の準備] ***************************************************************************** ok: [yyyyyy] TASK [ansible-tower : ライセンス情報設定] ******************************************************************************** An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ValueError: Expecting property name: line 1 column 2 (char 1) fatal: [yyyyyy]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n File \"<stdin>\", line 102, in <module>\n File \"<stdin>\", line 94, in _ansiballz_main\n File \"<stdin>\", line 40, in invoke_module\n File \"/usr/lib64/python2.7/runpy.py\", line 176, in run_module\n fname, loader, pkg_name)\n File \"/usr/lib64/python2.7/runpy.py\", line 82, in _run_module_code\n mod_name, mod_fname, mod_loader, pkg_name)\n File \"/usr/lib64/python2.7/runpy.py\", line 72, in _run_code\n exec code in run_globals\n File \"/tmp/ansible_tower_settings_payload_kwlL7n/ansible_tower_settings_payload.zip/ansible/modules/web_infrastructure/ansible_tower/tower_settings.py\", line 104, in <module>\n File \"/tmp/ansible_tower_settings_payload_kwlL7n/ansible_tower_settings_payload.zip/ansible/modules/web_infrastructure/ansible_tower/tower_settings.py\", line 91, in main\n File \"/usr/lib/python2.7/site-packages/tower_cli/resources/setting.py\", line 125, in modify\n data=self.coerce_type(setting, value))\n File \"/usr/lib/python2.7/site-packages/tower_cli/resources/setting.py\", line 150, in coerce_type\n return json.loads(value)\n File \"/usr/lib64/python2.7/json/__init__.py\", line 338, in loads\n return _default_decoder.decode(s)\n File \"/usr/lib64/python2.7/json/decoder.py\", line 366, in decode\n obj, end = self.raw_decode(s, idx=_w(s, 0).end())\n File \"/usr/lib64/python2.7/json/decoder.py\", line 382, in raw_decode\n obj, end = self.scan_once(s, idx)\nValueError: Expecting property name: line 1 column 2 (char 1)\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
value内に変数を入れない({{ }}の無い)場合は、正常に実行できます。
補足情報(FW/ツールのバージョンなど)
環境やバージョンは、以下の通りです。
$ cat /etc/centos-release CentOS Linux release 7.7.1908 (Core) $ ansible --version ansible 2.9.1 config file = /home/ssm-user/.ansible.cfg configured module search path = [u'/home/ssm-user/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, Aug 7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] $ tower-cli --version Tower CLI 3.3.0
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。