首页 文章

从连接到docker容器的代码/命令行运行ansible-playbook

提问于
浏览
0

我用这个:

Specify docker containers in /etc/ansible/hosts file

在docker容器上运行我的ansible playbooks .

但有没有办法避免使用物理 /etc/ansible/hosts 文件包含有关容器的信息?例如 . 从可以配置此信息的代码运行它?

我在看:

Running ansible-playbook using Python API

但是当看到答案时,我看到指向物理文件的变量,例如:

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

所以不确定为什么这比仅仅在不使用Python ansible API的情况下从命令行调用更好 .

1 回答

  • 0

    可以在Docker容器中安装Ansible,然后在容器内本地运行它 . 例如,在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
    

相关问题