stderr로 "A subdirectory or file C:\\test already exists.\r\n" 이 발생합니다.
이러한 command는 멱등성의 단위가 아닌 별개의 작업으로 간주됩니다.
한가지 더 살펴볼 것은 앞서 살펴본 예제에서도 있던 register입니다. register 를 whoami.exe 결과를 받아오는데 사용될 수도 있습니다. 예제를 조금 수정했습니다.
[root@labco7ans ansible]# cat 07.command2.yml
---
- hosts: win
gather_facts: no
tasks:
- name: run an executable using win_command
win_command: whoami.exe
register: output
- debug: msg="{{ output.stdout }}"
[root@labco7ans ansible]# ansible-playbook -i hosts 07.command2.yml
PLAY [win] ***********************************************************************************************************************************************************
TASK [run an executable using win_command] ***************************************************************************************************************************
changed: [172.16.3.106]
TASK [debug] *********************************************************************************************************************************************************
ok: [172.16.3.106] => {
"msg": "labw16ans\\administrator\r\n"
}
PLAY RECAP ***********************************************************************************************************************************************************
172.16.3.106 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
(7) 환경 변수 설정
시스템 환경변수를 추가하는 시나리오 입니다.
[root@labco7ans ansible]# cat 08.env.yml
---
- hosts: win
gather_facts: no
tasks:
- name: Set an environment variable for all users
win_environment:
state: present
name: NewVariable
value: New Value
level: machine
[root@labco7ans ansible]# ansible-playbook -i hosts 08.env.yml
PLAY [win] ***********************************************************************************************************************************************************
TASK [Set an environment variable for all users] *********************************************************************************************************************
changed: [172.16.3.106]
PLAY RECAP ***********************************************************************************************************************************************************
172.16.3.106 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
아래와 같이 생성되었습니다.
C:\Users\User1>set |findstr New
NewVariable=New Value
Windows 서버의 환경변수는 시스템 환경변수와 사용자 환경변수로 나뉩니다. 이를 level로 정의합니다. 하기 참고하시기 바랍니다.
공식문서의 Notes에서도 언급된 내용은, 이 module 자체가 변경사항을 전파하지 않는다는 점입니다(This module does not broadcast change events). 그러하므로 변경사항을 세션이나 시스템에 반영하기 위해 새로운 세션을 생성하거나 reboot이 필요할 수 있습니다.
추가로 확인해보니 performance counter 관련된 모듈은 없습니다. 이는 모니터링에서 보는게 효과적일 것 같습니다.
다른 방법은 win_command 모듈로 wmic 명령을 전달 할 수도 있습니다.
---
- hosts: win
tasks:
- name: run wmic command
win_command: wmic cpu get caption, deviceid, name, numberofcores, maxclockspeed, status
register: usage
- debug: msg="{{ usage.stdout }}"
참고로 앞선 win_ping 결과의 [Gathering Facts]를 보면 알 수 있듯이 playbook을 실행하면 기본적으로 ansible_facts를 사용해 서버 정보를 수집합니다. 이러한 정보를 활용을 하지 않는 경우 facts를 수집하지 않으므로써 수행 속도를 빠르게 할 수 있습니다. gather_facts: no 로 facts 수집을 생략할 수 있습니다.
여러 대의 서버를 AD join 해야하는 상황은 접속>정보 입력>인증>OS reboot>확인이 반복되야 합니다.
아래의 VM을 Domain에 Join하고 세팅하는 예시를 들어보겠습니다.
PS C:\Users\Administrator> Get-CimInstance Win32_ComputerSystem
Name PrimaryOwnerName Domain TotalPhysicalMemory Model Manufacturer
---- ---------------- ------ ------------------- ----- ------------
WIN-VS4UITP6P9O Windows User WORKGROUP 8588902400 Virtual Machine Microsoft Corpor...
inventory 파일의 win_ad에 여러대의 IP가 등록되었다면 다수 서버의 설정을 한번에 할 수 있습니다.
TASK [win_reboot] 단계에서 서버의 재시작이 수행되고, 완료가 되면 접속이 됩니다.
아래와 같이 AD 조인과 hostname 변경된 것을 확인 할 수 있습니다.
PS C:\Users\administrator.WINLAB> Get-CimInstance Win32_ComputerSystem
Name PrimaryOwnerName Domain TotalPhysicalMemory Model Manufacturer
---- ---------------- ------ ------------------- ----- ------------
LABW16ANSAD Windows User winlab.citec.com 8588902400 Virtual Machine Microsoft Corpor...
User & Group 관리
아래는 로컬 계정 및 그룹을 관리하는 시나리오 입니다.
[root@labco7ans ansible]# cat 02.user_group.yml
- hosts: win
gather_facts: no
tasks:
- name: Create local group to contain new users
win_group:
name: LocalGroup
description: LocalGroup
- name: Create local user
win_user:
name: '{{ item.name }}'
password: '{{ item.password }}'
groups: LocalGroup, Remote Desktop Users
password_never_expires: yes
loop:
- name: User1
password: Password1
- name: User2
password: Password2
[root@labco7ans ansible]# ansible-playbook -i hosts 02.user_group.yml
PLAY [win] ***********************************************************************************************************************************************************
TASK [Create local group to contain new users] ***********************************************************************************************************************
ok: [172.16.3.106]
TASK [Create local user] *********************************************************************************************************************************************
changed: [172.16.3.106] => (item={u'password': u'Password1', u'name': u'User1'})
changed: [172.16.3.106] => (item={u'password': u'Password2', u'name': u'User2'})
PLAY RECAP ***********************************************************************************************************************************************************
172.16.3.106 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
(3) feature 관리
서버에 Feature를 관리하는 시나리오 입니다.
[root@labco7ans ansible]# cat 03.feature.yml
- hosts: win
gather_facts: no
tasks:
- name: Install IIS Web-Server with sub features and management tools
win_feature:
name: Web-Server
state: present
include_sub_features: yes
include_management_tools: yes
register: win_feature
- name: Reboot if installing Web-Server feature requires it
win_reboot:
when: win_feature.reboot_required
[root@labco7ans ansible]# ansible-playbook -i hosts 03.feature.yml
PLAY [win] ***********************************************************************************************************************************************************
TASK [Install IIS Web-Server with sub features and management tools] *************************************************************************************************
changed: [172.16.3.106]
TASK [Reboot if installing Web-Server feature requires it] ***********************************************************************************************************
skipping: [172.16.3.106]
PLAY RECAP ***********************************************************************************************************************************************************
172.16.3.106 : ok=1 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
docs.ansible.com의 Parameters를 확인하는 TIP 1) Parameter 설명을 보면 어느 버전에서 사용가능한지(ex. addded in 2.8)와 필수값(required)인지 등을 알 수 있습니다. 2) Choices/Default 값을 알 수 있습니다. 파란색 글자로 작성된 값이 default이므로 명시하지 않은 경우 적용됩니다.
Docker는 기본적으로 컨테이너의 네트워크 네임스페이스를 호스트의 runtime data에 추가하지 않는다 (/run의 tmpfs로 마운트 되는 /var/run을 의미함)
실행된 컨테이너의 pid로 심볼릭 링크를 만들어주면 컨테이너의 네트워크 네임스페이스를 확인 할 수 있다.
# 호스트 네트워크
root@docker1:~# ip -br -c a
lo UNKNOWN 127.0.0.1/8 ::1/128
enp0s3 UP 10.0.2.15/24 fe80::31:14ff:fe45:3203/64
enp0s8 UP 192.168.50.10/24 fe80::a00:27ff:fe7c:22c3/64
docker0 UP 172.17.0.1/16 fe80::42:23ff:fef8:ad75/64
vethca6cbca@if7 UP fe80::e07c:f6ff:fe2c:6d25/64
root@docker1:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
71118483fd2e ubuntu "bash" 9 minutes ago Up 9 minutes sharp_panini
root@docker1:~# ip netns list # 컨테이너가 실행 중인데 네트워크 네임스페이스가 조회되지 않는다.
root@docker1:~# pid="$(docker inspect -f '{{.State.Pid}}' "711184" )"
root@docker1:~# echo $pid
8719
root@docker1:~# ls -l /var/run/netns
total 0
root@docker1:~# ln -s /proc/$pid/ns/net /var/run/netns/ubuntu
root@docker1:~# ip netns list
ubuntu (id: 0)
root@docker1:~# ip netns exec ubuntu ip -br -c a
lo UNKNOWN 127.0.0.1/8
eth0@if8 UP 172.17.0.2/16
root@docker1:~#