MongoDBに格納された情報を使用して初期設定を生成しようとしています。私はAnsibleで動的インベントリ機能を使用しています。バックエンドは単純なmongodbデータベースです。 ansible_fetch_mongodb.py --list
を手動で実行すると、Anabilitiesの要件に従ってJSONでグループとその変数/子が返されます。 --host <hostname>
引数を使用すると、問題のないホストとその変数も返されます。しかし、Ansibleプレイブック内の変数またはitem.var2
にアクセスしようとすると、エラーが発生し、item.hostname
が定義されていないことがわかります。私はそれをすべて実行するためにansible-playbook build_configs.yml -v -i ansible_fetch_mongodb.py
コマンドを使用しています。私はこの問題に関して何時間も働いており、動的ソースから変数にアクセスするための適切な構文の助けを借りて非常に感謝しています。ここMongoDBデータベースでダイナミックインベントリを使用しているホストの変数に、Aniableプレイブックを使用してアクセスするにはどうすればよいですか?
はハンドブックである:ここ
- hosts: localhost
tasks:
- name: configuration generator
template:
src=roles/core_router/templates/3850.j2
dest=/etc/ansible/generated_templates/{{ item }}.txt
with_inventory_hostnames: all
- debug: msg="{{ item.data1_svi_ip }}"
with_inventory_hostnames: all
はMongoDBのに表されたホストである:ここ
{
"_id": "ROUTER123",
"hostname": "ROUTER123",
"vars": {
"data1_svi_ip": "10.19.83.254 255.255.254.0",
"device_num": "01",
"device_type": "Router",
"floor": "04",
"grp_ip": "10.19.93.14 255.255.255.240",
"mgmt_net": "10.19.103.254 255.255.254.0",
"model": "3850",
}
}
はMongoDBの中で示される基である:ここ
{
"_id": "Router",
"children": [],
"hosts": [
"Router123",
],
"name": "Router",
"vars": {}
}
は、エラーであります:
TASK [debug] ****************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible.vars.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'data1_svi_ip'\n\nThe error appears to have been in '/etc/ansible/build_configs.yml': line 8, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n with_inventory_hostnames: all\n - debug: msg=\"{{ item.data1_svi_ip }}\"\n ^here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}
は、これは私が探していたまさに次のようになります。
は、ホスト変数にアクセスするには、使用する必要があります。ありがとうtechraf! –ニース!私はまだそれを知らなかった。どうもありがとう! –