Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update lxd connection to use all documented vars for options #3798

Merged
merged 4 commits into from
Dec 9, 2021

Conversation

cavcrosby
Copy link
Contributor

SUMMARY

The changes below are in attempt to "fix" the mapping of the inventory vars to the LXD connection plugin documented options.

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

plugins/connection/lxd.py

ADDITIONAL INFORMATION

STEPS TO REPRODUCE

The following example assumes there exist a VM called 'vm1' that runs LXD, also this LXD instance manages the container 'container1'.

inventory

vm1:container1 ansible_lxd_remote=x.x.x.x ansible_lxd_host=container1

[lxd_containers]
vm1:container1

[all:vars]
ansible_user=ansible
ansible_ssh_common_args='-o StrictHostKeyChecking=no'

playbook

---
- name: Check/install python on LXD container nodes
  hosts: lxd_containers
  connection: lxd
  gather_facts: no

  tasks:
    - name: Check if python3 is installed in container
      ansible.builtin.raw: dpkg -s python3
      register: python_install_check
      failed_when: python_install_check.rc not in [0, 1]
      changed_when: false

    - name: Install python3 in container
      ansible.builtin.raw: apt-get install --assume-yes python3
      when: python_install_check.rc == 1

output

ansible-playbook 2.10.5
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/conner/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 3.9.5 (default, May 11 2021, 08:20:37) [GCC 10.3.0]
Using /etc/ansible/ansible.cfg as config file
setting up inventory plugins
host_list declined parsing /home/conner/git/playground/vagrant/ansible/vagrant_ansible_inventory as it did not pass its verify_file() method
script declined parsing /home/conner/git/playground/vagrant/ansible/vagrant_ansible_inventory as it did not pass its verify_file() method
auto declined parsing /home/conner/git/playground/vagrant/ansible/vagrant_ansible_inventory as it did not pass its verify_file() method
Parsed /home/conner/git/playground/vagrant/ansible/vagrant_ansible_inventory inventory source with ini plugin
Loading callback plugin default of type stdout, v2.0 from /usr/lib/python3/dist-packages/ansible/plugins/callback/default.py
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.

PLAYBOOK: playbook.yaml ********************************************************
Positional arguments: playbook.yaml
verbosity: 4
connection: smart
timeout: 60
become_method: sudo
become_ask_pass: True
tags: ('all',)
inventory: ('/home/conner/git/playground/vagrant/ansible/vagrant_ansible_inventory',)
forks: 5
1 plays in playbook.yaml

PLAY [Check/install python on LXD container nodes] *****************************
META: ran handlers

TASK [Check if python3 is installed in container] ******************************
task path: /home/conner/git/playground/vagrant/ansible/playbook.yaml:10
redirecting (type: connection) ansible.builtin.lxd to community.general.lxd
Loading collection community.general from /home/conner/.ansible/collections/ansible_collections/community/general
[WARNING]: lxd does not support remote_user, using container default: root
<vm1:container1> ESTABLISH LXD CONNECTION FOR USER: root
<vm1:container1> EXEC dpkg -s python3
ok: [vm1:container1] => {
    "changed": false,
    "failed_when_result": false,
    "msg": "non-zero return code",
    "rc": 1,
    "stderr": "Error: Not Found\n",
    "stderr_lines": [
        "Error: Not Found"
    ],
    "stdout": "",
    "stdout_lines": []
}

TASK [Install python3 in container] ********************************************
task path: /home/conner/git/playground/vagrant/ansible/playbook.yaml:16
redirecting (type: connection) ansible.builtin.lxd to community.general.lxd
Loading collection community.general from /home/conner/.ansible/collections/ansible_collections/community/general
[WARNING]: lxd does not support remote_user, using container default: root
<vm1:container1> ESTABLISH LXD CONNECTION FOR USER: root
<vm1:container1> EXEC apt-get install --assume-yes python3
fatal: [vm1:container1]: FAILED! => {
    "changed": true,
    "msg": "non-zero return code",
    "rc": 1,
    "stderr": "Error: Not Found\n",
    "stderr_lines": [
        "Error: Not Found"
    ],
    "stdout": "",
    "stdout_lines": []
}

PLAY RECAP *********************************************************************
vm1:container1        : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0 

After applying the following changes, running the same playbook

