从两个列表的串联中设置ansible事实

时间:2016-09-15 06:53:59

标签: list concatenation ansible extend ansible-2.x

我希望将两个命令的输出合并为一个新变量,我可以将其用作另一个命令的args:

---
- hosts: '{{target}}'
  tasks:

    - name: determine storage nfs mount points
      shell: /usr/sbin/showmount -d | grep -v Directories
      register: nfs
      ignore_errors: yes

    - debug: var=nfs.stdout_lines

    - name: determine storage xrd mount points
      shell: df | grep /xrd | awk '{print $6}'
      register: xrd

    - debug: var=xrd.stdout_lines

    - name: determine all mount points
      set_fact: mounts="{{ nfs.stdout_lines }} + {{ xrd.stdout_lines }}"

    - name: run gather script
      script: gather.py {{mounts.stdout_lines|join(" ")}} > /tmp/gather.txt
      register: gather

然而,当我运行它时,它出现了:

PLAY [ltda-srv050] *************************************************************

TASK [setup] *******************************************************************
ok: [ltda-srv050]

TASK [determine storage nfs mount points] **************************************
fatal: [ltda-srv050]: FAILED! => {"changed": true, "cmd": "/usr/sbin/showmount -d | grep -v Directories", "delta": "0:00:00.011269", "end": "2016-09-14 23:48:14.489385", "failed": true, "rc": 1, "start": "2016-09-14 23:48:14.478116", "stderr": "clnt_create: RPC: Program not registered", "stdout": "", "stdout_lines": [], "warnings": []}
...ignoring

TASK [debug] *******************************************************************
ok: [ltda-srv050] => {
    "nfs.stdout_lines": []
}

TASK [determine storage xrd mount points] **************************************
changed: [ltda-srv050]

TASK [debug] *******************************************************************
ok: [ltda-srv050] => {
    "xrd.stdout_lines": [
        "/xrd/cache1",
        "/xrd/cache2",
        "/xrd/cache3",
        "/xrd/cache4",
        "/xrd/cache5",
        "/xrd/cache6",
        "/xrd/cache7",
        "/xrd/cache8",
        "/xrd/cache9",
        "/xrd/cache10",
        "/xrd/cache11"
    ]
}

TASK [determine all mount points] **********************************************
ok: [ltda-srv050]

TASK [run gather script] *******************************************************
fatal: [ltda-srv050]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'list object' has no attribute 'stdout_lines'\n\nThe error appears to have been in '/afs/slac.stanford.edu/u/sf/ytl/work/storage/gather_file_attributes/retrieve_file_attributes.yml': line 21, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: run gather script\n      ^ here\n"}

NO MORE HOSTS LEFT *************************************************************
 [WARNING]: Could not create retry file 'retrieve_file_attributes.retry'.         [Errno 2] No such file or directory: ''


PLAY RECAP *********************************************************************
ltda-srv050                : ok=6    changed=1    unreachable=0    failed=1

帮助...?

1 个答案:

答案 0 :(得分:0)

mounts已经是一个列表,因此删除它会尝试在其上调用.stdout_lines失败 - 删除.stdout_lines有效:)