Skip to content

Commit

Permalink
Merge pull request #4122 from PaulYuuu/ansible-connection
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulYuuu authored Sep 20, 2024
2 parents cf84766 + 58f91a6 commit 8edbb35
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 44 deletions.
53 changes: 24 additions & 29 deletions provider/ansible.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import logging
import os
import sys

from aexpect.client import Expect

Expand Down Expand Up @@ -29,7 +30,7 @@ class ExecutorTimeoutError(Exception):

class PlaybookExecutor(Expect):
def __init__(self, inventory, site_yml, remote_user=None, extra_vars=None,
callback_plugin=None, addl_opts=None):
callback_plugin=None, connection_plugin=None, addl_opts=None):
"""
The wrapper of Ansible-playbook.
Expand All @@ -38,13 +39,15 @@ def __init__(self, inventory, site_yml, remote_user=None, extra_vars=None,
:param remote_user: Connect as this user.
:param extra_vars: Set additional variables.
:param callback_plugin: The plugin of the main manager of console output.
:param connection_plugin: Connection plugin to connect to target hosts
:param addl_opts: Other ansible-playbook common options.
"""
self.program = path.find_command('ansible-playbook')
self.inventory = inventory
self.site_yml = site_yml
self.remote_user = remote_user
self.callback_plugin = callback_plugin
self.connection_plugin = connection_plugin
super(PlaybookExecutor, self).__init__(self._generate_cmd(extra_vars,
addl_opts))
LOG_JOB.info("Command of ansible playbook: '%s'", self.command)
Expand All @@ -57,13 +60,15 @@ def _generate_cmd(self, extra_vars=None, addl_opts=None):
:param addl_opts: Other ansible-playbook common options.
:return: The generated ansible-playbook command line.
"""
playbook_cmd_options = []
playbook_cmd_options = ["ANSIBLE_HOST_KEY_CHECKING=false"]
if self.callback_plugin:
playbook_cmd_options = [
'ANSIBLE_STDOUT_CALLBACK={}'.format(self.callback_plugin)]
playbook_cmd_options.append(
'ANSIBLE_STDOUT_CALLBACK={}'.format(self.callback_plugin))
playbook_cmd_options.extend([self.program,
self.site_yml,
'-i {}'.format(self.inventory)])
not self.connection_plugin or playbook_cmd_options.append(
'-c {}'.format(self.connection_plugin))
not self.remote_user or playbook_cmd_options.append(
'-u {}'.format(self.remote_user))
not extra_vars or playbook_cmd_options.append(
Expand Down Expand Up @@ -119,33 +124,26 @@ def check_ansible_playbook(params):
:param params: Dictionary with the test parameters.
:return: True if full ansible version is installed, else False.
"""

def _pip_binary():
"""
Define pip binary
"""
for binary in ['pip', 'pip3', 'pip2']:
if process.system("which %s" % binary, ignore_status=True) == 0:
return binary
LOG_JOB.error("Failed to get available pip binary")
return False

def python_install():
def python_install(packages=None):
"""
Install python ansible.
"""
install_cmd = '%s install ansible' % pip_bin # pylint: disable=E0606
if packages is None:
packages = ["ansible"]
install_cmd = f"{sys.executable} -m pip install {' '.join(packages)}"
status, output = process.getstatusoutput(install_cmd, verbose=True)
if status != 0:
LOG_JOB.error("Install python ansible failed as: %s", output)
LOG_JOB.error("Install python packages failed as: %s", output)
return False
return True

