Referencing Ansible environment variables for both local and remote in same task

I apologize if this question is out of scope and is a noob question:

but I read a few posts on here that inspired me to try and learn how to use Ansible (no experience until I started experimenting with Trellis). I really love the Trellis/Bedrock set up but wanted it to also push/pull database and wp uploads to and from remote using Ansible as opposed to using some of the Capistrano solutions I’ve read about on here (basically having to deploy out of two different directories)

I am having a problem referencing the “development” environment variables on a task that should run on the local machine. It seems when I use the environment variables, the remote machine ones are referenced. I’d like to be able to reference them both in the same playbook.

Is there a way I could “load the development environment variables” into an object (such as the development specific wordpress_sites.yml and vault.yml files for instance, and then just reference them like devSite.wordpress_sites, devSite.vault_mysql_root_password etc?

Has anyone had any luck with also deploying database changes with Ansible?

Thank you in advance for any tips and tricks on using Ansible with Trellis.

Can you please show code/files changed on what you’ve attempted so far, and the errors associated with them?

You can find several discussions on this forum about it: Search results for 'trellis database' - Roots Discourse

This might be helpful as well: https://github.com/roots/trellis/pull/650

It’s a backup feature but has examples of things you probably need.

Hey guys thanks for the reply,

Sure, I was using deploy.yml as a reference and set it up similar. This particular one is for pushing from local to remote and I structured the command the exact same as deploy.yml

tasks:
- name: Creating a backup of remote database
  remote_user: "{{ web_user }}"
  become: yes
  become_user: "{{ web_user }}"
  mysql_db:
    state: dump
    name: "{{ site_env.db_name }}"
    login_host: "{{ site_env.db_host }}"
    login_user: "{{ site_env.db_user }}"
    login_password: "{{ site_env.db_password }}"
    target: "{{ www_root }}/{{ site }}/shared/db/remote-backup.sql"

- name: Creating a dump of local database
  connection: local
  become: yes
  become_user: "{{ web_user }}"
  mysql_db:
    state: dump
    name: "{{ site_env.db_name }}"
    login_host: "{{ site_env.db_host }}"
    login_user: "{{ site_env.db_user }}"
    login_password: "{{ site_env.db_password }}"
    target: "{{ www_root }}/{{ site }}/shared/db/local.sql"

- name: Copying local dump to remote
  copy:
    src: "{{ www_root }}/{{ site }}/shared/db/local.sql"
    dest: "{{ www_root }}/{{ site }}/shared/db/local.sql"
    owner: "{{ web_user }}"
    group: "{{ web_group }}"
    mode: 0777
  run_once: true

- name: Importing local database to remote
  mysql_db:
    state: import
    login_host: "{{ site_env.db_host }}"
    login_user: "{{ site_env.db_user }}"
    login_password: "{{ site_env.db_password }}"
    name: "{{ site_env.db_name }}"
    target: "{{ www_root }}/{{ site }}/shared/db/local.sql"
  run_once: true

- name: Updating URLs in remote database
  command: wp db search-replace LOCAL_URL PRODUCTION_URL
  args:
    chdir: "{{ www_root }}/{{ site }}/current/"

I guess my main issue is being able to “switch” between the development environment variables and the remote server environment variables that are defined in the configuration files.

I’ve attempted loading the development wordpress_sites.yml file using lookup() and assigning it to a variable I could reference the wordpress_sites dictionary but when I attempted to reference the objects in that yml file it would always error and say the dictionary or object wouldn’t exist.

Am I on the right track?

Essentially I want to create two playbooks similar to the deploy.yml file except called push.yml and pull.yml and run the command the exact way you do when deploying.