web-dev-qa-db-ja.com

ansible:複数のコマンドを渡す方法

私はこれを試しました:

- command: ./configure chdir=/src/package/
- command: /usr/bin/make chdir=/src/package/
- command: /usr/bin/make install chdir=/src/package/

うまくいきますが、もっと何かあると思います。

だから私はこれを試しました:

from: https://stackoverflow.com/questions/24043561/multiple-commands-in-the-same-line-for-bruker-topspin 「そのようなファイルやディレクトリはありません」

- command: ./configure;/usr/bin/make;/usr/bin/make install chdir=/src/package/

私もこれを試しました: https://u.osu.edu/hasnan.1/2013/12/16/ansible-run-multiple-commands-using-command-module-and-with-items/

しかし、私は置くための正しい構文を見つけることができませんでした:

- command: "{{ item }}" chdir=/src/package/
  with_items:
      ./configure
      /usr/bin/make
      /usr/bin/make install

引用の問題があると言って、それはうまくいきません。

誰でも?

59
John Doe

YAMLの値が中括弧({)で始まる場合、YAMLパーサーはそれが 辞書 であると想定します。そのため、値に(Jinja2)変数があるこのようなケースでは、YAMLパーサーの混乱を避けるために、次の2つの戦略のいずれかを採用する必要があります。

コマンド全体を引用します。

- command: "{{ item }} chdir=/src/package/"
  with_items:
  - ./configure
  - /usr/bin/make
  - /usr/bin/make install    

または、引数の順序を変更します。

- command: chdir=/src/package/ {{ item }}
  with_items:
  - ./configure
  - /usr/bin/make
  - /usr/bin/make install

@ RamondelaFuenteの代替提案に感謝します。

79
Pedro Romano

Ansibleで複数のシェルコマンドを実行するには、次の例に示すように、複数行の文字列でShellモジュールを使用できます(Shell:の後のvertical slashに注意してください)。

  - name: Build nginx 
    Shell: |
      cd nginx-1.11.13
      Sudo ./configure
      Sudo make
      Sudo make install
48
Arvik

私は同じ問題に直面しました。私の場合、変数の一部はディクショナリ、つまりwith_dict変数(ループ)にあり、各item.keyで3つのコマンドを実行する必要がありました。このソリューションは、複数のコマンドを実行してwith_dict辞書を使用する必要がある場合に関連性が高くなります(with_itemsを必要とせずに

1つのタスクでwith_dictとwith_itemsを使用しても、変数を解決していなかったため、役に立ちませんでした。

私の仕事は次のようなものでした:

- name: Make install git source
  command: "{{ item }}"
  with_items:
    - cd {{ tools_dir }}/{{ item.value.artifact_dir }}
    - make prefix={{ tools_dir }}/{{ item.value.artifact_dir }} all
    - make prefix={{ tools_dir }}/{{ item.value.artifact_dir }} install
  with_dict: "{{ git_versions }}"

roles/git/defaults/main.ymlは:

---
tool: git
default_git: git_2_6_3

git_versions:
  git_2_6_3:
    git_tar_name: git-2.6.3.tar.gz
    git_tar_dir: git-2.6.3
    git_tar_url: https://www.kernel.org/pub/software/scm/git/git-2.6.3.tar.gz

上記の結果、各{{item}}について次のようなエラーが発生しました(上記の3つのコマンドの場合)。ご覧のとおり、tools_dirの値は設定されていません(tools_dirは共通のロールのdefaults/main.ymlで定義されている変数であり、item.value.git_tar_dirの値は設定も解決もされていません)。

failed: [server01.poc.jenkins] => (item=cd {# tools_dir #}/{# item.value.git_tar_dir #}) => {"cmd": "cd '{#' tools_dir '#}/{#' item.value.git_tar_dir '#}'", "failed": true, "item": "cd {# tools_dir #}/{# item.value.git_tar_dir #}", "rc": 2}
msg: [Errno 2] No such file or directory

解決は簡単でした。 Ansibleで「COMMAND」モジュールを使用する代わりに、「Shell」モジュールを使用して、roles/git/defaults/main.ymlに変数を作成しました

したがって、roles/git/defaults/main.ymlは次のようになります。

---
tool: git
default_git: git_2_6_3

git_versions:
  git_2_6_3:
    git_tar_name: git-2.6.3.tar.gz
    git_tar_dir: git-2.6.3
    git_tar_url: https://www.kernel.org/pub/software/scm/git/git-2.6.3.tar.gz

#git_pre_requisites_install_cmds: "cd {{ tools_dir }}/{{ item.value.git_tar_dir }} && make prefix={{ tools_dir }}/{{ item.value.git_tar_dir }} all && make prefix={{ tools_dir }}/{{ item.value.git_tar_dir }} install"

#or use this if you want git installation to work in ~/tools/git-x.x.x
git_pre_requisites_install_cmds: "cd {{ tools_dir }}/{{ item.value.git_tar_dir }} && make prefix=`pwd` all && make prefix=`pwd` install"

#or use this if you want git installation to use the default prefix during make 
#git_pre_requisites_install_cmds: "cd {{ tools_dir }}/{{ item.value.git_tar_dir }} && make all && make install"

タスクroles/git/tasks/main.ymlは次のようになります。

- name: Make install from git source
  Shell: "{{ git_pre_requisites_install_cmds }}"
  become_user: "{{ build_user }}"
  with_dict: "{{ git_versions }}"
  tags:
    - koba

今回は、モジュールが「シェル」であり、ansible出力が正しい値をエコーし​​たため、値は正常に置換されました。 これはwith_itemsを必要としませんでした:ループ。

"cmd": "cd ~/tools/git-2.6.3 && make prefix=/home/giga/tools/git-2.6.3 all && make prefix=/home/giga/tools/git-2.6.3 install",
5
Arun Sangal

次のようにすることもできます。

- command: "{{ item }}"
  args:
    chdir: "/src/package/"
  with_items:
    - "./configure"
    - "/usr/bin/make"
    - "/usr/bin/make install"

他の人に役立つことを願っています

4
Arbab Nazar