Playbook 部署Squid代理

目录结构

[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

主机文件

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

入口文件

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

任务文件

[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'

触发文件

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

检查语法、标签、预执行

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

检查无误部署

[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   

Last updated