在ansible任务中使用注册

时间:2017-09-29 20:14:20

标签: ansible ansible-2.x

我想在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 $2}'
    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 $2}'",
    "end": "2017-09-29 15:07:12.717112",
    "_ansible_no_log": false,
    "stdout": "/mnt/dvd",
    "changed": true,

stdout在此处可以找到/ mnt / dvd。现在想umount它。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

我能够自己使用以下代码解决方法。有关返回值的更多信息可以从here获得。

---
  - hosts: all
    remote_user: root

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

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