태그 보관물: ansible

ansible

complex-args 양식을 사용하여 자유 양식 명령을 Ansible에 전달 피하기 위해 (

프로그래밍 방식으로 생성 된 Ansible 플레이 북을 사용하고 있습니다. 일반적으로 플레이 북은 YAML이므로 간단합니다. 그러나 “간단한” key=value양식을 사용할 때 플레이 북 순수한 YAML 이 아니며shlex 구문 분석 가능한 양식이 포함 된 컨텐츠를 포함합니다 .

이 형식의 모호성을 피하기 위해 ( key=value명령에 대한 인수 또는 ansible에 대한 인수를 결합합니까?) 단일 형식으로 구문 분석 및 생성 해야합니다. -examples 저장소 .

이것은 다음 종류의 구문을 사용합니다.

action: module-name
args:
  key1: value1
  key2: value2

… 좋아요. 그러나, shell또는 command모듈 에이 양식을 사용하려고 할 때 ( 문서에 이름 free_form이 지정된 인수로 전달 된 실제 명령을 설명하는 )이 기능이 제대로 작동하지 않습니다.

action: shell
args:
  free_form: echo hello_world >/tmp/something
  creates: /tmp/something

호출되면 다음이 실행됩니다.

/bin/sh -c " free_form='echo hello_world >/tmp/something'  "

… 나는 달성하려는 것이 아닙니다.

순수한 YAML 구문을 사용하여 “자유 형식”명령을 수행하는 Ansible 모듈을 사용하는 올바른 방법은 무엇입니까?



답변

짧은 답변 : 할 일이 사용하지 command, raw, script, 또는 shell모듈을. 명령을 “정상”인수로 받아들이는 자체 모듈을 작성하십시오.

긴 대답 :

대부분의 경우 다음을 수행 할 수 있습니다.

- shell: echo hello_world > /tmp/something
  args:
    creates: /tmp/something

그러나 일부 경우에는 실패합니다.

- shell: echo hello_world > creates=something
  args:
    creates: creates=something  # The file is named "creates=something"

나는 이것을 처리하는 일반적인 방법을 모르지만 bash 특정 솔루션은 다음과 같습니다.

- shell: echo hello_world > "creates=something"
  args:
    creates: creates=something

답변

이것은 지금 Ansible 문서 에서 다루어 졌습니다.

# You can also use the 'args' form to provide the options. This command
# will change the working directory to somedir/ and will only run when
# /path/to/database doesn't exist.
- command: /usr/bin/make_database.sh arg1 arg2
  args:
    chdir: somedir/
    creates: /path/to/database

‘free_form’이라는 매개 변수가 없습니다.


답변