Implementación e implementación de Ansible de PlayBook
nombre de la computadora | IP | Role |
---|---|---|
puesto de trabajo | 192.168.182.130 | administrar |
atender | 192.168.182.131 | administrado |
servidorb | 192.168.182.132 | administrado |
servidorc | 192.168.182.133 | administrado |
desplegar
nodo de control
Ansible es fácil de instalar. Sólo es necesario instalarlo en el nodo o nodos de control en los que se ejecutará. Los hosts administrados por Ansbile no requieren la instalación de Ansible.
El nodo de control necesita instalar Python3 (superior a 3.5) o python2 (superior a 2.7)
yum install python36 -y
yum install epel-release -y
yum install ansible -y
#使用setup模块验证版本
[root@workstation ~]# ansible -m setup localhost | grep ansible_python_version
"ansible_python_version": "2.7.5",
host administrado
yum install python36 -y
Construir manifiesto
lista de definiciones
El inventario define un conjunto de hosts que Ansible administrará. Estos hosts también se pueden asignar a grupos para una gestión centralizada. Los grupos pueden contener subgrupos y los anfitriones pueden ser miembros de varios grupos. El manifiesto también puede establecer variables de aplicación con los hosts y grupos que define.
La lista estática especifica hosts administrados
Un archivo de inventario estático es un archivo de texto (INI/YAML) que especifica el host administrado de destino de ansible.
artículos relacionados con ansibles
Configurar archivos de activosinventory.ini
[root@workstation ~]# cat inventory.ini
[my_servers]
servera
serverb
serverc
Resolución de nombre de host
[root@workstation ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.182.131 servera
192.168.182.132 serverb
192.168.182.133 serverc
Implementar el libro de jugadas
Ejecutar libro de jugadas
El comando ansible-playbook se puede utilizar para ejecutar playboo. Este comando se ejecuta en el nodo de control, pasando el nombre del libro de jugadas a ejecutar como argumento.
Ejemplo
[root@workstation ~]# cat webserver.yml
---
- name: setup web server
hosts: my_servers
tasks:
- name: latest httpd
yum:
name: httpd
state: latest
Instalación de Servera en my_server
[root@workstation ~]# ansible-playbook -i inventory.ini webserver.yml --limit servera
PLAY [setup web server] ************************************************************************************
TASK [Gathering Facts] *************************************************************************************
ok: [servera]
TASK [latest httpd] ****************************************************************************************
changed: [servera]
PLAY RECAP *************************************************************************************************
servera : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
examinar
[root@servera ~]# systemctl start httpd
[root@servera ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2023-07-19 06:24:23 EDT; 53s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 15578 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─15578 /usr/sbin/httpd -DFOREGROUND
├─15579 /usr/sbin/httpd -DFOREGROUND
├─15580 /usr/sbin/httpd -DFOREGROUND
├─15581 /usr/sbin/httpd -DFOREGROUND
├─15582 /usr/sbin/httpd -DFOREGROUND
└─15583 /usr/sbin/httpd -DFOREGROUND
Jul 19 06:24:23 servera systemd[1]: Starting The Apache HTTP Server...
Jul 19 06:24:23 servera httpd[15578]: AH00558: httpd: Could not reliably determine the server's fully...sage
Jul 19 06:24:23 servera systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
Antes de ejecutar el playbook, es mejor verificar para asegurarse de que la prevención de contenido sea correcta.
[root@workstation ~]# ansible-playbook --syntax-check webserver.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does
not match 'all'
[WARNING]: Could not match supplied host pattern, ignoring: my_servers
playbook: webserver.yml
Esto demuestra que el formato es correcto.
Revisarwebserver.yml
[root@workstation ~]# cat webserver.yml
---
- name: setup web server
hosts: my_servers
tasks:
- name:latest httpd
yum:
name: httpd
state: latest
[root@workstation ~]# ansible-playbook --syntax-check webserver.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does
not match 'all'
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: No JSON object could be decoded
Syntax Error while loading YAML.
mapping values are not allowed in this context
The error appears to be in '/root/webserver.yml': line 6, column 8, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name:latest httpd
yum:
^ here
Explica que hay un problema.
La opción -C realiza un ensayo del libro de jugadas. Hace que Ansible informe qué cambios ocurrirán cuando se ejecute el libro de jugadas y no realice ningún cambio en los hosts administrados.
[root@workstation ~]# ansible-playbook -C webserver.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does
not match 'all'
[WARNING]: Could not match supplied host pattern, ignoring: my_servers
PLAY [setup web server] ************************************************************************************
skipping: no hosts matched
PLAY RECAP *************************************************************************************************