- name: "Ensure configuration.php DB settings match inventory" command: argv: - docker - exec - -e - J_DBTYPE={{ JOOMLA_DB_CONNECTOR }} - -e - J_DBHOST={{ database_host }}:{{ database_port }} - -e - J_DBUSER={{ database_username }} - -e - J_DBPASS={{ database_password }} - -e - J_DBNAME={{ database_name }} - "{{ JOOMLA_CONTAINER }}" - php - -r - | $f = '{{ JOOMLA_CONFIG_FILE }}'; if (!file_exists($f)) { exit(0); } $c = file_get_contents($f); $changed = 0; $map = [ 'dbtype' => getenv('J_DBTYPE'), 'host' => getenv('J_DBHOST'), 'user' => getenv('J_DBUSER'), 'password' => getenv('J_DBPASS'), 'db' => getenv('J_DBNAME'), ]; foreach ($map as $k => $v) { // Escape single quotes for safe embedding into the PHP source string $vEsc = str_replace("'", "\\'", $v); // Match current value in config: public $key = '...'; if (preg_match("/public \\$".$k."\\s*=\\s*'([^']*)';/", $c, $m) && $m[1] !== $v) { $c = preg_replace( "/public \\$".$k."\\s*=\\s*'[^']*';/", "public $".$k." = '".$vEsc."';", $c ); $changed = 1; } } if ($changed) { file_put_contents($f, $c); echo "changed"; } else { echo "ok"; } register: cfg_patch changed_when: cfg_patch.stdout == "changed" failed_when: cfg_patch.rc != 0 when: joomla_installed.rc == 0