Project

General

Profile

Ansible cheat sheet » History » Revision 17

Revision 16 (Brienne Kordis, 06/15/2023 02:09 PM) → Revision 17/25 (Brienne Kordis, 06/15/2023 02:18 PM)

{{last_updated_at}} by {{last_updated_by}} 

 # Ansible cheat sheet 

 This page will cover some helpful one-line commands we can execute with Ansible to accomplish various tasks. Always run **git pull** and **git submodule update** before executing these commands. 

 ### Synchronize a non-canonical site with the live site 
 ``` 
 ansible-playbook sync.yml -l mysite.local  
 ``` 
 * This works on test and dev sites. 
 * Depending on the sync strategy on the website inventory, it will pull from last night's backup, sync directly from the live site, or use the Pantheon API to make a backup/download. 
 * Only sites that pull from backup can sync to a site that's not on your local machine at present (I'll fix this at some point). 

 ### Ad-hoc examples 
 #### Revert a failed local update 
 ```shell 
 ansible '*.local' -m shell -a "cd {{webroot}} && git checkout ." --become --become-user "{{ run_as_user }}"  
 ``` 

 #### Roll back a site to the latest commit (e.g. after a failed merge): 
 ```shell 
 ansible --become --become-user="{{ run_as_user }}" -m shell -a 'cd {{civiroot}} && git clean -fd {{civiroot}} && git reset --hard HEAD'    mysite1.local,mysite2.local 
 ``` 

 #### Update all dev sites to the latest version of a submodule 
 ```shell 
 ansible --become --become-user="{{ run_as_user }}" -m shell -a 'cd {{webroot}}/{{civicrm_custom_path}}/extensions/com.megaphonetech.monitoring && git checkout master && git pull && cd .. && git add    com.megaphonetech.monitoring && git commit -m"Updated monitoring submodule" && git push' 'maintenance_civi:&websites_dev' 
 ``` 

 #### Send a drush command to multiple sites 
 This is trickier to handle places where we don't have root, but works. 
 ```shell 
 ansible --become --become-user="{{ run_as_user }}" -m shell -a "PATH=\$HOME/bin:\$PATH; drush --root={{ webroot }} pm-disable -y print" 'maintenance_drupal' 
 ``` 

 #### Update a Civi extension only on certain sites 
 ```shell 
 ansible --become --become-user="{{ run_as_user }}" -m shell -a "PATH=\$HOME/bin:\$PATH; cv --cwd={{ webroot }} ext:download --force extendedreport" site1.local,site2.local 
 ansible --become --become-user="{{ run_as_user }}" -m shell -a "cd {{ webroot }}; git add .; git commit -m'ExtendedReport 5.16'; git push" site1.local,site2.local 
 ``` 

 #### Send an arbitrary SQL statement to all Civi test instances with maintenance contracts 
 ```shell 
 ansible --become --become-user="{{ run_as_user }}" -m shell -a "PATH=\$HOME/bin:\$PATH; echo \"UPDATE civicrm_job SET is_active = 0 WHERE api_action = 'group_rebuild';\" | cv --cwd={{ webroot }} sql" 'maintenance_civi:&websites_test' 
 ```` 

 #### Set CLI PHP version across all servers 
 This is an example of server-level changes (others are site-level). 
 ```shell 
 ansible    --become -m command -a 'update-alternatives --set php /usr/bin/php7.4' vps 
 ``` 

 #### Copy a local file to all servers 
 ```shell 
 ansible --become -m copy -a "src=/home/jon/Downloads/goodphp81.php.ini dest=/etc/php/8.1/fpm/php.ini mode=0644 owner=root group=root" lamp 
 ``` 

 ### Run a non-command module across all servers 
 ``` 
 ansible --become -m user -a"name=joseph state=absent remove=yes" vps 
 ``` 

 ### Clear templates_c and flush cache on all maintenance servers 
 ``` 
 ansible --become -m shell -a "cd {{ webroot }}/wp-content/uploads/civicrm; rm -rf templates_c; cv flush" 'maintenance_civi:&maintenance_wp:&websites_live' 
 ansible --become -m shell -a "cd {{ webroot }}/sites/default/files/civicrm; rm -rf templates_c; cv flush" 'maintenance_civi:&maintenance_drupal:&websites_live' 
 ``` 

 

 ### Push changes to live and/or test sites 

 
 *Note that what is within the [] refers to the live/testing site's name or the client's shortname/keywords if using an * to apply the update to all matching options* 
 ```shell 
 ansible-playbook upgrade.yml --tags update-sites -l '[sitename]' '[clientshortname.megaphonetech.com]'  
 ansible-playbook upgrade.yml --tags update-sites -l [sitename] [clientshortname.megaphonetech.com]  
 # Do two or more sites - no space after the comma 
 ansible-playbook upgrade.yml --tags update-sites -l [sitename], [sitename] [clientshortname.megaphonetech.com], [clientwebsite] 
 # Wildcards require the single quotes. I like this approach because it's quicker to type 
 ansible-playbook upgrade.yml --tags update-sites -l '[wildcard keyword]*' '[clientshortname]*'  
 # Everything together 
 ansible-playbook upgrade.yml --tags update-sites -l '[wildcard keyword]*,*[wildcard keyword]*' '[clientshortname]*,*[clientkeyword]*'  
 ```