ansible-playbook 2.10.5
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/conner/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 3.9.5 (default, May 11 2021, 08:20:37) [GCC 10.3.0]
Using /etc/ansible/ansible.cfg as config file
setting up inventory plugins
host_list declined parsing /home/conner/git/playground/vagrant/ansible/vagrant_ansible_inventory as it did not pass its verify_file() method
script declined parsing /home/conner/git/playground/vagrant/ansible/vagrant_ansible_inventory as it did not pass its verify_file() method
auto declined parsing /home/conner/git/playground/vagrant/ansible/vagrant_ansible_inventory as it did not pass its verify_file() method
Parsed /home/conner/git/playground/vagrant/ansible/vagrant_ansible_inventory inventory source with ini plugin
Loading callback plugin default of type stdout, v2.0 from /usr/lib/python3/dist-packages/ansible/plugins/callback/default.py
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.

PLAYBOOK: playbook.yaml ********************************************************
Positional arguments: playbook.yaml
verbosity: 4
connection: smart
timeout: 60
become_method: sudo
become_ask_pass: True
tags: ('all',)
inventory: ('/home/conner/git/playground/vagrant/ansible/vagrant_ansible_inventory',)
forks: 5
1 plays in playbook.yaml

PLAY [Check/install python on LXD container nodes] *****************************
META: ran handlers

TASK [Check if python3 is installed in container] ******************************
task path: /home/conner/git/playground/vagrant/ansible/playbook.yaml:10
redirecting (type: connection) ansible.builtin.lxd to community.general.lxd
Loading collection community.general from /home/conner/.ansible/collections/ansible_collections/community/general
[WARNING]: lxd does not support remote_user, using container default: root
<vm1:container1> ESTABLISH LXD CONNECTION FOR USER: root
<vm1:container1> EXEC dpkg -s python3
ok: [vm1:container1] => {
    "changed": false,
    "failed_when_result": false,
    "rc": 0,
    "stderr": "",
    "stderr_lines": [],
    "stdout": "Package: python3\nStatus: install ok installed\nPriority: optional\nSection: python\nInstalled-Size: 89\nMaintainer: Matthias Klose <doko@debian.org>\nArchitecture: amd64\nMulti-Arch: allowed\nSource: python3-defaults\nVersion: 3.9.2-3\nReplaces: python3-minimal (<< 3.1.2-2)\nProvides: python3-profiler\nDepends: python3.9 (>= 3.9.2-0~), libpython3-stdlib (= 3.9.2-3)\nPre-Depends: python3-minimal (= 3.9.2-3)\nSuggests: python3-doc (>= 3.9.2-3), python3-tk (>= 3.9.2-0~), python3-venv (>= 3.9.2-3)\nDescription: interactive high-level object-oriented language (default python3 version)\n Python, the high-level, interactive object oriented language,\n includes an extensive class library with lots of goodies for\n network programming, system administration, sounds and graphics.\n .\n This package is a dependency package, which depends on Debian's default\n Python 3 version (currently v3.9).\nHomepage: https://www.python.org/\nCnf-Extra-Commands: python\nCnf-Priority-Bonus: 5\n",
    "stdout_lines": [
        "Package: python3",
        "Status: install ok installed",
        "Priority: optional",
        "Section: python",
        "Installed-Size: 89",
        "Maintainer: Matthias Klose <doko@debian.org>",
        "Architecture: amd64",
        "Multi-Arch: allowed",
        "Source: python3-defaults",
        "Version: 3.9.2-3",
        "Replaces: python3-minimal (<< 3.1.2-2)",
        "Provides: python3-profiler",
        "Depends: python3.9 (>= 3.9.2-0~), libpython3-stdlib (= 3.9.2-3)",
        "Pre-Depends: python3-minimal (= 3.9.2-3)",
        "Suggests: python3-doc (>= 3.9.2-3), python3-tk (>= 3.9.2-0~), python3-venv (>= 3.9.2-3)",
        "Description: interactive high-level object-oriented language (default python3 version)",
        " Python, the high-level, interactive object oriented language,",
        " includes an extensive class library with lots of goodies for",
        " network programming, system administration, sounds and graphics.",
        " .",
        " This package is a dependency package, which depends on Debian's default",
        " Python 3 version (currently v3.9).",
        "Homepage: https://www.python.org/",
        "Cnf-Extra-Commands: python",
        "Cnf-Priority-Bonus: 5"
    ]
}

