perf: gather facts

pull/8991/head
feng 2022-10-25 18:43:34 +08:00
parent a445e47f3d
commit 4dd4c29e12
12 changed files with 28 additions and 4669 deletions

View File

@ -2,27 +2,21 @@
gather_facts: no gather_facts: no
vars: vars:
ansible_python_interpreter: /usr/local/bin/python ansible_python_interpreter: /usr/local/bin/python
jms_account:
username: root
secret: redhat
jms_asset:
address: 127.0.0.1
port: 3306
tasks: tasks:
- name: Gather facts info - name: Get info
community.mysql.mysql_info: community.mysql.mysql_info:
login_user: "{{ jms_account.username }}" login_user: "{{ jms_account.username }}"
login_password: "{{ jms_account.secret }}" login_password: "{{ jms_account.secret }}"
login_host: "{{ jms_asset.address }}" login_host: "{{ jms_asset.address }}"
login_port: "{{ jms_asset.port }}" login_port: "{{ jms_asset.port }}"
filter: version
register: db_info register: db_info
- name: Get info - name: Define Mysql info by set_fact
set_fact: set_fact:
info: info:
version: "{{ db_info.version.full }}" version: "{{ db_info.version.full }}"
- debug: - debug:
var: db_info var: info

View File

@ -2,19 +2,9 @@
gather_facts: no gather_facts: no
vars: vars:
ansible_python_interpreter: /usr/local/bin/python ansible_python_interpreter: /usr/local/bin/python
jms_account:
username: postgre
secret: postgre
jms_asset:
address: 127.0.0.1
port: 5432
database: testdb
account:
username: test
secret: jumpserver
tasks: tasks:
- name: Test PostgreSQL connection - name: Get info
community.postgresql.postgresql_info: community.postgresql.postgresql_info:
login_user: "{{ jms_account.username }}" login_user: "{{ jms_account.username }}"
login_password: "{{ jms_account.secret }}" login_password: "{{ jms_account.secret }}"
@ -23,6 +13,10 @@
login_db: "{{ jms_asset.database }}" login_db: "{{ jms_asset.database }}"
register: db_info register: db_info
- name: Debug it - name: Define Postgresql info by set_fact
debug: set_fact:
var: db_info info:
version: "{{ db_info.server_version.raw }}"
- debug:
var: info

View File

@ -1,10 +0,0 @@
{% for account in accounts %}
- hosts: {{ account.asset.name }}
vars:
account:
username: {{ account.username }}
password: {{ account.password }}
public_key: {{ account.public_key }}
roles:
- change_secret
{% endfor %}

View File

@ -1,8 +0,0 @@
id: gather_facts_sqlserver
name: Change password for SQLServer
version: 1
category: database
type:
- sqlserver
method: gather_facts

View File

@ -1,27 +0,0 @@
- name: ping
ansible.builtin.ping:
#- name: print variables
# debug:
# msg: "Username: {{ account.username }}, Password: {{ account.password }}"
- name: Change password
user:
name: "{{ account.username }}"
password: "{{ account.password | password_hash('des') }}"
update_password: always
when: account.password
- name: Change public key
authorized_key:
user: "{{ account.username }}"
key: "{{ account.public_key }}"
state: present
when: account.public_key
- name: Verify password
ansible.builtin.ping:
vars:
ansible_user: "{{ account.username }}"
ansible_pass: "{{ account.password }}"
ansible_ssh_connection: paramiko

View File

@ -1,4 +1,5 @@
from common.utils import get_logger from common.utils import get_logger
from assets.const import AutomationTypes
from ..base.manager import BasePlaybookManager from ..base.manager import BasePlaybookManager
logger = get_logger(__name__) logger = get_logger(__name__)
@ -11,7 +12,7 @@ class GatherFactsManager(BasePlaybookManager):
@classmethod @classmethod
def method_type(cls): def method_type(cls):
return 'gather_facts' return AutomationTypes.gather_facts
def host_callback(self, host, asset=None, **kwargs): def host_callback(self, host, asset=None, **kwargs):
super().host_callback(host, asset=asset, **kwargs) super().host_callback(host, asset=asset, **kwargs)
@ -19,14 +20,10 @@ class GatherFactsManager(BasePlaybookManager):
return host return host
def on_host_success(self, host, result): def on_host_success(self, host, result):
info = result.get('Get info', {}).get('res', {}).get('ansible_facts', {}).get('info', {}) info = result.get('debug', {}).get('res', {}).get('info', {})
asset = self.host_asset_mapper.get(host) asset = self.host_asset_mapper.get(host)
if asset and info: if asset and info:
asset.info = info asset.info = info
asset.save() asset.save()
else: else:
logger.error("Not found info, task name must be 'Get info': {}".format(host)) logger.error("Not found info, task name must be 'Get info': {}".format(host))

