在ansible任务中使用寄存器

using register in ansible task

我试图在 cat /etc/fstab 中找到特定行,将其注册到变量中,然后想将其用于 umount 找到的目录。

---
- hosts: all
  remote_user: root

  tasks:
  - name: Finding if the mount point exists
    shell: cat /etc/fstab | grep /mnt | awk '{print }'
    register: mountpoint

  - name: UMOUNT the mountpoint found in expression
    shell: umount "{{ item }}"
    with_items: mountpoint.stdout

我看到第一个任务的输出有效。但是在第二个任务中它说,"stderr": "umount: mountpoint.stdout: mountpoint not found", "stderr_lines": ["umount: mountpoint.stdout: mountpoint not found"]"

寄存器变量不是应该在这里工作吗?我错过了什么吗?

这是输出

{
    "_ansible_parsed": true,
    "stderr_lines": [],
    "cmd": "cat /etc/fstab | grep /mnt | awk '{print }'",
    "end": "2017-09-29 15:07:12.717112",
    "_ansible_no_log": false,
    "stdout": "/mnt/dvd",
    "changed": true,

stdout 在这里被发现为 /mnt/dvd。现在想umount啦。

提前致谢。

我可以自己使用以下代码解决问题。有关 return 值的更多信息,请访问 here

---
  - hosts: all
    remote_user: root

    tasks:
    - name: Finding source source drive
      shell: cat /etc/fstab | grep /dev/sr0 | awk '{ print }'
      register: dest_path
    - name: Finding Destination path for Mount
      shell: cat /etc/fstab | grep /dev/sr0 | awk '{ print }'
      register: src_path

    - name: Mounting the data Drives
      mount:
        path: "{{ dest_path.stdout }}"
        src: "{{ src_path.stdout }}"
        fstype: auto
        opts: ro
        state: unmounted