TASK [Install python3 in container] ********************************************
task path: /home/conner/git/playground/vagrant/ansible/playbook.yaml:16
skipping: [vm1:container1] => {
    "changed": false,
    "skip_reason": "Conditional result was False"
}
META: ran handlers
META: ran handlers

PLAY RECAP *********************************************************************
vm1:container1        : ok=1    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   

The main one I wanted to see change was interpolation of <instance> (lxc exec [<remote>:]<instance> ...) being from self.get_option("remote_addr") instead of self._host. If an inventory alias is used for a node, then the ansible_lxd_host inventory variable is ignored entirely. That said, ansible_host worked ok here vs ansible_lxd_host.

The others are so the other inventory vars can be mapped according to the other documented options. It could also be that it is desired to still make use of the play_context, and that the other specific lxd variables (e.g. ansible_lxd_host, ansible_lxd_executable) are not required.

@ansibullbot
Copy link
Collaborator

@ansibullbot ansibullbot added bug This issue/PR relates to a bug connection connection plugin new_contributor Help guide this first time contributor plugins plugin (any type) labels Nov 27, 2021
Copy link
Collaborator

@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! Can you please add a changelog fragment? Thanks!

@felixfontein felixfontein added backport-3 check-before-release PR will be looked at again shortly before release and merged if possible. labels Nov 28, 2021
@@ -126,7 +126,7 @@ def put_file(self, in_path, out_path):
local_cmd.extend([
"file", "push",
in_path,
"%s:%s/%s" % (self.get_option("remote"), self._host, out_path)
"%s:%s/%s" % (self.get_option("remote"), self.get_option("remote_addr"), out_path)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you only replace it here, in line 92 and in line 148?

Copy link
Contributor Author

@cavcrosby cavcrosby Nov 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self._host seems to work fine else where in the plugin (otherwise its usage is just to be appended to verbose/error output), and seeing the inventory alias (e.g. vm1:container1) would be more descriptive in the case a user chooses to use something more elaborate for the inventory alias.

@ansibullbot ansibullbot added needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR and removed needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Dec 1, 2021
@felixfontein
Copy link
Collaborator

If nobody complains, I'll merge in a couple of days.

@felixfontein felixfontein removed the check-before-release PR will be looked at again shortly before release and merged if possible. label Dec 9, 2021
@felixfontein felixfontein merged commit 8f6866d into ansible-collections:main Dec 9, 2021
@patchback
Copy link

patchback bot commented Dec 9, 2021

Backport to stable-3: 💚 backport PR created

✅ Backport PR branch: patchback/backports/stable-3/8f6866dba6a2e2d1e8d90c0120924efb5eb4fb30/pr-3798

Backported as #3880

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

@felixfontein
Copy link
Collaborator

@cavcrosby thanks for your contribution!

patchback bot pushed a commit that referenced this pull request Dec 9, 2021
* Update lxd connection to use documented vars

* Add a changelog fragment

* Add clarification to changelog description

* Shorten changelog fragment description

(cherry picked from commit 8f6866d)
@patchback
Copy link

patchback bot commented Dec 9, 2021

Backport to stable-4: 💚 backport PR created

✅ Backport PR branch: patchback/backports/stable-4/8f6866dba6a2e2d1e8d90c0120924efb5eb4fb30/pr-3798

Backported as #3881

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

patchback bot pushed a commit that referenced this pull request Dec 9, 2021
* Update lxd connection to use documented vars

* Add a changelog fragment

* Add clarification to changelog description

* Shorten changelog fragment description

(cherry picked from commit 8f6866d)
felixfontein pushed a commit that referenced this pull request Dec 9, 2021
…3880)

* Update lxd connection to use documented vars

* Add a changelog fragment

* Add clarification to changelog description

* Shorten changelog fragment description

(cherry picked from commit 8f6866d)

Co-authored-by: Conner Crosby <conner@cavcrosby.tech>
felixfontein pushed a commit that referenced this pull request Dec 9, 2021
…3881)

* Update lxd connection to use documented vars

* Add a changelog fragment

* Add clarification to changelog description

* Shorten changelog fragment description

(cherry picked from commit 8f6866d)

Co-authored-by: Conner Crosby <conner@cavcrosby.tech>
@cavcrosby cavcrosby deleted the lxd-inventory-vars branch March 24, 2024 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue/PR relates to a bug connection connection plugin new_contributor Help guide this first time contributor plugins plugin (any type)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants