Queremos ejecutar el siguiente libro de jugadas con el archivo yml : ansible-playbook install_os.yml
El libro de playbook install_os.yml está funcionando bien, pero ahora queremos agregar la validación de los argumentos <machine name>,<machine IP> .
Como el seguiente:
ansible-playbook install_os.yml --limit RHEL01,73.22.3.44 Desde mi punto, ambos argumentos deben identificarse como cadenas (sin verificar la IP válida) y entre <machine name> a <machine IP> , debemos establecer , separador
Entonces, ¿es posible validar las cadenas? ¿Y salir si uno de ellos o ambos no están definidos?
Puede acceder al límite especificado en los argumentos de ansible-playbook con la ayuda de la variable especial ansible_limit .
A partir de ahí, puede assert los valores --limit en función de las necesidades de su negocio.
Por ejemplo:
- hosts: all gather_facts: no tasks: - assert: that: ## We have exactly one comma, separating two hosts - ansible_limit | split(',') | length == 2 ## We have a string before the comma - (ansible_limit | split(',')).0 is string ## We have a non-empty string before the comma - (ansible_limit | split(',')).0 | length > 0 ## We have a string after the comma - (ansible_limit | split(',')).1 is string ## We have a non-empty string after the comma - (ansible_limit | split(',')).1 | length > 0 ## 'all', which has a wildcard meaning, ## is not one of the two hosts separated by the comma - "'all' not in ansible_limit | split(',')" ## We do not have any character having a special meaning in the limit ## see: https://docs.ansible.com/ansible/latest/user_guide/intro_patterns.html#common-patterns - "'@' not in ansible_limit" - "':' not in ansible_limit" - "'!' not in ansible_limit" - "'&' not in ansible_limit" - "'*' not in ansible_limit" run_once: true Esto probablemente lo limitaría al caso de uso que desee.
Dicho esto, tenga en cuenta que --limit es un indicador existente con su propio comportamiento, por lo que, en función de lo que busca, también podría ser mejor con un parámetro adicional pasado en la línea de comandos .