인벤토리를 지정하지 않고 호스트를 직접 지정하지 않고 Ansible을 실행하는 방법은 무엇입니까?
(ANSIBLE_HOST)를 통해 인벤토리 파일을 지정하지 않고 Python에서 Ansible을 실행하고 싶습니다.
ansible.run.Runner(
module_name='ping',
host='www.google.com'
)
실제로 패브릭에서 쉽게 할 수 있지만 파이썬에서 어떻게할지 궁금합니다. 반면에 Python 용 Ansible API 문서는 실제로 완전하지 않습니다.
놀랍게도 트릭은 ,
# Host and IP address
ansible all -i example.com,
ansible all -i 93.184.216.119,
또는
# Requires 'hosts: all' in your playbook
ansible-playbook -i example.com, playbook.yml
앞의 호스트 매개 변수 ,
는 호스트 이름 또는 IPv4 / v6 주소 일 수 있습니다.
이 질문이 정말 오래되었다는 것을 알고 있지만이 작은 트릭이 도움이 필요한 미래의 사용자에게 도움이 될 것이라고 생각합니다.
ansible-playbook -i 10.254.3.133, site.yml
로컬 호스트에 대해 실행하는 경우 :
ansible-playbook -i localhost, --connection=local site.yml
트릭은 IP 주소 / DNS 이름 뒤에 쉼표를 따옴표 안에 넣고 hosts: all
플레이 북에 ' '가 필요하다는 것 입니다.
이것이 도움이되기를 바랍니다.
다음과 같이 할 수 있습니다.
hosts = ["webserver1","webserver2"]
webInventory = ansible.inventory.Inventory(hosts)
webPing = ansible.runner.Runner(
pattern='webserver*',
module_name='ping',
inventory = webInventory
).run()
호스트에있는 모든 것이 인벤토리가되고 패턴으로 검색 (또는 "모두"수행) 할 수 있습니다.
또한 Ansible Python API 를 구동 해야했으며 인벤토리를 유지하는 대신 호스트를 인수로 전달하는 것이 좋습니다. Ansible의 요구 사항을 우회하기 위해 임시 파일을 사용했는데, 이는 다른 사람들에게 도움이 될 수 있습니다.
from tempfile import NamedTemporaryFile
from ansible.inventory import Inventory
from ansible.runner import Runner
def load_temporary_inventory(content):
tmpfile = NamedTemporaryFile()
try:
tmpfile.write(content)
tmpfile.seek(0)
inventory = Inventory(tmpfile.name)
finally:
tmpfile.close()
return inventory
def ping(hostname):
inventory = load_temporary_inventory(hostname)
runner = Runner(
module_name='ping',
inventory=inventory,
)
return runner.run()
이것은 완전한 대답은 아니지만 이 토론 스레드 에이 주제에 대한 토론이 있습니다. 해당 스레드의 첫 번째 게시물이 끝나면 ansible-playbook에 대한 래퍼 bash 스크립트를 만들라는 제안이 있습니다. 이는 약간의 해킹이지만 실행 가능합니다.
Other things that I've been considering are the use of 'ansible-pull' and the creation of an ansible inventory plugin. I'm also interested in finding the answer to this question, and I'll keep updating this answer as I find more information.
There seems to be not direct way to give a pattern. This is my hack to solve it.
echo fldn[3789:3799].mysite.com >test; ansible all -i test -m ping
In my case, I did not want to have hosts: all
in my playbook, because it would be bad if someone ran the playbook and forgot to include -i 10.254.3.133,
This was my solution (ansible 2.6):
$ ansible-playbook myplaybook.yml -e "{target: 10.1.1.1}" -i 10.1.1.1, ...
And then, in the playbook:
- hosts: "{{ target }}"
remote_user: donn
vars_files:
- myvars
roles:
- myrole
This is a special use-case when I need to provision a host and I don't want/need to add it to the inventory.
'Programing' 카테고리의 다른 글
스크립트 태그에 매개 변수를 전달하는 방법은 무엇입니까? (0) | 2020.11.13 |
---|---|
C #을 사용하여 배열에서 빈 값 제거 (0) | 2020.11.13 |
이 표현에 무슨 일이 일어나고 있습니까? (0) | 2020.11.13 |
모호함을 통한 보안이 왜 나쁜 생각입니까? (0) | 2020.11.13 |
이번 주와 달의 첫날을 얻는 방법은 무엇입니까? (0) | 2020.11.13 |