如何用正则提取WordPress wp-config数据库信息并通过Ansible自动化更新虚拟主机
提取WordPress数据库信息的正则表达式及Ansible剧本问题解决
问题描述
各位技术同仁:
我花费5小时调试正则表达式仍未解决问题,特此求助。需要一个正则表达式提取WordPress的数据库信息,用于Ansible剧本中。
目标格式示例:
/** The host of the database for WordPress */ define( 'DB_HOST', 'database_host_here' );
我尝试过ChatGPT等AI工具生成的多个正则表达式,均因转义问题失败。以下是当前使用的Ansible剧本:
--- - name: Update wp-config.php with new template hosts: localhost vars_prompt: - name: username prompt: "Enter the username" private: false vars: wp_config_filename: "wp-config.php" enable_debug: false multi_site: false search_paths: - "/home/{{ username }}/public_html/public" tasks: - name: Find wp-config.php files ansible.builtin.find: paths: "{{ search_paths }}" patterns: "{{ wp_config_filename }}" register: wp_config_files - name: Fail if no wp-config.php files found ansible.builtin.fail: msg: "No wp-config.php files found in the provided paths." when: wp_config_files.matched == 0 - name: Debug wp-config.php files ansible.builtin.debug: var: wp_config_files - name: Read existing wp-config.php for database credentials ansible.builtin.slurp: src: "{{ item.path }}" loop: "{{ wp_config_files.files }}" loop_control: loop_var: item register: wp_config_content when: wp_config_files.matched > 0 - name: Decode base64 encoded wp-config.php content ansible.builtin.set_fact: wp_config_decoded: "{{ wp_config_content.results | map(attribute='content') | map('b64decode') | list }}" - name: Debug decoded wp-config.php content ansible.builtin.debug: var: wp_config_decoded - name: Set database credentials from existing wp-config.php ansible.builtin.set_fact: db_host: "{{ item | regex_search('define\\(\\s*[\'\\\"]DB_HOST[\'\\\"]\\s*,\\s*[\'\\\"]([^\'\\\"]+)[\'\\\"]') | default('localhost') }}" db_name: "{{ item | regex_search('define\\(\\s*[\'\\\"]DB_NAME[\'\\\"]\\s*,\\s*[\'\\\"]([^\'\\\"]+)[\'\\\"]') | default('wordpress') }}" db_user: "{{ item | regex_search('define\\(\\s*[\'\\\"]DB_USER[\'\\\"]\\s*,\\s*[\'\\\"]([^\'\\\"]+)[\'\\\"]') | default('root') }}" db_password: "{{ item | regex_search('define\\(\\s*[\'\\\"]DB_PASSWORD[\'\\\"]\\s*,\\s*[\'\\\"]([^\'\\\"]+)[\'\\\"]') | default('password') }}" loop: "{{ wp_config_decoded }}" loop_control: loop_var: item - name: Debug extracted database credentials ansible.builtin.debug: msg: > DB Name: {{ db_name }}, DB User: {{ db_user }}, DB Password: {{ db_password }}, DB Host: {{ db_host }} - name: Create a new wp-config.php from template ansible.builtin.template: src: "wp-config.php.j2" dest: "{{ item.path }}" mode: "0644" loop: "{{ wp_config_files.files }}" when: wp_config_files.matched > 0
我的应用场景是在虚拟主机环境下管理多个WordPress站点,需要自动化检测数据库信息、更新模板并部署到正确路径。最初使用的正则表达式:
define\(\s*[\'"]DB_HOST[\'"]\s*,\s*[\'"]([^\'"]+)[\'"]\)
该正则在Ansible中执行失败。
更新:解决方案
感谢@WiktorStribiżew的帮助,已找到适用于Ansible的regex_search正则表达式:
regex_search('define\\s*\\(\\s*[\\x22\\x27]DB_HOST[\\x22\\x27]\\s*,\\s*[\\x22\\x27]([^\\x22\\x27]+)[\\x22\\x27]\\s*\\)', '\\1') | first
更新2:完整参考剧本
基于@U880D的回复,以下是完整的Ansible剧本供有相同需求的用户参考:
--- - name: Update wp-config.php with new template hosts: all become: true tasks: - name: Find all user directories under /home ansible.builtin.find: paths: "{{ search_path }}" file_type: directory register: user_dirs - name: Find all wp-config.php files in user directories ansible.builtin.find: paths: "{{ item.path }}/public_html" patterns: "wp-config.php" register: wp_config_files loop: "{{ user_dirs.files }}" - name: Fail if no wp-config.php files found ansible.builtin.fail: msg: "No wp-config.php files found in the provided paths." when: wp_config_files.matched == 0 - name: Debug wp-config.php files ansible.builtin.debug: var: wp_config_files - name: Read existing wp-config.php for database credentials ansible.builtin.slurp: src: "{{ item.path }}" loop: "{{ wp_config_files.files }}" register: wp_config_content when: wp_config_files.matched > 0 - name: Debug wp-config.php content ansible.builtin.debug: var: wp_config_content.results - name: Set database credentials from existing wp-config.php ansible.builtin.set_fact: db_host: >- {{ item.content | b64decode | regex_search('DB_HOST(.+)', '\\1') | regex_search('([A-Za-z])+') }} db_name: >- {{ item.content | b64decode | regex_search('DB_NAME(.+)', '\\1') | regex_search('([A-Za-z])+') }} db_user: >- {{ item.content | b64decode | regex_search('DB_USER(.+)', '\\1') | regex_search('([A-Za-z])+') }} db_password: >- {{ item.content | b64decode | regex_search('DB_PASSWORD(.+)', '\\1') | regex_search('([A-Za-z])+') }} loop: "{{ wp_config_content.results }}" loop_control: loop_var: item when: item.content is defined - name: Debug extracted database credentials ansible.builtin.debug: msg: >- DB Name: {{ db_name }}, DB User: {{ db_user }}, DB Password: {{ db_password }}, DB Host: {{ db_host }} - name: Create a new wp-config.php from template ansible.builtin.template: src: "wp-config.php.j2" dest: "{{ item.path }}" mode: "0644" loop: "{{ wp_config_files.files }}" when: wp_config_files.matched > 0
内容的提问来源于stack exchange,提问作者fariscode
相关产品推荐
相关产品推荐

