Ansible cheat sheet » History » Version 15
Brienne Kordis, 01/19/2023 10:34 PM
1 | 1 | Jon Goldberg | {{last_updated_at}} by {{last_updated_by}} |
---|---|---|---|
2 | |||
3 | # Ansible cheat sheet |
||
4 | |||
5 | 3 | Irene Meisel | 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. |
6 | 1 | Jon Goldberg | |
7 | 15 | Brienne Kordis | ### Synchronize a non-canonical site with the live site |
8 | 2 | Jon Goldberg | ``` |
9 | 15 | Brienne Kordis | ansible-playbook sync.yml -l mysite.local |
10 | 2 | Jon Goldberg | ``` |
11 | 6 | Jon Goldberg | * This works on test and dev sites. |
12 | * 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. |
||
13 | 1 | Jon Goldberg | * 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). |
14 | |||
15 | 6 | Jon Goldberg | ### Ad-hoc examples |
16 | 1 | Jon Goldberg | #### Revert a failed local update |
17 | ```shell |
||
18 | ansible '*.local' -m shell -a "cd {{webroot}} && git checkout ." --become --become-user "{{ run_as_user }}" |
||
19 | ``` |
||
20 | 4 | Jon Goldberg | |
21 | 6 | Jon Goldberg | #### Roll back a site to the latest commit (e.g. after a failed merge): |
22 | ```shell |
||
23 | 14 | Jon Goldberg | ansible --become --become-user="{{ run_as_user }}" -m shell -a 'cd {{civiroot}} && git clean -fd {{civiroot}} && git reset --hard HEAD' mysite1.local,mysite2.local |
24 | 6 | Jon Goldberg | ``` |
25 | |||
26 | 13 | Jon Goldberg | #### Update all dev sites to the latest version of a submodule |
27 | ```shell |
||
28 | 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' |
||
29 | ``` |
||
30 | |||
31 | 7 | Jon Goldberg | #### Send a drush command to multiple sites |
32 | This is trickier to handle places where we don't have root, but works. |
||
33 | ```shell |
||
34 | ansible --become --become-user="{{ run_as_user }}" -m shell -a "PATH=\$HOME/bin:\$PATH; drush --root={{ webroot }} pm-disable -y print" 'maintenance_drupal' |
||
35 | ``` |
||
36 | |||
37 | 9 | Jon Goldberg | #### Update a Civi extension only on certain sites |
38 | ```shell |
||
39 | 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 |
||
40 | 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 |
||
41 | ``` |
||
42 | |||
43 | 1 | Jon Goldberg | #### Send an arbitrary SQL statement to all Civi test instances with maintenance contracts |
44 | ```shell |
||
45 | 4 | Jon Goldberg | 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' |
46 | ```` |
||
47 | 6 | Jon Goldberg | |
48 | #### Set CLI PHP version across all servers |
||
49 | This is an example of server-level changes (others are site-level). |
||
50 | ```shell |
||
51 | ansible --become -m command -a 'update-alternatives --set php /usr/bin/php7.4' vps |
||
52 | ``` |
||
53 | 10 | Jon Goldberg | |
54 | 12 | Jon Goldberg | #### Copy a local file to all servers |
55 | ```shell |
||
56 | 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 |
||
57 | ``` |
||
58 | |||
59 | 10 | Jon Goldberg | ### Run a non-command module across all servers |
60 | ``` |
||
61 | ansible --become -m user -a"name=joseph state=absent remove=yes" vps |
||
62 | ``` |
||
63 | 11 | Jon Goldberg | |
64 | ### Clear templates_c and flush cache on all maintenance servers |
||
65 | ``` |
||
66 | ansible --become -m shell -a "cd {{ webroot }}/wp-content/uploads/civicrm; rm -rf templates_c; cv flush" 'maintenance_civi:&maintenance_wp:&websites_live' |
||
67 | ansible --become -m shell -a "cd {{ webroot }}/sites/default/files/civicrm; rm -rf templates_c; cv flush" 'maintenance_civi:&maintenance_drupal:&websites_live' |
||
68 | ``` |