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

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

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

CentOSは、主にRed Hat Enterprise Linux(RHEL)をベースにした、フリーのソフトウェアオペレーティングシステムです。

VirtualBox

VirtualBoxは、現在米オラクル社が開発している、 x86仮想化ソフトウェア・パッケージの一つです。

Vagrant

Vagrantは、VirtualBox上の仮想マシンを コマンドラインから作成してくれるソフトウェアです。 ビルド環境など容易に構築が可能です。

Ansible

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

Q&A

解決済

1回答

10181閲覧

ansible環境変数参照時にエラー

shigel

総合スコア11

CentOS

CentOSは、主にRed Hat Enterprise Linux(RHEL)をベースにした、フリーのソフトウェアオペレーティングシステムです。

VirtualBox

VirtualBoxは、現在米オラクル社が開発している、 x86仮想化ソフトウェア・パッケージの一つです。

Vagrant

Vagrantは、VirtualBox上の仮想マシンを コマンドラインから作成してくれるソフトウェアです。 ビルド環境など容易に構築が可能です。

Ansible

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

0グッド

1クリップ

投稿2016/11/16 00:58

編集2016/11/16 03:36

###前提・実現したいこと

Ansibleで環境変数を参照してコマンドを実行したいのですが、 group_vars/all で宣言している変数参照時にエラーが出てしまいます。
書式などに初歩的なミスがありますでしょうか?
ご協力のほど、よろしくお願いいたします。

###発生している問題・エラーメッセージ

the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'unicode object' has no attribute 'gid' The error appears to have been in '/vagrant/ansible/roles/test/tasks/main.yml': line 7, column 3, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: - name: create groups debug ^ here
[root@localhost ansible]# ansible-playbook test.yml --limit=vagrant --connection=local -vvvv Using /vagrant/ansible/ansible.cfg as config file Loading callback plugin default of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc PLAYBOOK: test.yml ************************************************************* 1 plays in test.yml PLAY [all] ********************************************************************* TASK [setup] ******************************************************************* Using module file /usr/lib/python2.7/site-packages/ansible/modules/core/system/setup.py <127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root <127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1479267155.59-84022657785728 `" && echo ansible-tmp-1479267155.59-84022657785728="` echo $HOME/.ansible/tmp/ansible-tmp-1479267155.59-84022657785728 `" ) && sleep 0' <127.0.0.1> PUT /tmp/tmpKJgvMU TO /root/.ansible/tmp/ansible-tmp-1479267155.59-84022657785728/setup.py <127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1479267155.59-84022657785728/ /root/.ansible/tmp/ansible-tmp-1479267155.59-84022657785728/setup.py && sleep 0' <127.0.0.1> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1479267155.59-84022657785728/setup.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1479267155.59-84022657785728/" > /dev/null 2>&1 && sleep 0' ok: [127.0.0.1] TASK [test : create groups debug] ********************************************** task path: /vagrant/ansible/roles/test/tasks/main.yml:2 ok: [127.0.0.1] => (item=user_groups) => { "invocation": { "module_args": { "var": "user_groups" }, "module_name": "debug" }, "item": "user_groups", "user_groups": [ { "gid": 1111, "name": "hoge" }, { "gid": 1112, "name": "foo" }, { "gid": 1113, "name": "bar" } ] } TASK [test : create groups debug] ********************************************** task path: /vagrant/ansible/roles/test/tasks/main.yml:7 fatal: [127.0.0.1]: FAILED! => { "failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'unicode object' has no attribute 'gid'\n\nThe error appears to have been in '/vagrant/ansible/roles/test/tasks/main.yml': line 7, 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: create groups debug\n ^ here\n" } to retry, use: --limit @/vagrant/ansible/test.retry PLAY RECAP ********************************************************************* 127.0.0.1 : ok=2 changed=0 unreachable=0 failed=1 [root@localhost ansible]#

###該当のソースコード

  • 構成

bash

