ad-hoc 명령이란?

ansible은 일반적으로 playbook을 사용하도록 설계되어 있다. 하지만 한번만 딱 쓰거나 간단한 명령, 상태를 확인하는 정도의 명령은 굳이 플레이북을 사용하지 않고 간단한 명령 구문으로 신속하게 수행할 수 있다. 이 방식이 ad-hoc  명령이라고 부른다. 예를 들어, ad-hoc 명령 한줄로 인벤토리에 있는 모든 서버의 /etc/hosts 파일의 특정 줄을 확인할 수 있다.

 

 

ad-hoc 명령 구문

ansible 호스트명패턴 -m 모듈명 [-a 'module argunets'] [-i inventory]

* 설명

- 대괄호는 필수가 아닌 선택사항이다.

- 호스트명 패턴은 web[1:9].example.com 이런식으로 사용하거나, 그룹명 또는 그룹의 그룹명을 명시하는 방법을 쓸 수 있다.

 

ad-hoc 명령 옵션

* 일반 옵션

-m 모듈을 명시하는 옵션이다.
-i  특정 인벤토리 파일(경로포함)을 명시해서 사용하는 옵션이다.
-a

여러 모듈은 추가 명시해야 할 인수가 있을 수도 있고 없을수도 있다. -a 옵션은 추가 인수를 넣을 때 명시한다. 대부분의 모듈은 인수를 받는다. 모듈에서 사용하는 인수들은 ansible-doc 명령으로 확인 가능하다. 여러 인수를 넣을 때는 다음과 같이 사용한다. -a 'name=newbie uid=400 state=present'

-o 결과가 보통 여러줄로 나오는데, 결과를 한줄로 나오게 한다.
-v 어떤 구성파일을 쓰고 있는지 명시해준다.

* 권한 및 유저 관련 옵션

--become-method BECOME_METHOD
--become-user BECOME_USER
--ask-become-pass, -K
--become, -b
--ask-pass, -k
--user REMOTE_USER, -u REMOTE_USER
이 옵션들 어디서 많이 본 옵션들이다. 4장에서 다룬 ansible.cfg 구성파일에 있는 내용들이다. 즉 ansible.cfg에서 기본 설정을 해 두었더라도, ad-hoc 명령 구문에서 바꿀 수 있다는 것이다. ansible.cfg에서 설정한 값보다, ad-hoc 명령에서 옵션을 넣은 값이 더 우선순위가 높다.

예를들어 ansible.cfg 파일에서 become = false , true값으로 변경하고 싶은데 이번 한번만 그렇게 하고 싶다면, 명령어 라인에서 --become 옵션만 넣어주면 된다는 것이다. (예시 : ansible all -m copy -a 'content="This server is managed by Ansible!.\n" dest=/etc/motd' --become)

상세 내용은 아래 4장에서, ansible.cfg 구성파일 내용 부분을 확인할 수 있다. 2021/02/06 - [Ansible] - 4장. ansible.cfg 파일 톺아보기

# 추가 이해를 위한 참고 예시

ansible localhost -m command -a 'id'

ansible localhost -m command -a 'id' -u devops

- 두 명령의 결과는 다를 것이다.

 

* 관리 옵션

--version ansible의 버전 및 기타 환경 정보를 확인한다.
--list-hosts 인벤토리를 확인할 때 사용한다. 상세 내용은 아래 3장을 참고한다.
2021/02/06 - [Ansible] - 3장. Inventory 파일
-v, --verbose verbose 모드로, -vvv은 더 상세하게, -vvvv는 디버그 모드이다. troubleshooting을 할 때 사용한다.

여러 옵션이 더 있는데, 더 자세한 것은 ansible --help에서 확인할 수 있다.

 

 

 

참고 링크

 

패턴 사용 : ansible 설명서

https://docs.ansible.com/ansible/2.7/user_guide/intro_patterns.html

 

Working with Patterns — Ansible Documentation

Docs » User Guide » Working with Patterns You are reading an unmaintained version of the Ansible documentation. Unmaintained Ansible versions can contain unfixed security vulnerabilities (CVE). Please upgrade to a maintained version. See the latest Ansib

docs.ansible.com

애드혹 명령 소개: ansible 문서

http://docs.ansible.com/ansible/2.7/user_guide/intro_adhoc.html

 

Introduction To Ad-Hoc Commands — Ansible Documentation

Docs » User Guide » Introduction To Ad-Hoc Commands You are reading an unmaintained version of the Ansible documentation. Unmaintained Ansible versions can contain unfixed security vulnerabilities (CVE). Please upgrade to a maintained version. See the la

docs.ansible.com

 

'Ansible (RH294 & EX294)' 카테고리의 다른 글

부록1 - 자주 사용되는 모듈들  (0) 2021.02.19
6장. ansible에서 모듈 사용하기  (0) 2021.02.19
4장. ansible.cfg 파일 톺아보기  (0) 2021.02.06
3장. Inventory 파일  (2) 2021.02.06
2장. Ansible 설치하기  (0) 2021.02.06

+ Recent posts