andreasscherbaum / ansible-lxc-ssh

Ansible connection plugin using ssh + lxc-attach
45 stars 22 forks source link

Cannot detect lxc when installed as a Snap #21

Closed dmp1ce closed 5 years ago

dmp1ce commented 5 years ago

Because I have installed lxd as a snap, the plugin cannot see lxc when it checks for it. As a result the plugin fails with Cannot identify LXC version.

Perhaps there is a more general way to address this with Ansible rather than solving the issue with the plugin. Maybe this is an Ansible issue. I'm not sure. Here is more information on the issue: https://www.reddit.com/r/ansible/comments/awjm0j/difference_between_path_while_manually_ssh_into/

Any help would be appreciated.

andreasscherbaum commented 5 years ago

Where is "lxc" installed, what is the path?

dmp1ce commented 5 years ago

/snap/bin/lxc

andreasscherbaum commented 5 years ago

As stated in the Reddit responses, you need to make sure that this is in your $PATH.

This plugin can't do anything if lxc is not in your path, also Ansible can't do anything about it.

Well, it could specifically search in /snap/bin, but that's exactly what $PATH is for, to avoid these kind of problems.

dmp1ce commented 5 years ago

I'm not sure, but this might still be a problem because I have tried to set the $PATH but I still get the same error. Shouldn't changing the environment work in setting the $PATH that I need? Do I need to change the environment for the host or container hosts section?

This is what my playbook looks like at the moment:

- hosts: lxd_host
  environment:
    PATH: /snap/bin:{{ ansible_env.PATH }}
  become: yes
  tasks:
    - name: Package dependencies
      apt:
         name: lxc-utils

- hosts: lxd_containers
  environment:
    PATH: /snap/bin:{{ ansible_env.PATH }}

  tasks:
    # Update lxc container
    - ping:
    #- name: Test that a file can be created inside container
    #  file:
    #    path: /tmp/testfile-ansible
    #    state: touch

Debug output:

XX connect
<192.168.1.204> ESTABLISH SSH CONNECTION FOR USER: dave
<192.168.1.204> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=publickey,gssapi-with-mic,gssapi-keyex,hostbased -o PasswordAuthentication=no -o User=dave -o Conn
ectTimeout=10 -o ControlPath=/home/david/.ansible/cp/829ec5e5f8 -tt 192.168.1.204 'which lxc'
<192.168.1.204> (1, b'', b'Shared connection to 192.168.1.204 closed.\r\n')
XXX connect
<192.168.1.204> ESTABLISH SSH CONNECTION FOR USER: dave
<192.168.1.204> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=publickey,gssapi-with-mic,gssapi-keyex,hostbased -o PasswordAuthentication=no -o User=dave -o Conn
ectTimeout=10 -o ControlPath=/home/david/.ansible/cp/829ec5e5f8 -tt 192.168.1.204 'which lxc-info'
<192.168.1.204> (1, b'', b'Shared connection to 192.168.1.204 closed.\r\n')
fatal: [omd]: FAILED! => {
    "msg": "Cannot identify LXC version"
}
andreasscherbaum commented 5 years ago

I'm not sure if the environment applies for connection settings.

dmp1ce commented 5 years ago

I would like to modify the environment through Ansible so that the /snap/bin directory can be found in the $PATH. Is this a limitation of this connection plugin or of Ansible in general?

andreasscherbaum commented 5 years ago

The connection plugin works before tasks are run, therefore setting the environment in the Playbook will not work. I still think that should be either fixed in your environment itself (somewhere in your login settings), or you have to raise this with Ansible.

dmp1ce commented 5 years ago

After patching #5 locally I was able to get lxc in my path with the task:

- hosts: lxd_host
  tasks:
    - name: Ensure /snap/bin is in path for lxc
      lineinfile:
        path: "~/.bashrc"
        regexp: '^PATH='
        line: PATH=$PATH:/snap/bin
        insertbefore: BOF
andreasscherbaum commented 5 years ago

So it is a problem in your environment (on the LXC host), after all.

dmp1ce commented 5 years ago

I course I can work around my issue by changing the host. I would probably say a feature could be added to the connection plugin system to allow the PATH to be changed on each command, but I haven't submitted an issue to Ansible.

I only posted the task I used to help others if they run into this with the plugin. I would expect it is common to install LXD with snaps on Ubuntu.