Home > Net >  Ansible Docker containers dynamic inventory on MacOS
Ansible Docker containers dynamic inventory on MacOS

Time:02-08

I have started a Docker container using the following command:

docker run tomcat:latest

Then I created a file named docker.yml with the following contents:

plugin: community.docker.docker_containers
docker_host: unix://var/run/docker.sock

Finally I try to obtain a list of the currently running Docker containers using:

ansible-inventory -i docker.yml --list

However instead of a list of running containers, I only get the following result:

[WARNING]:  * Failed to parse docker.yml with yaml plugin: Plugin configuration YAML file, not YAML inventory
[WARNING]:  * Failed to parse docker.yml with constructed plugin: Incorrect plugin name in file: community.docker.docker_containers
[WARNING]: Unable to parse docker.yml as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
{
    "_meta": {
        "hostvars": {}
    },
    "all": {
        "children": [
            "ungrouped"
        ]
    }
}

Have I misunderstood the Ansible Docker containers dynamic inventory or am I doing something wrong?

CodePudding user response:

I suspect I had a case of system in disarray and this was the cure:

  • I retained my current Python installation located at ~/Library/Python/3.9/.
  • Attempted to uninstall Ansible using pip:
    pip uninstall ansible
    
  • Manually removed all things Ansible:
    sudo rm -r /etc/ansible
    sudo rm -r -/.ansible
    sudo rm -r /usr/local/lib/python3.9/site-packages/ansible*
    sudo rm /usr/local/bin/ansible*
    
  • Performed a fresh installation of Ansible:
    pip install ez_setup
    pip install --user ansible
    
  • Installed Ansible Docker collection prerequisite:
    pip install docker
    
  • Installed Ansible Docker collection:
    ansible-galaxy collection install community.docker
    

After the above, the Ansible Docker container dynamic inventory works as expected and without errors.

  •  Tags:  
  • Related