Ansible playbook 在尝试运行补丁时不起作用

Dun*_*ock 2 vagrant ansible

我正在尝试使用 Ansible 来配置 Vagrant VM。VM 正在运行 CentOS 6.4。我正在使用以下(缩写)ansible playbook:

- hosts: default
  vars:
    home: '/home/vagrant'
    curl_version: '7_19_7'
    curl_url: 'https://github.com/bagder/curl/archive/curl-{{ curl_version }}.tar.gz'
    curl_dir: '{{ home }}/curl-curl-{{ curl_version }}'

  # user: vagrant
  remote_user: vagrant
  sudo: yes

  tasks:

  - name: Ensure required packages and installed and up to date - pt1
    yum: pkg={{ item }} state=present
    with_items:
      - make
      - gcc
      - etc...

  # Lots more yum tasks in here

  - name: Ensure CURL source downloaded
    get_url: url={{ curl_url }} dest=/home/vagrant/curl-{{ curl_version }}.tar

  - name: Extract CURL source
    command: tar -zxf {{ home }}/curl-{{ curl_version }}.tar creates={{ curl_dir }}

  - name: Copy ssh patch over
    copy: src=./files/ssh.c.patch dest={{ home }}/ssh.c.patch

  - name: Patch CURL with openssl
    command: '"{{ item }}" chdir={{ curl_dir }}/lib'
    with_items:
      - patch {{ curl_dir }}/lib/ssh.c {{ home }}/ssh.c.patch
Run Code Online (Sandbox Code Playgroud)

Vagrangt 工作正常,Ansible playbook 成功运行到最后一个任务 'Patch CURL with openssl' - 失败,如下所示:

TASK: [Patch CURL with openssl] *********************************************** 
failed: [default] => (item=patch < /home/vagrant/ssh.c.patch) => {"cmd": ["patch < /home/vagrant/ssh.c.patch"], "failed": true, "item": "patch < /home/vagrant/ssh.c.patch", "rc": 2}
msg: [Errno 2] No such file or directory

FATAL: all hosts have already failed -- aborting
Run Code Online (Sandbox Code Playgroud)

我已经验证到那时的所有任务都可以工作,并且文件被下载并解压缩到预期的位置。

任务失败后,如果您通过 SSH 连接到正在配置的 VM,并自己运行相同的事情 - 使用剧本变量中的确切值,它会起作用:

- hosts: default
  vars:
    home: '/home/vagrant'
    curl_version: '7_19_7'
    curl_url: 'https://github.com/bagder/curl/archive/curl-{{ curl_version }}.tar.gz'
    curl_dir: '{{ home }}/curl-curl-{{ curl_version }}'

  # user: vagrant
  remote_user: vagrant
  sudo: yes

  tasks:

  - name: Ensure required packages and installed and up to date - pt1
    yum: pkg={{ item }} state=present
    with_items:
      - make
      - gcc
      - etc...

  # Lots more yum tasks in here

  - name: Ensure CURL source downloaded
    get_url: url={{ curl_url }} dest=/home/vagrant/curl-{{ curl_version }}.tar

  - name: Extract CURL source
    command: tar -zxf {{ home }}/curl-{{ curl_version }}.tar creates={{ curl_dir }}

  - name: Copy ssh patch over
    copy: src=./files/ssh.c.patch dest={{ home }}/ssh.c.patch

  - name: Patch CURL with openssl
    command: '"{{ item }}" chdir={{ curl_dir }}/lib'
    with_items:
      - patch {{ curl_dir }}/lib/ssh.c {{ home }}/ssh.c.patch
Run Code Online (Sandbox Code Playgroud)

我是 Ansible 的新手,我不确定为什么这不起作用 - 看起来应该是这样?我究竟做错了什么?

小智 6

看起来您在“命令”调用中使用了 shell 重定向小于号(但它被 ServerFault 解析器吃掉了)。尝试在那里使用“shell”而不是“command”。命令不通过外壳程序,因此重定向和管道之类的外壳程序将不起作用。壳应该工作。