简体   繁体   English

从连接到Docker容器的代码/命令行运行Ansible-Playbook

[英]Run ansible-playbook from code/command line connecting to docker container

I am using this: 我正在使用这个:

Specify docker containers in /etc/ansible/hosts file 在/ etc / ansible / hosts文件中指定Docker容器

to run my ansible playbooks against a docker container. 对docker容器运行我的ansible剧本。

But is there any way to avoid having a physical /etc/ansible/hosts file with the information about the container? 但是,有什么方法可以避免包含有关容器信息的物理/etc/ansible/hosts文件? Eg run it from code where this information can be configured? 例如从可以配置此信息的代码运行它?

I looked at: 我看着:

Running ansible-playbook using Python API 使用Python API运行Ansible-Playbook

but when looking at the answers I see variables pointing to physical files, eg: 但是当查看答案时,我看到指向物理文件的变量,例如:

inventory = Inventory(loader=loader, sources='/home/slotlocker/hosts2')
playbook_path = '/home/slotlocker/ls.yml'

So not really sure why that is better than simply just calling from command line without using the Python ansible API. 因此,不能确定为什么这比只使用命令行而不使用Python ansible API更好。

May be install Ansible in Docker container and then run it locally inside the container. 可以将Ansible安装在Docker容器中,然后在容器内本地运行。 For example in the Dockerfile, include: 例如,在Dockerfile中,包括:

# Install Ansible
RUN pip install ansible
COPY ansible /tmp/ansible
# Run Ansible to configure the machine
RUN cd /tmp/ansible && ansible-playbook -i inventory/docker example_playbook.yml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM