042 Filters dict_to_ast and ast_to_nginx
Use case
Use filter vbotka.freebsd.dict_to_ast and filter vbotka.freebsd.ast_to_nginx to create Nginx configuration.
Tree
shell> tree .
.
├── ansible.cfg
├── pb-test-ast.yml
└── pb-test-nginx.yml
Synopsis
In the playbook
pb-test-ast.ymluse the filter vbotka.freebsd.dict_to_ast to convert YAML dictionary into a crossplane-compatible AST (Abstract Syntax Tree) list for NGINX configuration generation.In the playbook
pb-test-nginx.ymluse the filter vbotka.freebsd.ast_to_nginx to convert crossplane AST (Abstract Syntax Tree) list into NGINX config.
Requirements
filter vbotka.freebsd.dict_to_ast
filter vbotka.freebsd.ast_to_nginx
Notes
The filter ast_to_nginx requires package crossplane.
See also
ansible.cfg
[defaults]
callback_result_format = yaml
deprecation_warnings = false
display_skipped_hosts = false
gathering = explicit
interpreter_python = auto_silent
log_path = /var/log/ansible.log
[connection]
pipelining = true
Playbook pb-test-ast.yml
- name: Test filter dict_to_ast
hosts: localhost
connection: local
vars:
nginx_conf:
worker_processes: ['auto']
events:
worker_connections: ['1024']
http:
include: ['/etc/nginx/mime.types']
server:
listen: ['80']
server_name: ['example.com', 'www.example.com']
location /:
return: ['200', 'Hello from Crossplane!']
tasks:
- debug:
msg: |
ast:
{{ nginx_conf | vbotka.freebsd.dict_to_ast
| to_yaml(indent=2) | indent(2) }}
Playbook output - Test filter dict_to_ast
(env) > ansible-playbook -i localhost, pb-test-ast.yml
PLAY [Test filter dict_to_ast] *************************************************
TASK [debug] *******************************************************************
ok: [localhost] =>
msg: |-
ast:
- args: [auto]
directive: worker_processes
- args: []
block:
- args: ['1024']
directive: worker_connections
directive: events
- args: []
block:
- args: [/etc/nginx/mime.types]
directive: include
- args: []
block:
- args: ['80']
directive: listen
- args: [example.com, www.example.com]
directive: server_name
- args: [/]
block:
- args: ['200', Hello from Crossplane!]
directive: return
directive: location
directive: server
directive: http
PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Playbook pb-test-nginx.yml
- name: Test filter ast_to_nginx
hosts: localhost
connection: local
vars:
nginx_conf:
worker_processes: ['auto']
events:
worker_connections: ['1024']
http:
include: ['/etc/nginx/mime.types']
server:
listen: ['80']
server_name: ['example.com', 'www.example.com']
location /:
return: ['200', 'Hello from Crossplane!']
tasks:
- debug:
msg: |
{{ nginx_conf | vbotka.freebsd.dict_to_ast
| vbotka.freebsd.ast_to_nginx }}
Playbook output - Test filter ast_to_nginx
(env) > ansible-playbook -i localhost, pb-test-nginx.yml
PLAY [Test filter ast_to_nginx] ************************************************
TASK [debug] *******************************************************************
ok: [localhost] =>
msg: |-
worker_processes auto;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
server {
listen 80;
server_name example.com www.example.com;
location / {
return 200 'Hello from Crossplane!';
}
}
}
PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0