いつも助けて頂いてありがとうございます。下記を実現する方法がもしあれば教えて頂けると助かります。自身も本当に出来るのかわからず探りながらやってます(´;ω;`)
実現したいこと
・パラメータ情報が記載された外部ファイル(json)を読み込み、jinja2テンプレートに入れた(入れて加工したいのですが、加工の内容はまだ決めてません)後
後続のタスクでパラメータを個別に指定して呼び出し使用したい。
・上記を一枚のPlaybookで完結させたい。
・"{{ 変数名.パラメータ値 }}"という形で呼び出したいけど、他の形でも方法があるのか知りたい。
該当のソースコード
test.yml
- hosts: fortigates collections: - fortinet.fortios connection: httpapi vars: ansible_httpapi_use_ssl: yes ansible_httpapi_validate_certs: no ansible_httpapi_port: 443 test1: "{{ lookup('file', '/srv/data/parameter.json') }}" test2: "{{ lookup('template', '/srv/data/parameter.j2', template_vars=test1) }}" tasks: - name: set_factest set_fact: parameter: "{{ test2 }}" - name: debug debug: msg: "{{ parameter }}" - name: system_global fortios_system_global: system_global: language: "{{ parameter.language }}" ------------------------------------------------------------------------------------------------------- ■parameter.json { "timezone": "60", "hostname": "FGT60FTK21035539", "language": "japanese" } ■parameter.j2 { "timezone": "{{ timezone }}", "hostname": "{{ hostname }}", "language": "{{ language }}" } ------------------------------------------------------------------------------------------------------- ■実行結果&エラーメッセージ [root@localhost template]# ansible-playbook -i hosts test.yml -vvvv ansible-playbook [core 2.11.9] config file = /etc/ansible/ansible.cfg configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/local/lib/python3.6/site-packages/ansible ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections executable location = /usr/local/bin/ansible-playbook python version = 3.6.8 (default, Nov 16 2020, 16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] jinja version = 3.0.3 libyaml = True Using /etc/ansible/ansible.cfg as config file setting up inventory plugins host_list declined parsing /etc/ansible/fortigate/test/template/hosts as it did not pass its verify_file() method auto declined parsing /etc/ansible/fortigate/test/template/hosts as it did not pass its verify_file() method Parsed /etc/ansible/fortigate/test/template/hosts inventory source with ini plugin Loading collection fortinet.fortios from /root/.ansible/collections/ansible_collections/fortinet/fortios redirecting (type: callback) ansible.builtin.counter_enabled to community.general.counter_enabled Loading collection community.general from /root/.ansible/collections/ansible_collections/community/general redirecting (type: callback) ansible.builtin.counter_enabled to community.general.counter_enabled Loading callback plugin community.general.counter_enabled of type stdout, v2.0 from /root/.ansible/collections/ansible_collections/community/general/plugins/callback/counter_enabled.py 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. 1 plays in test.yml PLAY [fortigates] ************************************************************************ TASK 1/3 [Gathering Facts] *************************************************************** task path: /etc/ansible/fortigate/test/template/test.yml:1 redirecting (type: connection) ansible.builtin.httpapi to ansible.netcommon.httpapi Loading collection ansible.netcommon from /root/.ansible/collections/ansible_collections/ansible/netcommon <ansible_host=192.168.1.1> attempting to start connection <ansible_host=192.168.1.1> using connection plugin ansible.netcommon.httpapi Found ansible-connection at path /usr/local/bin/ansible-connection <ansible_host=192.168.1.1> local domain socket does not exist, starting it <ansible_host=192.168.1.1> control socket path is /root/.ansible/pc/be701deeba <ansible_host=192.168.1.1> redirecting (type: connection) ansible.builtin.httpapi to ansible.netcommon.httpapi <ansible_host=192.168.1.1> Loading collection ansible.netcommon from /root/.ansible/collections/ansible_collections/ansible/netcommon <ansible_host=192.168.1.1> local domain socket listeners started successfully <ansible_host=192.168.1.1> <ansible_host=192.168.1.1> local domain socket path is /root/.ansible/pc/be701deeba <ansible_host=192.168.1.1> ESTABLISH LOCAL CONNECTION FOR USER: root <ansible_host=192.168.1.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-local-22733w074xrrv `"&& mkdir "` echo /root/.ansible/tmp/ansible-local-22733w074xrrv/ansible-tmp-1661929267.8036003-22747-140663278401999 `" && echo ansible-tmp-1661929267.8036003-22747-140663278401999="` echo /root/.ansible/tmp/ansible-local-22733w074xrrv/ansible-tmp-1661929267.8036003-22747-140663278401999 `" ) && sleep 0' Using module file /usr/local/lib/python3.6/site-packages/ansible/modules/setup.py <ansible_host=192.168.1.1> PUT /root/.ansible/tmp/ansible-local-22733w074xrrv/tmpk6k_0n83 TO /root/.ansible/tmp/ansible-local-22733w074xrrv/ansible-tmp-1661929267.8036003-22747-140663278401999/AnsiballZ_setup.py <ansible_host=192.168.1.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-local-22733w074xrrv/ansible-tmp-1661929267.8036003-22747-140663278401999/ /root/.ansible/tmp/ansible-local-22733w074xrrv/ansible-tmp-1661929267.8036003-22747-140663278401999/AnsiballZ_setup.py && sleep 0' <ansible_host=192.168.1.1> EXEC /bin/sh -c '/usr/bin/python3 /root/.ansible/tmp/ansible-local-22733w074xrrv/ansible-tmp-1661929267.8036003-22747-140663278401999/AnsiballZ_setup.py && sleep 0' <ansible_host=192.168.1.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-local-22733w074xrrv/ansible-tmp-1661929267.8036003-22747-140663278401999/ > /dev/null 2>&1 && sleep 0' ok: 1/1 [ansible_host=192.168.1.1] META: ran handlers TASK 2/3 [set_factest] ******************************************************************* task path: /etc/ansible/fortigate/test/template/test.yml:12 File lookup using /srv/data/parameter.json as file File lookup using /srv/data/parameter.j2 as file redirecting (type: connection) ansible.builtin.httpapi to ansible.netcommon.httpapi Loading collection ansible.netcommon from /root/.ansible/collections/ansible_collections/ansible/netcommon <ansible_host=192.168.1.1> attempting to start connection <ansible_host=192.168.1.1> using connection plugin ansible.netcommon.httpapi Found ansible-connection at path /usr/local/bin/ansible-connection <ansible_host=192.168.1.1> found existing local domain socket, using it! <ansible_host=192.168.1.1> updating play_context for connection <ansible_host=192.168.1.1> <ansible_host=192.168.1.1> local domain socket path is /root/.ansible/pc/be701deeba ok: 1/1 [ansible_host=192.168.1.1] => { "ansible_facts": { "parameter": { "hostname": "FGT60FTK21035539", "language": "japanese", "timezone": "60" } }, "changed": false } TASK 3/3 [debug] ************************************************************************* task path: /etc/ansible/fortigate/test/template/test.yml:15 redirecting (type: connection) ansible.builtin.httpapi to ansible.netcommon.httpapi Loading collection ansible.netcommon from /root/.ansible/collections/ansible_collections/ansible/netcommon <ansible_host=192.168.1.1> attempting to start connection <ansible_host=192.168.1.1> using connection plugin ansible.netcommon.httpapi Found ansible-connection at path /usr/local/bin/ansible-connection <ansible_host=192.168.1.1> found existing local domain socket, using it! <ansible_host=192.168.1.1> updating play_context for connection <ansible_host=192.168.1.1> <ansible_host=192.168.1.1> local domain socket path is /root/.ansible/pc/be701deeba ok: 1/1 [ansible_host=192.168.1.1] => { "msg": { "hostname": "FGT60FTK21035539", "language": "japanese", "timezone": "60" } } TASK 4/3 [system_global] ***************************************************************** task path: /etc/ansible/fortigate/test/template/test.yml:18 fatal: 1/1 [ansible_host=192.168.1.1]: FAILED! => { "msg": "The task includes an option with an undefined variable. The error was: 'language' is undefined\n\nThe error appears to be in '/etc/ansible/fortigate/test/template/test.yml': line 18, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n msg: \"{{ parameter }}\"\n - name: system_global\n ^ here\n" } PLAY RECAP ******************************************************************************* ansible_host=192.168.1.1 : ok=3 changed=0 unreachable=0 failed=1 rescued=0 ignored=0
0 コメント