2017-05-02 11 views
-2

ユーザが存在する場合は、そのプレイヤーのpwを変更するプレイブックを作成します。 プレイブックはユーザーを受け取り、そのユーザーのpwを変更することができます。ループでレジスタを使用する場合

今のところimがループのために空であるという問題を抱えていますが、私はwith_items:{{user_exists.results}}を使ってみましたが、これは何とかうまくいきません。 (http://docs.ansible.com/ansible/playbooks_loops.html#using-register-with-a-loop

何か問題がありますか?

Brで、 Numblesix

--- 
- 
    become: true 
    become_method: sudo 
    hosts: xetest 
    name: "Updates the password of given User if exists" 
    tasks: 
    - 
     ignore_errors: true 
     name: "Check if User exists" 
     register: user_exists 
     shell: "grep -q {{ item.key }} /etc/passwd &>/dev/null" 
     with_dict: "{{ users }}" 
    - 
     debug: 
     var: user_exists 
    - 
     debug: 
     msg: "User name is {{ item.key }} and hash is {{ item.value.passwd}} and return code is: " 
     with_dict: "{{ users }}" 
    - 
     debug: 
     var: user_exists 
     with_items: "{{user_exists.results }}" 
    - 
     name: "updating password for given User" 
     user: "name={{ item.key }} update_password=always password={{ item.value.passwd}} createhome=no" 
     when: user_exists.rc == 0 
     with_dict: "{{ users }}" 
     with_items: "{{ user_exists.results }}" 
    vars: 
    users: 
     foo: 
     passwd: $6$random_salt$12A.ar9eNDsgmds3leKoCDZPmq7OHLvhBtQg/Q3K2G/3yeEa/r8Ou4DxJpN6vzccewugvZt7IkfCbHFF2i.QU. 

エラーになります! with_items WITHOUT

duplicate loop in task: items 

: "{{user_exists.results}}" イムは、このエラーに私のテストでは

"failed": true, "msg": "The conditional check 'user_exists.rc == 0' failed. 
The error was: error while evaluating conditional (user_exists.rc == 0): 
'dict object' has no attribute 'rc' 

答えて

0

を取得、私はansible 2.1.4.0を使用しています。

スクリプトを実行している場合、あなたはそれが入力されたリターンコードと一緒に渡された値が含まれていuser_exists.resultsのデバッグに見ることができますので、代わりにされているだろう二つのループを(やっ

"results": [ 
     { 
      "_ansible_item_result": true, 
      "_ansible_no_log": false, 
      "_ansible_parsed": true, 
      "changed": true, 
      "cmd": "grep -q foo /etc/passwd", 
      "delta": "0:00:00.009034", 
      "end": "2017-05-02 17:42:57.835871", 
      "failed": true, 
      "invocation": { 
       "module_args": { 
        "_raw_params": "grep -q foo /etc/passwd", 
        "_uses_shell": true, 
        "chdir": null, 
        "creates": null, 
        "executable": null, 
        "removes": null, 
        "warn": true 
       }, 
       "module_name": "command" 
      }, 
      "item": { 
       "key": "foo", 
       "value": { 
        "passwd": "foobar" 
       } 
      }, 
      "rc": 1, 
      "start": "2017-05-02 17:42:57.826837", 
      "stderr": "", 
      "stdout": "", 
      "stdout_lines": [], 
      "warnings": [] 
     }, 

を)with_nested 2つのリストで行わ、あなたは、単一のループですべてを行うことができます。

- name: "updating password for given User" 
    debug: 
    msg: "name={{ item.item.key }} update_password=always password={{ item.item.value.passwd}} createhome=no" 
    when: item.rc == 0 
    with_items: "{{ user_exists.results }}" 

注:私のテストシェルで:「grepの-q {{item.key}}/etc/passwdファイル&>は/ dev/null "は常に0の戻りコードを返していました。適切なリターンコードを取得するには、 "&>/dev/null"部分を削除しなければなりませんでした。

+0

うわー、それは働いています:)もっと少し説明できますか?私は>&dev/nullと違ったrcを取得しますが、もう一度チェックしてください。ユーザーコードが存在するかどうかチェックするようになりました+ - 名前: "指定されたユーザーのパスワードを更新しています" user: "name = {{item.item {{ユーザー}} " with_items:" {{ユーザー}} {パスワード} = {パスワード} {パスワード} user_exists.results}} " – Sandro

+0

こんにちは@Sandro、私はあなたが私にもっと説明してほしい部分が不明です。 –

関連する問題