Ansible playbook 无法使用复杂的开关运行 make & configure

Dun*_*ock 10 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
    shell: patch -t {{ curl_dir }}/lib/ssh.c {{ home }}/ssh.c.patch chdir={{ curl_dir }}/lib
    when: path_file_result.changed

  - name: Build CURL with openssl
    command: 'chdir={{ curl_dir }} "{{ item }}"'
    with_items:
      - ./buildconf
      - ./configure --with-gssapi --with-libidn --with-libssh2 --prefix=/usr --without-nss
      - make
      - make install
      - ldconfig
Run Code Online (Sandbox Code Playgroud)

Vagrant 工作正常,Ansible playbook 成功运行到最后一个任务“使用 openssl 构建 CURL” - 失败,如下所示:

    TASK: [Build CURL with openssl] *********************************************** 
    changed: [default] => (item=./buildconf) => {"changed": true, "cmd": ["./buildconf"], "delta": "0:00:10.709817", "end": "2014-02-07 02:26:44.802652", "item": "./buildconf", "rc": 0, "start": "2014-02-07 02:26:34.092835", "stderr": "configure.ac:78: installing `./compile'
    configure.ac:73: installing `./config.guess'
    configure.ac:73: installing `./config.sub'
    configure.ac:65: installing `./missing'
    Makefile.am: installing `./depcomp'
    configure.ac:137: installing `./config.guess'
    configure.ac:137: installing `./config.sub'
    docs/examples/Makefile.am: installing `./depcomp'", "stdout": "buildconf: autoconf version 2.63 (ok)
    buildconf: autom4te version 2.63 (ok)
    buildconf: autoheader version 2.63 (ok)
    buildconf: automake version 1.11.1 (ok)
    buildconf: aclocal version 1.11.1 (ok)
    buildconf: libtool version 2.2.6 (ok)
    buildconf: libtoolize found
    buildconf: GNU m4 version 1.4.13 (ok)
    buildconf: running libtoolize
    buildconf: running aclocal
    buildconf: running aclocal hack to convert all mv to mv -f
    buildconf: running autoheader
    buildconf: cp lib/curl_config.h.in src/curl_config.h.in
    buildconf: running autoconf
    buildconf: running in ares
    buildconf: running automake
    buildconf: OK"}

    failed: [default] => (item=./configure --with-gssapi --with-libidn --with-libssh2 --prefix=/usr --without-nss) => {"cmd": ["./configure --with-gssapi --with-libidn --with-libssh2 --prefix=/usr --without-nss"], "failed": true, "item": "./configure --with-gssapi --with-libidn --with-libssh2 --prefix=/usr --without-nss", "rc": 2}
    msg: [Errno 2] No such file or directory
    failed: [default] => (item=make) => {"changed": true, "cmd": ["make"], "delta": "0:00:00.001828", "end": "2014-02-07 02:26:45.003968", "item": "make", "rc": 2, "start": "2014-02-07 02:26:45.002140"}
    stderr: make: *** No targets specified and no makefile found.  Stop.
    failed: [default] => (item=make install) => {"cmd": ["make install"], "failed": true, "item": "make install", "rc": 2}
    msg: [Errno 2] No such file or directory
    changed: [default] => (item=ldconfig) => {"changed": true, "cmd": ["ldconfig"], "delta": "0:00:00.009685", "end": "2014-02-07 02:26:46.096829", "item": "ldconfig", "rc": 0, "start": "2014-02-07 02:26:46.087144", "stderr": "", "stdout": ""}

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

因此,该./buildconf步骤有效,但该./configure --with-gssapi --with-libidn --with-libssh2 --prefix=/usr --without-nss步骤似乎因[Errno 2] No such file or directory. 我认为这是因为它试图像运行单个命令一样运行整个字符串?

如果我将其更改为 useshell而不是 ,则会command得到以下信息:

failed: [default] => (item=./configure --with-gssapi --with-libidn --with-libssh2 --prefix=/usr --without-nss) => {"changed": true, "cmd": " \"./configure --with-gssapi --with-libidn --with-libssh2 --prefix=/usr --without-nss\" ", "delta": "0:00:00.001171", "end": "2014-02-07 02:31:34.862373", "item": "./configure --with-gssapi --with-libidn --with-libssh2 --prefix=/usr --without-nss", "rc": 127, "start": "2014-02-07 02:31:34.861202"}
stderr: /bin/sh: ./configure --with-gssapi --with-libidn --with-libssh2 --prefix=/usr --without-nss: No such file or directory
Run Code Online (Sandbox Code Playgroud)

我已经验证了到那时为止的所有任务都可以正常工作,并且文件被下载并解压缩到预期的位置并且补丁可以正常工作(请参阅此处)。

任务失败后(或者如果您将其注释掉),如果您通过 SSH 连接到正在配置的 VM,并自己运行所有相同的构建步骤 - 使用剧本中的确切值,它就可以工作。

我对 Ansible 还是很陌生,我不确定为什么这不起作用?我究竟做错了什么?如果这是问题,是否有某种方法可以以configure不同的方式格式化或引用该命令,以便对其进行正确解释?我应该raw为此使用吗?或者是其他东西?

Dun*_*ock 14

解决方案是改变这一点:

shell: 'chdir={{ curl_dir }} "{{ item }}"'
Run Code Online (Sandbox Code Playgroud)

对此:

shell: "{{ item }}"
args:
  chdir: "{{ curl_dir }}"
Run Code Online (Sandbox Code Playgroud)

shell 模块文档现在解决了格式中的这个棘手问题。完整的工作构建任务目前看起来像这样:

- name: Build CURL with openssl
  shell: "{{ item }}"
  args:
    chdir: "{{ curl_dir }}"
  with_items:
    - ./buildconf
    - ./configure --with-gssapi --with-libidn --with-libssh2 --prefix=/usr --without-nss
    - make
    - make install
    - ldconfig
Run Code Online (Sandbox Code Playgroud)