perf: push account automation (#9497)

Co-authored-by: feng <1304903146@qq.com>
pull/9499/head
fit2bot 2023-02-10 14:07:20 +08:00 committed by GitHub
parent fcf20335e8
commit 0c11a602a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 25 additions and 4 deletions

View File

@ -8,11 +8,18 @@
# debug: # debug:
# msg: "Username: {{ account.username }}, Password: {{ account.secret }}" # msg: "Username: {{ account.username }}, Password: {{ account.secret }}"
- name: Get groups of a Windows user
ansible.windows.win_user:
name: "{{ jms_account.username }}"
register: user_info
- name: Change password - name: Change password
ansible.windows.win_user: ansible.windows.win_user:
name: "{{ account.username }}" name: "{{ account.username }}"
password: "{{ account.secret }}" password: "{{ account.secret }}"
groups: "Administrators" groups: "{{ user_info.groups[0].name }}"
groups_action: add
update_password: always update_password: always
when: account.secret_type == "password" when: account.secret_type == "password"

View File

@ -22,6 +22,8 @@ logger = get_logger(__name__)
class ChangeSecretManager(AccountBasePlaybookManager): class ChangeSecretManager(AccountBasePlaybookManager):
ansible_account_prefer = ''
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.method_hosts_mapper = defaultdict(list) self.method_hosts_mapper = defaultdict(list)

View File

@ -30,6 +30,10 @@ class GatherAccountsFilter:
result = {} result = {}
for line in info: for line in info:
data = line.split('@') data = line.split('@')
if len(data) == 1:
result[line] = {}
continue
if len(data) != 3: if len(data) != 3:
continue continue
username, address, dt = data username, address, dt = data

View File

@ -4,8 +4,13 @@
- name: Gather posix account - name: Gather posix account
ansible.builtin.shell: ansible.builtin.shell:
cmd: > cmd: >
users=$(getent passwd | grep -v nologin | grep -v shutdown | awk -F":" '{ print $1 }');for i in $users; users=$(getent passwd | grep -v nologin | grep -v shutdown | awk -F":" '{ print $1 }');for i in $users;
do last -w -F $i -1 | head -1 | grep -v ^$ | awk '{ print $1"@"$3"@"$5,$6,$7,$8 }';done do k=$(last -w -F $i -1 | head -1 | grep -v ^$ | awk '{ print $1"@"$3"@"$5,$6,$7,$8 }')
if [ -n "$k" ]; then
echo $k
else
echo $i
fi;done
register: result register: result
- name: Define info by set_fact - name: Define info by set_fact

View File

@ -12,6 +12,7 @@ logger = get_logger(__name__)
class PushAccountManager(ChangeSecretManager, AccountBasePlaybookManager): class PushAccountManager(ChangeSecretManager, AccountBasePlaybookManager):
ansible_account_prefer = ''
@classmethod @classmethod
def method_type(cls): def method_type(cls):

View File

@ -25,6 +25,7 @@ class PlaybookCallback(DefaultCallback):
class BasePlaybookManager: class BasePlaybookManager:
bulk_size = 100 bulk_size = 100
ansible_account_policy = 'privileged_first' ansible_account_policy = 'privileged_first'
ansible_account_prefer = 'root,Administrator'
def __init__(self, execution): def __init__(self, execution):
self.execution = execution self.execution = execution
@ -123,6 +124,7 @@ class BasePlaybookManager:
def generate_inventory(self, platformed_assets, inventory_path): def generate_inventory(self, platformed_assets, inventory_path):
inventory = JMSInventory( inventory = JMSInventory(
assets=platformed_assets, assets=platformed_assets,
account_prefer=self.ansible_account_prefer,
account_policy=self.ansible_account_policy, account_policy=self.ansible_account_policy,
host_callback=self.host_callback, host_callback=self.host_callback,
) )

View File

@ -14,7 +14,7 @@ __all__ = ['JobViewSet', 'JobExecutionViewSet', 'JobRunVariableHelpAPIView',
from ops.tasks import run_ops_job_execution from ops.tasks import run_ops_job_execution
from ops.variables import JMS_JOB_VARIABLE_HELP from ops.variables import JMS_JOB_VARIABLE_HELP
from orgs.mixins.api import OrgBulkModelViewSet from orgs.mixins.api import OrgBulkModelViewSet
from orgs.utils import tmp_to_org, get_current_org_id, get_current_org from orgs.utils import tmp_to_org, get_current_org
from accounts.models import Account from accounts.models import Account