> For the complete documentation index, see [llms.txt](https://close.gitbook.io/yun-wei-bi-ji/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://close.gitbook.io/yun-wei-bi-ji/centos/ansible/playbook-bu-shu-squid-dai-li.md).

# Playbook 部署Squid代理

### **目录结构**

```bash
[root@localhost ~]# mkdir roles/{files,handlers,tasks} -p
[root@localhost ~]# tree
.
|-- hosts
|-- install.yml
`-- roles
    `-- squid
        |-- files
        |   `-- squid.conf     # 略
        |-- handlers
        |   `-- main.yml
        `-- tasks
            `-- main.yml

5 directories, 5 files
```

### **主机文件**

```bash
[root@localhost ]# cat hosts
[squid]
10.10.181.17
```

### **入口文件**

```yaml
[root@localhost ]# cat install.yaml
---
- name: 'Deployment to squid'
  gather_facts: false
  hosts: squid
  roles:
    - { role: 'squid', tags: "install_squid" }
```

### **任务文件**

```yaml
[root@localhost ]# cat roles/squid/tasks/main.yml 
- name: 'Install squid'
  yum:
    name: squid
    state: installed
- name: 'Copy configuration to remote'
  copy:
    src: squid.conf
    dest: /etc/squid/squid.conf
    mode: 0644
    backup: yes
  notify:
   - restart squid
- name: 'Crontab for restart squid'
  cron:
    name: restartSquid
    state: present
    hour: '23'
    job: "sync && echo 3 >/proc/sys/vm/drop_caches && rm -f /var/log/squid/access.log ; systemctl restart squid"
- name: 'Crontab for check squid'
  cron:
    name: CheckSquid
    state: present
    minute: '*'
    job: 'sleep 5 &&  systemctl status squid || systemctl start squid'
```

### **触发文件**

```yaml
[root@localhost ]# cat roles/squid/handlers/main.yml 
- name: restart squid
  systemd:
    name: squid
    state: restarted
    enabled: yes
```

### 检查语法、标签、预执行

```bash
ansible-playbook -i hosts --syntax-check install.yaml
ansible-playbook -i hosts --list-hosts --list-tags install.yaml
ansible-playbook -i hosts -C install.yaml
```

### 检查无误部署

```bash
[root@localhost checkSquid]# ansible-playbook -i hosts install.yml -k
SSH password: 

PLAY [Deployment to squid] **********************************************************************************************************************************************

TASK [Install squid] ****************************************************************************************************************************************************
changed: [10.10.181.17]

TASK [squid : Copy configuration to remote] *****************************************************************************************************************************
changed: [10.10.181.17]

TASK [Crontab for restart squid] ****************************************************************************************************************************************
changed: [10.10.181.17]

TASK [Crontab for check squid] ******************************************************************************************************************************************
changed: [10.10.181.17]

RUNNING HANDLER [restart squid] *****************************************************************************************************************************************
changed: [10.10.181.17]

PLAY RECAP **************************************************************************************************************************************************************
10.10.181.17               : ok=5    changed=5    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
```
