Sou muito novo no Ansible, então posso configurar coisas erradas
[tenho um contêiner Docker executando o serviço Ansible no CentOS8.
Tenho um repositório Ansible que inclui os arquivos Ansible (este é um repositório .Git]
Minha vontade era reverter automaticamente cada laboratório (o laboratório é composto por 8 vms, 5 windows server 2016 e 3 windows 10. O DC inclui política para habilitar winrm nessas máquinas) no vCenter server para um snapshot específico. Mas primeiro estou tentando: uma vez desligar as máquinas do laboratório quando elas estiverem ligadas e uma vezligar as máquinas do laboratório quando elas estiverem desligadas
Então, eu (com a ajuda deguia ansible-roles-explicado-com-exemplos):
- Criou uma função com
ansible-galaxy init
nome de comandovcenter
(veja a árvore de diretórios abaixo) - Criados alguns arquivos de tarefas do vcenter dentro
tasks
da pasta (veja a árvore de diretórios abaixo). Aqui está um exemplo de arquivospoweroff.yml
depoweron.yml
tarefas:
- name: Set the state of a virtual machine to poweroff
community.vmware.vmware_guest_powerstate:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
folder: "/{{ datacenter_name }}/vm/{{ folder }}"
name: "{{ ansible_hostname }}"
# name: "{{ guest_name }}"
validate_certs: no
state: powered-off
force: yes
delegate_to: localhost
register: deploy
- name: Set the state of a virtual machine to poweron using MoID
community.vmware.vmware_guest_powerstate:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
folder: "/{{ datacenter_name }}/vm/{{ folder }}"
name: "{{ ansible_hostname }}"
# moid: vm-42
validate_certs: no
state: powered-on
delegate_to: localhost
register: deploy
- Credenciais do vCenter fornecidas em
vcenter\vars\main.yml
arquivo, assim:
# vars file for vcenter
vcenter_hostname: vcenter.foo.com
vcenter_username: [email protected]
vcenter_password: f#0$o#1$0o
datacenter_name: FOO_Fighters
# datastore_name:
cluster_name: FOO
folder: '/FOO/PRODUCT/DOMAIN.COM/'
- Incluídas as tarefas em
tasks\main.yml
arquivo comimport-task
chave, assim:
---
# tasks file for roles/vcenter
- import_tasks: poweroff.yml
# - import_tasks: poweron.yml
# - import_tasks: revert.yml
# - import_tasks: shutdown.yml
- Criei uma
all.yml
pasta group_vars interna na biblioteca de inventários (não sei se é uma maneira profissional de fazer isso) que inclui todos os detalhes do winrm como este:
---
#WinRM Protocol Details
ansible_user: DOMAIN\user
ansible_password: f#0$o#1$0o
ansible_connection: winrm
ansible_port: 5985
ansible_winrm_scheme: http
ansible_winrm_server_cert_validation: ignore
ansible_winrm_transport: ntlm
ansible_winrm_read_timeout_sec: 60
ansible_winrm_operation_timeout_sec: 58
- Criou um
revert_lab.yml
manual que inclui a função, como este
---
- name: revert an onpremis lab
hosts: all
roles:
- vcenter
O meu ansible.cfg
é assim:
[defaults]
inventory = /ansible/inventories
roles_path = ./roles:..~/ansible/roles
Executei o playbook com sucesso para desligar todas as máquinas do laboratório, depois "liguei" a tarefa poweron na função, assim:
---
# tasks file for roles/vcenter
# - import_tasks: poweroff.yml
- import_tasks: poweron.yml
# - import_tasks: revert.yml
# - import_tasks: shutdown.yml
Agora que todas as máquinas do laboratório estão desligadas, executando o playbook, ocorre o seguinte erro:
PLAY [revert vmware vcenter lab] *************************************************
TASK [Gathering Facts] ***********************************************************
fatal: [vm1.domain.com]: UNREACHABLE! => {"changed": false, "msg": "ntlm:
HTTPConnectionPool(host='vm1.domain.com', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb7ae4908d0>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}
fatal: [vm2.domain.com]: UNREACHABLE! => {"changed": false, "msg": "ntlm: HTTPConnectionPool(host='vm2.domain.com', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb7ae487b00>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}
fatal: [vm3.domain.com]: UNREACHABLE! => {"changed": false, "msg": "ntlm: HTTPConnectionPool(host='vm3.domain.com', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb7ae48acc0>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}
fatal: [vm4.domain.com]: UNREACHABLE! => {"changed": false, "msg": "ntlm: HTTPConnectionPool(host='vm4.domain.com', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb7ae48de80>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}
fatal: [vm5.domain.com]: UNREACHABLE! => {"changed": false, "msg": "ntlm:
HTTPConnectionPool(host='vm5.domain.com', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb7ae41f080>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}
fatal: [vm6.domain.com]: UNREACHABLE! => {"changed": false, "msg": "ntlm: HTTPConnectionPool(host='vm6.domain.com', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb7ae41d7f0>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}
fatal: [vm7.domain.com]: UNREACHABLE! => {"changed": false, "msg": "ntlm: HTTPConnectionPool(host='vm7.domain.com', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb7ae428048>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}
fatal: [vm8.domain.com]: UNREACHABLE! => {"changed": false, "msg": "ntlm: HTTPConnectionPool(host='vm8.domain.com', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb7ae425588>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}
PLAY RECAP ***********************************************************************
vm1.domain.com : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
vm2.domain.com : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
vm3.domain.com : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
vm4.domain.com : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
vm5.domain.com : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
vm6.domain.com : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
vm7.domain.com : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
vm8.domain.com : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
Por que a tarefa de desligamento funciona bem e a inicialização não? Como posso corrigir esse problema?
Meu repositório:
C:.
├───ansible
│ │ ansible.cfg
│ ├───inventories
│ │ └───test
│ │ ├───cloud
│ │ └───onpremis
│ │ └───domain.com
│ │ │ lab_j.yml
│ │ │ lab_r.yml
│ │ └───group_vars
│ │ all.yml
│ ├───playbooks
│ │ revert_lab.yml
│ └───roles
│ └───vcenter
│ ├───tasks
│ │ main.yml
│ │ poweroff.yml
│ │ poweron.yml
│ │ revert.yml
│ │ shutdown.yml
│ └───vars
│ main.yml
Meu inventário lab_r.yml
- este é um esquema parcial
---
all:
children:
root:
children:
center:
children:
appservers:
hosts:
vm1.domain.com:
qservers:
hosts:
vm2.domain.com:
dbservers:
hosts:
vm3.domain.com:
Responder1
O problema foi resolvido por:
defini folder
o valor-chave como "/{{ datacenter_name }}/"
adiciono a poweron
tarefa a uma tarefa adicional como revert
- o que significa que poweron
a tarefa funcionou para mim apenas se fizesse parte de uma sequência de tarefas
Infelizmente, a poweron
tarefa não funcionou para mim como uma tarefa independente