Default function treating variable as a string.

31 views
Skip to first unread message

farrukh ahmed

unread,
Aug 19, 2022, 8:15:51 AM8/19/22
to ansible...@googlegroups.com
Hello Team,

Hope you all are doing well.
Here is  a case:

### Templates/php_app.conf.j2
;# MANAGED BLOCK ###
php_admin_flag[display_errors] = {{server_config.display_errors}}
php_admin_value[error_log] = {{ server_config.error_log | default({"error_log":"{{base_directory_path }}/{{app_name}}/logs/php-fpm_error.log"}) }}
php_admin_flag[log_errors] = {{server_config.log_errors}}
php_admin_flag[short_open_tag] = {{server_config.short_open_tag}}
php_admin_value[memory_limit] = {{server_config.memory_limit}}
php_admin_value[post_max_size] = {{server_config.post_max_size}}
php_admin_value[upload_max_filesize] = {{server_config.upload_max_filesize}}
php_admin_value[max_input_time] = {{server_config.max_input_time}}
php_admin_value[max_execution_time] = {{server_config.max_execution_time}}
php_admin_value[max_file_uploads] = {{server_config.max_file_uploads}}
php_admin_value[max_input_vars] = {{server_config.max_input_vars}}
php_admin_value[date.timezone] = {{server_config.time_zone}}
;# MANAGED BLOCK ###
####


### Tasks/main.yml
- name: Copy template of PHP configuration
template:
src: "../templates/php_app.conf.j2"
dest: "{{ php_app_configs_path }}/{{ app_name }}.conf"
backup: yes
#####

### Defaults/main/yml
php_app_configs_path: "/home/farrukh/php"

server_config: {
display_errors: "On",
error_log: "{{ base_directory_path }}/{{ app_name }}/logs/php-fpm_error.log",
log_errors: "On",
short_open_tag: "Off",
memory_limit: 128M,
post_max_size: 8M,
upload_max_filesize: 2M,
max_input_time: 60,
max_execution_time: 30,
max_file_uploads: 20,
max_input_vars: 1000,
time_zone: UTC
}
####

Here is the command I ran:
ansible-playbook php_config.yml -i hosts -e '{"app_name":"test-80php","base_directory_path":"/var/www/html/applications/test-80php","server_config":{"display_errors":"Off","log_errors":"On","memory_limit":"200M","post_max_size":"8M","upload_max_filesize":"8M","max_input_time":"60","max_execution_time":"30","max_file_uploads":"20","max_input_vars":"2000","short_open_tag":"On","time_zone":"Asia/Karachi"}}' -vvv
After Running the command, the template file output. It is treated as a string but not as a variable.
For instance, If I don't pass the variable "error_log" variable from CLI, its value should put the value in the template file as the variables defined, should be like i.e(php_admin_value[error_log] = /var/www/html/applications/test-80php/logs/php-fpm_error.log) in the template file

But it is treated as the string like the below code:
;# MANAGED BLOCK ###
php_admin_flag[display_errors] = Off
php_admin_value[error_log] = {'error_log': '{{base_directory_path }}/{{app_name}}/logs/php-fpm_error.log'}
php_admin_flag[log_errors] = On
php_admin_flag[short_open_tag] = Off
php_admin_value[memory_limit] = 200M
php_admin_value[post_max_size] = 8M
php_admin_value[upload_max_filesize] = 8M
php_admin_value[max_input_time] = 120
php_admin_value[max_execution_time] = 30
php_admin_value[max_file_uploads] = 60
php_admin_value[max_input_vars] = 2000
php_admin_value[date.timezone] = Asia/Islamabad
;# MANAGED BLOCK ###

Am I doing it the right way? Any help or solution will be appreciated

Thanks

Farrukh Ahmed.

Rowe, Walter P. (Fed)

unread,
Aug 19, 2022, 8:22:03 AM8/19/22
to ansible...@googlegroups.com
You need one more "}" on your -e string to close out the server_config.


ansible-playbook php_config.yml -i hosts -e '{"app_name":"test-80php","base_directory_path":"/var/www/html/applications/test-80php","server_config":{ "display_errors":"Off", "log_errors":"On", "memory_limit":"200M", "post_max_size":"8M", "upload_max_filesize":"8M", "max_input_time":"60", "max_execution_time":"30", "max_file_uploads":"20", "max_input_vars":"2000", "short_open_tag":"On", "time_zone":"Asia/Karachi"} }}' -vvv
                   ------------------------------^

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

Walter Rowe

unread,
Aug 19, 2022, 8:37:24 AM8/19/22
to Ansible Project
That clearly didn't display correctly ...

You need three close braces at the end of your "-e" extra vars param.

} }}' -vvv

Hope that helps.

farrukh ahmed

unread,
Aug 20, 2022, 10:32:46 AM8/20/22
to ansible...@googlegroups.com
Here is the command previously used, I hope it is clearly visible.
ansible-playbook php_config.yml -i hosts -e '{"app_name":"test-80php",
"base_directory_path":"/var/www/html/applications/test-80php",
"server_config":{"display_errors":"Off",
"log_errors":"On",
"memory_limit":"200M",
"post_max_size":"8M",
"upload_max_filesize":"8M",
"max_input_time":"60",
"max_execution_time":"30",
"max_file_uploads":"20",
"max_input_vars":"2000",
"short_open_tag":"On",
"time_zone":"Asia/Karachi"}}' -vvv

Walter, I have tried adding more braces, but unfortunately, it didn't work for me.
The number of openings and closings braces is the same.

Regards,

Farrukh Ahmed
 

Todd Lewis

unread,
Aug 20, 2022, 12:30:26 PM8/20/22
to ansible...@googlegroups.com
Mustaches do not nest. The "error_log" line in templates/php_app.conf.j2 should be

php_admin_value[error_log] = {{ server_config.error_log | default(base_directory_path ~ "/" ~ "app_name/logs/php-fpm_error.log") }}

farrukh ahmed

unread,
Aug 22, 2022, 3:04:02 AM8/22/22
to Ansible Project
Thanks, Lewis for the solution. It actually worked. But would you mind explaining or sharing the documentation of what you have done above inside the "default()"?

I actually want to learn and understand this.


Thanks & regards,

Farrukh Ahmed
Reply all
Reply to author
Forward
0 new messages