実現したいこと
環境変数の外部ファイルをロードしてタスクをループで処理できるようにしたいです。
環境情報としては以下となります。
OS:RHEL9.2
Ansibleバージョン:2.15.13
ansible
1ansible-config [core 2.15.13] 2 config file = /etc/ansible/ansible.cfg 3 configured module search path = ['/etc/ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] 4 ansible python module location = /home/ansible/.local/lib/python3.9/site-packages/ansible 5 ansible collection location = /etc/ansible/collections:/usr/share/ansible/collections 6 executable location = /home/ansible/.local/bin/ansible-config 7 python version = 3.9.16 (main, Jan 4 2024, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)] (/bin/python) 8 jinja version = 3.1.4 9 libyaml = True
ディレクトリ構成は以下となります。
tree
1. 2├── ansible.cfg 3├── appspec.yml 4├── bat001.yml 5├── bat_servers.yml 6├── inventories 7│ ├── group_vars 8│ │ ├── all.yml 9│ │ ├── bat_servers.yml 10│ │ └── web_servers.yml 11│ ├── host_vars 12│ │ ├── bat001.yml 13│ │ └── web001.yml 14│ └── hosts 15├── roles 16│ └── rhel9.2 17│ └── tasks 18│ ├── directory.yml 19│ ├── groupadd.yml 20│ ├── hostname.yml 21│ ├── main.yml 22│ └── useradd.yml 23├── site.yml 24├── web001.yml 25└── web_servers.yml
site.ymlで各グループのプレイブックをインポートし、
各グループのプレイブックはグループに属するホストのプレイブックをインポートしてます。
site.yml
1--- 2- import_playbook: web_servers.yml 3- import_playbook: bat_servers.yml
web_servers.yml
1--- 2- import_playbook: web001.yml
各ホストのプレイブック内では環境変数の外部ファイルをロードしたり、
実行ロールをインポートさせています。
web001.yml
1--- 2- name: web001_playbook 3 hosts: web001 4 vars_files: 5 - /etc/ansible/inventories/group_vars/all.yml 6 - /etc/ansible/inventories/group_vars/web_servers.yml 7 - /etc/ansible/inventories/host_vars/web001.yml 8 tasks: 9 - import_role: 10 name: rhel9.2
ロール内のタスクは用途毎で分割して、main.ymlでタスクを集約するようにしてます。
タスクは環境変数ファイルで定義したパラメータを元にループ処理させるようにしてます。
発生している問題・分からないこと
ドライラン実行時に以下エラーが発生。
loopで指定した環境変数のパラメータを上手く読み込めてなさそうなエラー。
※シンタックスチェックは問題ないことは確認済み。
エラーメッセージ
error
1$ansible-playbook -i /etc/ansible/inventories/hosts /etc/ansible/site.yml --check -vvvv 2~~~ 3Read vars_file '/etc/ansible/inventories/group_vars/all.yml' 4Read vars_file '/etc/ansible/inventories/group_vars/web_servers.yml' 5Read vars_file '/etc/ansible/inventories/host_vars/web001.yml' 6TASK [rhel9.2 : App Group Add] ********************************************************************************************************************************************************************************* 7task path: /etc/ansible/roles/rhel9.2/tasks/groupadd.yml:8 8fatal: [web001]: FAILED! => { 9 "msg": "Invalid data passed to 'loop', it requires a list, got this instead: {'all': ['web001', 'bat001'], 'ungrouped': [], 'web_servers': ['web001'], 'bat_servers': ['bat001']}. Hint: If you passed a list/dict of just one element, try adding wantlist=True to your lookup invocation or use q/query instead of lookup." 10}
該当のソースコード
inventories/group_vars/web_servers.yml
1server_function: web 2groups: 3 - group_name: online 4 group_gid: 2000 5 - group_name: ope 6 group_gid: 4000 7accounts: 8 - user_name: admin 9 user_password: "{{ os_common_password }}" 10 user_homedir: /home/admin 11 user_uid: 1001 12 user_group: wheel 13 user_shell: /bin/bash 14 - user_name: app01 15 user_password: "{{ os_common_password }}" 16 user_homedir: /home/app01 17 user_uid: 2001 18 user_group: online 19 user_shell: /bin/bash 20 - user_name: user01 21 user_password: "{{ os_common_password }}" 22 user_homedir: /home/user01 23 user_uid: 4001 24 user_group: ope 25 user_shell: /bin/bash 26directories: 27 - directory_path: /home/app01 28 directory_state: directory 29 directory_mode: 0666 30 directory_recurse: no 31 directory_owner: app01 32 directory_group: online 33 - directory_path: /home/app01/online/deploy 34 directory_state: directory 35 directory_mode: 0666 36 directory_recurse: yes 37 directory_owner: app01 38 directory_group: online
roles/rhel9.2/tasks/main.yml
1--- 2- import_tasks: groupadd.yml
roles/rhel9.2/tasks/groupadd.yml
1--- 2- name: App Group Add 3 group: 4 state: present 5 name: "{{ item.group_name }}" 6 gid: "{{ item.group_gid }}" 7 loop: "{{ groups }}"
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
タスク内の'loop'オプションを以下のようなリスト形式すると別のエラーとなりました。
roles/rhel9.2/tasks/groupadd.yml
1--- 2- name: App Group Add 3 group: 4 state: present 5 name: "{{ item.group_name }}" 6 gid: "{{ item.group_gid }}" 7 loop: 8 - "{{ groups }}"
error
1Read vars_file '/etc/ansible/inventories/group_vars/all.yml' 2Read vars_file '/etc/ansible/inventories/group_vars/web_servers.yml' 3Read vars_file '/etc/ansible/inventories/host_vars/web001.yml' 4 5TASK [rhel9.2 : App Group Add] ********************************************************************************************************************************************************************************* 6task path: /etc/ansible/roles/rhel9.2/tasks/groupadd.yml:8 7fatal: [web001]: FAILED! => { 8 "msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'group_name'. 'dict object' has no attribute 'group_name'\n\nThe error appears to be in '/etc/ansible/roles/rhel9.2/tasks/groupadd.yml': line 8, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: App Group Add\n ^ here\n" 9}
'loop'オプションを'with_items'に変更して試したところ、上記と同様のエラーとなりました。
タスクの書き方としては以下の記事を参考にしてます。
特に異なる箇所はないかなと思ってはいるのですが、ぬまり沼っているのでどこか見落としがあるのか。。。
https://zenn.dev/y_mrok/books/ansible-no-tsukaikata/viewer/chapter13
・vars_files セクション
補足
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2024/11/11 00:03
2024/11/11 14:16