본문 바로가기
카테고리 없음

[ansible] 앤서블 windows 계정 현황 playbook

by IT맥구리나스 2023. 2. 14.

개요

ansible을 통해 윈도우즈 계정 현황을 파악할 수있다.

 

세부내용

gathering_facts를 통해 서버정보 수집 내역을 통해  윈도우 OS별 계정현황을 확인할 수 있다.

주석처리해놓은 name이  printos를 통해 서버 os 정보를 확인할 수 잇음

 

아래는 windows 2012와 window 2012가 아닌 버전으로 나눠 놨는데 2012는 powershell을 통한 계정확인이 불가능하다.

그래서 cmd를 통한 확인방법과 powershell을 통한 확인 방법을 나눠서 만들어봣다.

 

when 구문에서

"문자" in ansible_distribution 은 문자를 포함 하면 참

ansible_distribution  in ["문자1","문자2"]은 문자1이거나 문자2이면 참

---
- name: print net user
  hosts: all
  vars:
     #ansible_user: infra
     #nasible_password: 
     ansible_connection: winrm
     ansible_winrm_server_cert_validation: ignore
     ansible_winrm_scheme: http
     ansible_become: false
     ansible_winrm_transport: basic
     ansible_port: 5985
  tasks:
    #- name: printos
    #  debug:
    #    var: ansible_distribution

    - name: extract
      win_shell: powershell "Get-LocalUser | select name"
      no_log: false
      #win_shell: net user
      args:
        executable: cmd
      register: result
      ignore_errors: true
      #when: ansible_distribution not in ["Microsoft Windows Server 2012 R2 Standard","Microsoft Windows Server 2012 R2 Datacenter"]
      when: "'Microsoft Windows Server 2012 R2' not in ansible_distribution"

    - name: user list result
      debug:
        msg: "{{ inventory_hostname }}: {{ result.stdout_lines }}"
      #when: ansible_distribution not in ["Microsoft Windows Server 2012 R2 Standard","Microsoft Windows Server 2012 R2 Datacenter"]
      when: "'Microsoft Windows Server 2012 R2' not in ansible_distribution"

    - name: extract2
      win_shell: wmic useraccount get name
      args:
        executable: cmd
      register: result2
      #when: ansible_distribution in ["Microsoft Windows Server 2012 R2 Standard","Microsoft Windows Server 2012 R2 Datacenter"]
      when: "'Microsoft Windows Server 2012 R2' in ansible_distribution"

    - name: user list result2
      debug:
        msg: "{{ inventory_hostname }}: {{ result2.stdout_lines }}"
      when: "'Microsoft Windows Server 2012 R2' in ansible_distribution"
반응형

댓글