Project

General

Profile

Ansible cheat sheet » History » Version 8

Jon Goldberg, 06/28/2022 12:47 AM

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 6 Jon Goldberg
### Synchronize a non-canonical site with the live site's database
8 2 Jon Goldberg
```
9 1 Jon Goldberg
ansible-playbook main-playbook.yml --tags site-db-sync -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 8 Jon Goldberg
ansible --become --become-user="{{ run_as_user }}" -m shell -a 'cd {{civiroot}} && git clean -fdx {{civiroot}} && git reset --hard HEAD'  mysite1.local,mysite2.local
24 6 Jon Goldberg
```
25
26 7 Jon Goldberg
#### Send a drush command to multiple sites
27
This is trickier to handle places where we don't have root, but works.
28
```shell
29
ansible --become --become-user="{{ run_as_user }}" -m shell -a "PATH=\$HOME/bin:\$PATH; drush --root={{ webroot }} pm-disable -y print" 'maintenance_drupal'
30
```
31
32 1 Jon Goldberg
#### Send an arbitrary SQL statement to all Civi test instances with maintenance contracts
33
```shell
34 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'
35
````
36 6 Jon Goldberg
37
#### Set CLI PHP version across all servers
38
This is an example of server-level changes (others are site-level).
39
```shell
40
ansible  --become -m command -a 'update-alternatives --set php /usr/bin/php7.4' vps
41
```