1$ tree 2. 3├── ansible.cfg 4├── group_vars/ 5│   └── all 6├── hosts/ 7├── host_vars/ 8├── roles/ 9│   └── test/ 10│   ├── defaults/ 11│   ├── files/ 12│   ├── handlers/ 13│   ├── meta/ 14│   ├── tasks/ 15│   │   └── main.yml 16│   ├── templates/ 17│   └── vars/ 18└── test.yml 19$ cat ansible.cfg 20[defaults] 21hostfile = ./hosts 22#private_key_file = ~/.vagrant.d/insecure_private_key 23host_key_checking = False 24deprecation_warnings=False 25$ cat group_vars/all 26user_groups: 27 - { name: 'hoge', gid: 1111 } 28 - { name: 'foo', gid: 1112 } 29 - { name: 'bar', gid: 1113 } 30$ cat hosts 31[vagrant] 32127.0.0.1 ansible_connection=local 33$ cat roles/test/tasks/main.yml 34--- 35- name: create groups debug 36 debug: var={{ item }} 37 with_items: user_groups 38 tags: user 39 40- name: create groups debug 41 debug: var={{ item.gid }} 42 with_items: user_groups 43 tags: user 44 45$ cat test.yml 46--- 47- hosts: all 48 remote_user: root 49 roles: 50 - test 51 52$

###試したこと

以前、ほぼこれと同様のコードで上手く動作しておりましたが、いつの間にか動作しなくなっておりました。
Linux OS上で yum update をおこなっているので、以前と比較するとpythonやansibleのバージョンは上がっております。

変数書式を {{ item.gid }} から {{ item.['gid'] }} に変更しましたが、こちらも同じエラーが出てしまいました。

###補足情報(言語/FW/ツール等のバージョンなど)

  • Mac OSX
    • Vagrant 1.8.1
    • VirtualBox 5.0.26
      • CentOS Linux release 7.2.1511 (Core)
        • Python 2.7.5
        • ansible 2.2.0.0

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

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

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

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

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

nagaetty

2016/11/16 03:29

デバッグのために-vvvvオプションをつけて実行してみてはいかがでしょうか?
shigel

2016/11/16 03:38 編集

`-vvvv` に変更してエラーメッセージを修正しました。これと言って気になるメッセージが今のところ見当たらないです。
guest

回答1

0

ベストアンサー

with_itemsに変数名をそのまま記載するのが少し前から非推奨になって、version2.2でより厳しくなったんだと思います。

以下、2.1系で似たようなPlaybookを用意したときのwarningです。
2.2になった時点で、これが削除されていると見るのがいいかと。

[DEPRECATION WARNING]: Using bare variables is deprecated. Update your playbooks so that the environment value uses the full variable syntax ('{{user_groups}}').
This feature will be removed in a
future release. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

解決策としては、WARNINGの指示に従い、'{{ 変数名 }}'の書式に変えるのが一番だと思います。

yaml

1- name: create groups debug 2 debug: var={{ item }} 3 with_items: '{{ user_groups }}' 4 tags: user 5 6- name: create groups debug 7 debug: var={{ item.gid }} 8 with_items: '{{ user_groups }}' 9 tags: user

投稿2016/11/16 04:18

attakei

総合スコア2738

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

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

shigel

2016/11/16 04:46 編集

