Stelios Joseph Karras
Stelios Joseph Karras

Reputation: 483

SSH-less LXC containers using Ansible

I am new to ansible, and I am trying to use ansible on some lxc containers. My problem is that I don't want to install ssh on my containers. So

What I tried:

I tried to use this connection plugin but it seams that it does not work with ansible 2.
After understanding that chifflier connection plugin doesn't work, I tried to use the connection plugin from openstack.

After some failed attempts I dived into the code, and I understand that the plugin doesn't have the information that the host I am talking with is a container.(because the code never reached this point)

My current setup: {Ansbile host}---|ssh|---{vm}--|ansible connection plugin|---{container1}

My ansible.cfg:

[defaults]
connection_plugins = /home/jkarr/ansible-test/connection_plugins/ssh
inventory = inventory

My inventory:

[hosts]
vm ansible_host=192.168.28.12

[containers]
mailserver physical_host=vm container_name=mailserver

my group vars:

ansible_host: "{{ physical_hostname }}"
ansible_ssh_extra_args: "{{ container_name }}"
ansible_user: containeruser
container_name: "{{ inventory_hostname }}"
physical_hostname: "{{ hostvars[physical_host]['ansible_host'] }}"

My testing playbook:

- name: Test Playbook
  hosts: containers
  gather_facts: true
  tasks:
    - name: testfile
      copy:
        content: "Test"
        dest: /tmp/test

The output is:

fatal: [mailserver]: UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname mailserver: No address associated with hostname\r\n", 
    "unreachable": true
}

Ansible version is: 2.3.1.0

So what am I doing wrong? any tips? Thanks in advance!

Update 1:
Based on eric answer I am now using this connection plug-in. I update the my inventory and it looks like:

[hosts]
vm ansible_host=192.168.28.12

[containers]
mailserver physical_host=vm ansible_connection=lxc 

After running my playbook I took:

<192.168.28.12> THIS IS A LOCAL LXC DIR
fatal: [mailserver]: FAILED! => {
    "failed": true, 
    "msg": "192.168.28.12 is not running"
}

Which is weird because 192.168.28.12 is the vm and the container is called mailserver. Also I verified that the container is running.

Also why it says that 192.168.28.12 is local lxc dir?

Update 2:

I remove my group_vars, my ansible.cfg and the connection plugin from the playbook and I got the this error:

<mailserver> THIS IS A LOCAL LXC DIR
fatal: [mailserver]: FAILED! => {
    "failed": true, 
    "msg": "mailserver is not running"
}

Upvotes: 3

Views: 3299

Answers (2)

Philip Szalla
Philip Szalla

Reputation: 111

I'm trying something similar.

I want to configure a host over ssh using ansible and run lxc containers on the host, which are also configured using ansible:

ansible control node ----> host-a -----------> container-a
                      ssh          lxc-attach

The issue with the lxc connection module is, that it only works for local lxc containers. There is no way to get it working through ssh.

At the moment the only way seems to be a direct ssh connection or a ssh connection through the first host:

                      ssh
ansible control node ----> container-a

or
                      ssh          ssh
ansible control node ----> host-a ----> container-a

Both require sshd installed in the container. But the second way doesn't need port forwarding or multiple ip addresses.

Did you get a working solution?

Upvotes: 0

Eric Citaire
Eric Citaire

Reputation: 4513

You should take a look at this lxc connection plugin. It might fit your needs.

Edit : lxc connection plugin is actually part of Ansible.

Just add ansible_connection=lxc in your inventory or group vars.

Upvotes: 8

Related Questions