質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.34%
Ansible

Ansibleは、Python で書かれたサーバーの設定を管理するための 構成管理ツールです。

Red Hat Enterprise

Red Hat Enterpriseは、レッドハット社により開発・サポートが行われている業務向けLinuxディストリビューションです。オープンソースで無償で利用することができ、バイナリ版の入手・サポートは有償です。商用ディストリビューションとして人気が高く、代表的なLinuxの選択肢の一つです。

Ansible Tutorial

Ansible Tutorialは、Pythonで記述されたサーバの設定・管理を自動化させるためのツールです。サーバからクライアントへ直接命令し結果を取得するため、各クライアントでAnsible Tutorialをインストールが不要になります。

Q&A

解決済

1回答

135閲覧

Ansible タスクのループ処理でエラー

sunap220

総合スコア23

Ansible

Ansibleは、Python で書かれたサーバーの設定を管理するための 構成管理ツールです。

Red Hat Enterprise

Red Hat Enterpriseは、レッドハット社により開発・サポートが行われている業務向けLinuxディストリビューションです。オープンソースで無償で利用することができ、バイナリ版の入手・サポートは有償です。商用ディストリビューションとして人気が高く、代表的なLinuxの選択肢の一つです。

Ansible Tutorial

Ansible Tutorialは、Pythonで記述されたサーバの設定・管理を自動化させるためのツールです。サーバからクライアントへ直接命令し結果を取得するため、各クライアントでAnsible Tutorialをインストールが不要になります。

0グッド

0クリップ

投稿2024/11/09 16:30

実現したいこと

環境変数の外部ファイルをロードしてタスクをループで処理できるようにしたいです。

環境情報としては以下となります。
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 セクション

補足

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

インベントリの環境変数名の頭文字に'_'を付けてやることで解決しました。
恐らく'groups'という環境変数名はAnsibleで既に予約されている環境変数であったため、環境変数の値が上手く読み込まれなかったと推測。

投稿2024/11/10 13:22

sunap220

総合スコア23

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sunap220

2024/11/11 14:16

情報のご共有ありがとうございます! やはり予約された変数名だったのですね。勉強になりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.34%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問