Program Tip

ansible : 여러 명령을 전달하는 방법

programtip 2020. 12. 3. 19:10
반응형

ansible : 여러 명령을 전달하는 방법


나는 이것을 시도했다 :

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

작동하지만 뭔가 더 .. 깔끔한 것 같아요.

그래서 나는 이것을 시도했습니다.

에서 : 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

견적 문제가 있다고 말하면 작동하지 않습니다.

누군가?


YAML의 값이 중괄호 ( {)로 시작하는 경우 YAML 파서는 해당 값 이 사전 이라고 가정합니다 . 따라서 값에 (Jinja2) 변수가있는 이와 같은 경우 YAML 파서의 혼동을 피하기 위해 다음 두 가지 전략 중 하나를 채택해야합니다.

전체 명령을 인용하십시오.

- 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 대안 제안에 감사드립니다.


ansible로 여러 셸 명령을 실행하려면 다음 예제와 같이 여러 줄 문자열이있는 모듈을 사용할 수 있습니다 ( shell : 뒤에 수직 슬래시 참고) .

  - name: Build nginx 
    shell: |
      cd nginx-1.11.13
      sudo ./configure
      sudo make
      sudo make install

나는 같은 문제에 직면했다. 제 경우에는 변수의 일부가 사전에있었습니다. 예를 들어 with_dict 변수 (루핑) 였고 각 item.key에 대해 3 개의 명령을 실행해야했습니다. 이 솔루션은 여러 명령을 실행하는 with_dict 사전을 사용해야하는 경우에 더 적합합니다 ( with_items 필요 없음 ).

한 작업에서 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

위의 결과 각 {{항목}}에 대해 다음과 유사한 오류가 발생했습니다 (위에서 언급 한 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

Solution was easy. Instead of using "COMMAND" module in Ansible, I used "Shell" module and created a a variable in roles/git/defaults/main.yml

So, now roles/git/defaults/main.yml looks like:

---
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"

and the task roles/git/tasks/main.yml looks like:

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

This time, the values got successfully substituted as the module was "SHELL" and ansible output echoed the correct values. This didn't require with_items: loop.

"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",

You can also do like this:

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

Hope that might help other

참고URL : https://stackoverflow.com/questions/24851575/ansible-how-to-pass-multiple-commands

반응형