ansible adhoc 명령은 playbook 을 작성하지 않고 command-line 에서 직접 앤서블 모듈을 호출해서 실행하는 방식입니다.


간단하며 재사용이 필요없는 명령어를 여러 호스트에서 실행할 수 있습니다.


애드혹 명령어 사용법은 다음과 같이 호스트 패턴을 입력하고 -m 옵션 뒤에 사용할 모듈을 지정하며 [  ] 안에 있는 내용은 옵션입니다.

$ ansible host-pattern -m module [-a 'module options'] [-i inventory]
BASH


애드혹은 ansible module 을 그대로 사용할 수 있지만 playbook 과 달리 복잡한 명령보다는 간단한 테스트용 모듈을 구동할 때 많이 사용합니다.

-m 옵션으로 모듈을 지정하지 않을 경우 ansible 은 임의의 명령을 실행할 수 있는 모듈인 command 모듈을 사용합니다.


ping module

많이 사용하는 모듈중 하나는 서버에서 python 모듈 실행 여부를 확인할 수 있는 ping 모듈(TCP 의 ping 아님)로 다음은 webserver 인벤토리내 모든 서버에 대해 테스트를 실행합니다.

$ ansible webserver -m ping

localhost | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
BASH

setup module

애드혹에서 많이 사용하는 모듈중 하나는 ansible fact 를 수집하는 setup 모듈입니다.

실행하면 인벤토리에 있는 서버의 모든 fact 를 수집해서 출력합니다.

$ ansible webserver -m setup

localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "172.31.23.247",
            "10.88.0.1"
        ],
	 ....
}
BASH

출력되는 양이 너무 많으므로 setup 모듈이 지원하는 필터 기능을 사용해서 필요한 데이타만 출력할 수 있습니다. 필터를 사용할 경우 모듈에 아규먼트로 사용할 필터를 넘겨주면 됩니다.


다음은 ansible_dist 문자열이 있는 fact 목록을 출력하며 배포판 종류, 버전, 릴리스 코드명 등을 출력합니다.

$ ansible all -m setup -a "filter=ansible_dist*"
localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution": "RedHat",
        "ansible_distribution_file_parsed": true,
        "ansible_distribution_file_path": "/etc/redhat-release",
        "ansible_distribution_file_search_string": "Red Hat",
        "ansible_distribution_file_variety": "RedHat",
        "ansible_distribution_major_version": "8",
        "ansible_distribution_release": "Ootpa",
        "ansible_distribution_version": "8.3",
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
BASH


같이 보기


Ref