Ansible에서 여러 줄 셸 스크립트를 수행하는 방법
지금은 여러 줄에있는 경우 훨씬 더 읽기 쉬운 ansible에서 쉘 스크립트를 사용하고 있습니다.
- name: iterate user groups
shell: groupmod -o -g {{ item['guid'] }} {{ item['username'] }} ....more stuff to do
with_items: "{{ users }}"
Ansible 셸 모듈에서 여러 줄 스크립트를 허용하는 방법을 잘 모르겠습니다.
Ansible은 플레이 북에서 YAML 구문을 사용합니다. YAML에는 여러 블록 연산자가 있습니다.
은
>
접는 블록 연산자이다. 즉, 여러 줄을 공백으로 결합합니다. 다음 구문 :key: > This text has multiple lines
에 값
This text has multiple lines\n
을 할당합니다key
.|
문자는 블록 문자 연산자이다. 이것은 아마도 여러 줄 셸 스크립트에 대해 원하는 것입니다. 다음 구문 :key: | This text has multiple lines
에 값
This text\nhas multiple\nlines\n
을 할당합니다key
.
다음과 같은 여러 줄 셸 스크립트에 사용할 수 있습니다.
- name: iterate user groups
shell: |
groupmod -o -g {{ item['guid'] }} {{ item['username'] }}
do_some_stuff_here
and_some_other_stuff
with_items: "{{ users }}"
한 가지주의 할 점이 있습니다. Ansible은 shell
명령 에 대한 인수의 버벅 거림 조작을 수행하므로 위의 내용은 일반적으로 예상대로 작동하지만 다음은 작동하지 않습니다.
- shell: |
cat <<EOF
This is a test.
EOF
Ansible은 실제로 해당 텍스트를 선행 공백으로 렌더링합니다. 즉, 셸이 EOF
줄 시작 부분에서 문자열 을 찾지 못함을 의미합니다 . 다음 cmd
과 같은 매개 변수 를 사용하여 Ansible의 도움이되지 않는 휴리스틱 스를 피할 수 있습니다 .
- shell:
cmd: |
cat <<EOF
This is a test.
EOF
YAML 라인 연속을 언급합니다.
예 (ansible 2.0.0.2로 시도) :
---
- hosts: all
tasks:
- name: multiline shell command
shell: >
ls --color
/home
register: stdout
- name: debug output
debug: msg={{ stdout }}
셸 명령은 다음과 같이 한 줄로 축소됩니다. ls --color /home
EOF 구분 기호 앞에 공백을 추가하면 cmd를 피할 수 있습니다.
- shell: |
cat <<' EOF'
This is a test.
EOF
참고 URL : https://stackoverflow.com/questions/40230184/how-to-do-multiline-shell-script-in-ansible
'developer tip' 카테고리의 다른 글
Java String의 유니 코드 코드 포인트를 어떻게 반복 할 수 있습니까? (0) | 2020.08.28 |
---|---|
같은 줄에 새 출력 인쇄 (0) | 2020.08.28 |
Maven 2에서 아티팩트를 수동으로 설치하는 방법은 무엇입니까? (0) | 2020.08.28 |
Ant : 디렉토리의 각 파일에 대해 명령을 실행하는 방법은 무엇입니까? (0) | 2020.08.28 |
재귀 대 반복 (0) | 2020.08.28 |