2017-05-02 41 views
-2

我想编写一个如果用户存在的剧本改变它的pw。 该剧本应该能够取得n个用户并改变这些用户的权重。Ansible中的循环使用寄存器

目前我有问题,因为循环,当我是空的,我尝试使用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. 

导致错误!

duplicate loop in task: items 

WITHOUT with_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": [] 
     }, 

所以与其做两个循环(这将是

- 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 }}" 

注:在我的测试外壳:用with_nested和两个列表),你可以做一个单循环完成这一切的“grep -q {{item.key}}/etc/passwd文件&>的/ dev/null“总是返回0返回码。我不得不删除“&>/dev/null”部分以获取正确的返回码。

+0

哇它的工作:)你能解释一点点吗?我使用>/dev/null获得了不同的rc,但生病再次检查。我的代码现在看起来像检查用户是否存在+ - name:“更新给定用户的密码” user:“name = {{item.item .key}} update_password = always password = {{item.item.value.passwd}} createhome = no“ when:item.rc == 0 #with_dict:”{{users}}“ with_items:”{{ user_exists.results}}“ – Sandro

+0

嗨@Sandro,我不确定你想让我解释更多的部分。 –