View File

@ -9,6 +9,7 @@ from common.db.fields import EncryptJsonDictTextField
from orgs.mixins.models import OrgModelMixin from orgs.mixins.models import OrgModelMixin
from ops.mixin import PeriodTaskModelMixin from ops.mixin import PeriodTaskModelMixin
from assets.models import Node, Asset from assets.models import Node, Asset
from assets.tasks import execute_automation
class BaseAutomation(CommonModelMixin, PeriodTaskModelMixin, OrgModelMixin): class BaseAutomation(CommonModelMixin, PeriodTaskModelMixin, OrgModelMixin):
@ -38,7 +39,11 @@ class BaseAutomation(CommonModelMixin, PeriodTaskModelMixin, OrgModelMixin):
return assets.group_by_platform() return assets.group_by_platform()
def get_register_task(self): def get_register_task(self):
raise NotImplementedError name = f"automation_{self.type}_strategy_period_{str(self.id)[:8]}"
task = execute_automation.name
args = (str(self.id), Trigger.timing, self._meta.model)
kwargs = {}
return name, task, args, kwargs
def get_many_to_many_ids(self, field: str): def get_many_to_many_ids(self, field: str):
return [str(i) for i in getattr(self, field).all().values_list('id', flat=True)] return [str(i) for i in getattr(self, field).all().values_list('id', flat=True)]

View File

@ -4,7 +4,6 @@ from django.utils.translation import ugettext_lazy as _
from common.db import fields from common.db import fields
from common.const.choices import Trigger from common.const.choices import Trigger
from common.db.models import JMSBaseModel from common.db.models import JMSBaseModel
from assets.tasks import execute_change_secret_automation
from assets.const import AutomationTypes, SecretType, SecretStrategy, SSHKeyStrategy from assets.const import AutomationTypes, SecretType, SecretStrategy, SSHKeyStrategy
from .base import BaseAutomation from .base import BaseAutomation
@ -35,13 +34,6 @@ class ChangeSecretAutomation(BaseAutomation):
class Meta: class Meta:
verbose_name = _("Change secret automation") verbose_name = _("Change secret automation")
def get_register_task(self):
name = "automation_change_secret_strategy_period_{}".format(str(self.id)[:8])
task = execute_change_secret_automation.name
args = (str(self.id), Trigger.timing)
kwargs = {}
return name, task, args, kwargs
def to_attr_json(self): def to_attr_json(self):
attr_json = super().to_attr_json() attr_json = super().to_attr_json()
attr_json.update({ attr_json.update({

View File

@ -1,16 +1,15 @@
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from assets.const import AutomationTypes
from .base import BaseAutomation from .base import BaseAutomation
__all__ = ['GatherFactsAutomation'] __all__ = ['GatherFactsAutomation']
class GatherFactsAutomation(BaseAutomation): class GatherFactsAutomation(BaseAutomation):
class Meta:
verbose_name = _("Gather asset facts")
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
self.type = 'gather_facts' self.type = AutomationTypes.gather_facts
super().save(*args, **kwargs) super().save(*args, **kwargs)
class Meta:
verbose_name = _("Gather asset facts")

View File

@ -7,12 +7,11 @@ logger = get_logger(__file__)
@shared_task @shared_task
def execute_change_secret_automation(pid, trigger): def execute_automation(pid, trigger, mode):
from assets.models import ChangeSecretAutomation
with tmp_to_root_org(): with tmp_to_root_org():
instance = get_object_or_none(ChangeSecretAutomation, pk=pid) instance = get_object_or_none(mode, pk=pid)
if not instance: if not instance:
logger.error("No automation plan found: {}".format(pid)) logger.error("No automation task found: {}".format(pid))
return return
with tmp_to_org(instance.org): with tmp_to_org(instance.org):
instance.execute(trigger) instance.execute(trigger)

View File

@ -1,10 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
import abc import abc
import uuid
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.db import models from django.db import models
from django import forms
from rest_framework import serializers from rest_framework import serializers
from .celery.utils import ( from .celery.utils import (