def distro_install(packages="ansible"):
def distro_install(packages=None):
"""
Install ansible from the distro
"""
# Provide custom dnf repo containing ansible
if packages is None:
packages = ["ansible"]
if params.get("ansible_repo"):
repo_options = {
"priority": "1",
Expand Down Expand Up @@ -174,15 +172,10 @@ def distro_install(packages="ansible"):
if ansible_install_policy not in policy_map:
LOG_JOB.error(f"No valid install policy: {ansible_install_policy}.")
return False
package_list = params.get_list("package_list", 'sshpass')
try:
check_cmd = params.get("ansible_check_cmd")
if ansible_install_policy == 'python_install':
global pip_bin
pip_bin = _pip_binary()
check_cmd = rf"{pip_bin} freeze | grep -v ansible-core | grep -q ansible="
elif ansible_install_policy == 'distro_install':
package_list.insert(0, 'ansible')
check_cmd = rf"{sys.executable} -m pip freeze | grep -q ^ansible=="
if check_cmd:
LOG_JOB.debug(f"Is full ansible version installed: '{check_cmd}'")
process.run(check_cmd, verbose=False, shell=True)
Expand All @@ -196,8 +189,10 @@ def distro_install(packages="ansible"):
if not policy_map[ansible_install_policy]():
return False
# Install ansible depended packages that can't be installed
# by pip (or are not a dependency) when installing ansible
if not policy_map['distro_install'](package_list):
return False
# automatically when installing ansible
package_dict = params.get_dict("package_dict", delimiter=",")
for policy, pkg_list in package_dict.items():
if not policy_map[f'{policy}_install'](pkg_list.split()):
return False
# If ansible and dependents packages are installed correctly
return True
6 changes: 3 additions & 3 deletions qemu/tests/ansible_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def run(test, params, env):
guest_passwd = params["password"]
step_time = params.get_numeric("step_time", 60)
ansible_callback_plugin = params.get("ansible_callback_plugin")
ansible_connection_plugin = params.get("ansible_connection_plugin")
ansible_addl_opts = params.get("ansible_addl_opts", "")
ansible_ssh_extra_args = params["ansible_ssh_extra_args"]
ansible_extra_vars = params.get("ansible_extra_vars", "{}")
custom_extra_vars = params.objects("custom_extra_vars")
playbook_repo = params["playbook_repo"]
Expand All @@ -59,8 +59,7 @@ def run(test, params, env):

error_context.base_context("Generate playbook related options.",
test.log.info)
extra_vars = {"ansible_ssh_extra_args": ansible_ssh_extra_args,
"ansible_ssh_pass": guest_passwd,
extra_vars = {"ansible_ssh_pass": guest_passwd,
"test_harness_log_dir": test_harness_log_dir}
extra_vars.update(json.loads(ansible_extra_vars))
custom_params = params.object_params("extra_vars")
Expand All @@ -74,6 +73,7 @@ def run(test, params, env):
remote_user=guest_user,
extra_vars=json.dumps(extra_vars),
callback_plugin=ansible_callback_plugin,
connection_plugin=ansible_connection_plugin,
addl_opts=ansible_addl_opts
)

Expand Down
6 changes: 3 additions & 3 deletions qemu/tests/ansible_with_responsive_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def run(test, params, env):
guest_passwd = params["password"]
step_time = params.get_numeric("step_time", 60)
ansible_callback_plugin = params.get("ansible_callback_plugin")
ansible_connection_plugin = params.get("ansible_connection_plugin")
ansible_addl_opts = params.get("ansible_addl_opts", "")
ansible_ssh_extra_args = params["ansible_ssh_extra_args"]
ansible_extra_vars = params.get("ansible_extra_vars", "{}")
custom_extra_vars = params.objects("custom_extra_vars")
playbook_repo = params["playbook_repo"]
Expand Down Expand Up @@ -66,8 +66,7 @@ def run(test, params, env):

error_context.base_context("Generate playbook related options.",
test.log.info)
extra_vars = {"ansible_ssh_extra_args": ansible_ssh_extra_args,
"ansible_ssh_pass": guest_passwd,
extra_vars = {"ansible_ssh_pass": guest_passwd,
"mq_port": mq_listen_port,
"test_harness_log_dir": test_harness_log_dir}
extra_vars.update(json.loads(ansible_extra_vars))
Expand All @@ -82,6 +81,7 @@ def run(test, params, env):
remote_user=guest_user,
extra_vars=json.dumps(extra_vars),
callback_plugin=ansible_callback_plugin,
connection_plugin=ansible_connection_plugin,
addl_opts=ansible_addl_opts
)

Expand Down
3 changes: 2 additions & 1 deletion qemu/tests/cfg/ansible_test.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
toplevel_playbook = "site.yml"
playbook_timeout = 600
ansible_callback_plugin = debug
ansible_ssh_extra_args = "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
ansible_repo = ""
# Here we can define an extra set of variables for the playbook with json format
#ansible_extra_vars = '{"debug_msg": "Hello Ansible!", "force_handlers": true}'
Expand All @@ -16,6 +15,8 @@
#echo_word_extra_vars = Hello ansible
start_vm = no
ansible_install_policy = 'distro_install'
#package_dict = distro=sshpass,python=paramiko
#ansible_connection_plugin = paramiko_ssh
variants:
- @default:
- responsive_migration:
Expand Down
1 change: 0 additions & 1 deletion qemu/tests/cfg/nested_block_resize.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
toplevel_playbook = "site.yml"
playbook_timeout = 600
ansible_callback_plugin = debug
ansible_ssh_extra_args = "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
ansible_repo = ""
only virtio_scsi
wait_response_timeout = 1800
Expand Down
1 change: 0 additions & 1 deletion qemu/tests/cfg/nested_vsock_con_sockets.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
virt_test_type = qemu
playbook_timeout = 600
ansible_callback_plugin = debug
ansible_ssh_extra_args = "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
wait_response_timeout = 600
vsocks = vhost_vsock0
vsock_port = 2345
Expand Down
6 changes: 3 additions & 3 deletions qemu/tests/nested_block_resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def _on_status(obj, msg):
guest_passwd = params["password"]
step_time = params.get_numeric("step_time", 60)
ansible_callback_plugin = params.get("ansible_callback_plugin")
ansible_connection_plugin = params.get("ansible_connection_plugin")
ansible_addl_opts = params.get("ansible_addl_opts", "")
ansible_ssh_extra_args = params["ansible_ssh_extra_args"]
ansible_extra_vars = params.get("ansible_extra_vars", "{}")
playbook_repo = params["playbook_repo"]
playbook_timeout = params.get_numeric("playbook_timeout")
Expand All @@ -97,8 +97,7 @@ def _on_status(obj, msg):

error_context.base_context("Generate playbook related options.",
test.log.info)
extra_vars = {"ansible_ssh_extra_args": ansible_ssh_extra_args,
"ansible_ssh_pass": guest_passwd,
extra_vars = {"ansible_ssh_pass": guest_passwd,
"mq_port": mq_listen_port,
"test_harness_log_dir": test_harness_log_dir}
extra_vars.update(json.loads(ansible_extra_vars))
Expand All @@ -110,6 +109,7 @@ def _on_status(obj, msg):
remote_user=guest_user,
extra_vars=json.dumps(extra_vars),
callback_plugin=ansible_callback_plugin,
connection_plugin=ansible_connection_plugin,
addl_opts=ansible_addl_opts
)

Expand Down
6 changes: 3 additions & 3 deletions qemu/tests/nested_vsock_con_sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def _send_file_from_host_to_l1(obj, msg):
guest_passwd = params["password"]
step_time = params.get_numeric("step_time", 60)
ansible_callback_plugin = params.get("ansible_callback_plugin")
ansible_connection_plugin = params.get("ansible_connection_plugin")
ansible_addl_opts = params.get("ansible_addl_opts", "")
ansible_ssh_extra_args = params["ansible_ssh_extra_args"]
ansible_extra_vars = params.get("ansible_extra_vars", "{}")
playbook_repo = params["playbook_repo"]
playbook_timeout = params.get_numeric("playbook_timeout")
Expand All @@ -96,8 +96,7 @@ def _send_file_from_host_to_l1(obj, msg):

error_context.base_context("Generate playbook related options.",
test.log.info)
extra_vars = {"ansible_ssh_extra_args": ansible_ssh_extra_args,
"ansible_ssh_pass": guest_passwd,
extra_vars = {"ansible_ssh_pass": guest_passwd,
"mq_port": mq_port,
"test_harness_log_dir": test_harness_log_dir}
extra_vars.update(json.loads(ansible_extra_vars))
Expand All @@ -109,6 +108,7 @@ def _send_file_from_host_to_l1(obj, msg):
remote_user=guest_user,
extra_vars=json.dumps(extra_vars),
callback_plugin=ansible_callback_plugin,
connection_plugin=ansible_connection_plugin,
addl_opts=ansible_addl_opts
)

Expand Down

0 comments on commit 8edbb35

Please sign in to comment.