attakkeiさん、回答ありがとうございます! 下記コードで無事通りました。 ansible 2.1でWarning、ansible 2.2でエラーになったんですね。 たまたま `ignore_errors: True` にしてたのでWarningをスルーしてました。 助かりました。 - 修正後 ```bash # cat roles/test/tasks/main.yml --- - name: create groups debug 1 debug: var={{ item }} with_items: - '{{ user_groups }}' tags: user - name: create groups debug 2 debug: var={{ item.gid }} with_items: - '{{ user_groups }}' tags: user - name: create groups debug 3 debug: var={{ item['gid'] }} with_items: - '{{ user_groups }}' tags: user ``` - 実行結果 ``` [root@localhost ansible.2]# ansible-playbook test.yml --limit=vagrant --connection=local -vvvv Using /vagrant/ansible.2/ansible.cfg as config file Loading callback plugin default of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc PLAYBOOK: test.yml ************************************************************* 1 plays in test.yml PLAY [all] ********************************************************************* TASK [setup] ******************************************************************* Using module file /usr/lib/python2.7/site-packages/ansible/modules/core/system/setup.py <127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root <127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1479270996.91-45035124739952 `" && echo ansible-tmp-1479270996.91-45035124739952="` echo $HOME/.ansible/tmp/ansible-tmp-1479270996.91-45035124739952 `" ) && sleep 0' <127.0.0.1> PUT /tmp/tmp24Vhdx TO /root/.ansible/tmp/ansible-tmp-1479270996.91-45035124739952/setup.py <127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1479270996.91-45035124739952/ /root/.ansible/tmp/ansible-tmp-1479270996.91-45035124739952/setup.py && sleep 0' <127.0.0.1> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1479270996.91-45035124739952/setup.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1479270996.91-45035124739952/" > /dev/null 2>&1 && sleep 0' ok: [127.0.0.1] TASK [test : create groups debug 1] ******************************************** task path: /vagrant/ansible.2/roles/test/tasks/main.yml:2 ok: [127.0.0.1] => (item={u'gid': 1111, u'name': u'hoge'}) => { "<type 'dict'>": "VARIABLE IS NOT DEFINED!", "invocation": { "module_args": { "var": { "gid": 1111, "name": "hoge" } }, "module_name": "debug" }, "item": { "gid": 1111, "name": "hoge" } } ok: [127.0.0.1] => (item={u'gid': 1112, u'name': u'foo'}) => { "<type 'dict'>": "VARIABLE IS NOT DEFINED!", "invocation": { "module_args": { "var": { "gid": 1112, "name": "foo" } }, "module_name": "debug" }, "item": { "gid": 1112, "name": "foo" } } ok: [127.0.0.1] => (item={u'gid': 1113, u'name': u'bar'}) => { "<type 'dict'>": "VARIABLE IS NOT DEFINED!", "invocation": { "module_args": { "var": { "gid": 1113, "name": "bar" } }, "module_name": "debug" }, "item": { "gid": 1113, "name": "bar" } } TASK [test : create groups debug 2] ******************************************** task path: /vagrant/ansible.2/roles/test/tasks/main.yml:8 ok: [127.0.0.1] => (item={u'gid': 1111, u'name': u'hoge'}) => { "1111": "1111", "invocation": { "module_args": { "var": "1111" }, "module_name": "debug" }, "item": { "gid": 1111, "name": "hoge" } } ok: [127.0.0.1] => (item={u'gid': 1112, u'name': u'foo'}) => { "1112": "1112", "invocation": { "module_args": { "var": "1112" }, "module_name": "debug" }, "item": { "gid": 1112, "name": "foo" } } ok: [127.0.0.1] => (item={u'gid': 1113, u'name': u'bar'}) => { "1113": "1113", "invocation": { "module_args": { "var": "1113" }, "module_name": "debug" }, "item": { "gid": 1113, "name": "bar" } } TASK [test : create groups debug 3] ******************************************** task path: /vagrant/ansible.2/roles/test/tasks/main.yml:14 ok: [127.0.0.1] => (item={u'gid': 1111, u'name': u'hoge'}) => { "1111": "1111", "invocation": { "module_args": { "var": "1111" }, "module_name": "debug" }, "item": { "gid": 1111, "name": "hoge" } } ok: [127.0.0.1] => (item={u'gid': 1112, u'name': u'foo'}) => { "1112": "1112", "invocation": { "module_args": { "var": "1112" }, "module_name": "debug" }, "item": { "gid": 1112, "name": "foo" } } ok: [127.0.0.1] => (item={u'gid': 1113, u'name': u'bar'}) => { "1113": "1113", "invocation": { "module_args": { "var": "1113" }, "module_name": "debug" }, "item": { "gid": 1113, "name": "bar" } } PLAY RECAP ********************************************************************* 127.0.0.1 : ok=4 changed=0 unreachable=0 failed=0 ```
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問