Ansible PlayBooks 学习之 软件包安装与卸载

控制主机通过ansible-playbooks对被控制主机进行httpd服务安装

  • install_httpd.yml
---
    - hosts: slave1 // 被控制主机
  tasks:
    - name: Installs httpd server
      yum: name=httpd state=installed update_cache=true 
      notify:
        - start httpd

// 通过yum安装
// name=httpd 安装软件包为httpd
// state=installed 状态为安装
// update_cache=true 更新缓存

  handlers:
    - name: start httpd
      service: name=httpd state=started // 启动httpd服务
  • remove_httpd.yml
---
- hosts: slave1
  tasks:
    - name: stop httpd server // 停止httpd服务
      service: name=httpd state=stopped
      notify:
        - remove httpd


  handlers:
    - name: remove httpd
      yum: name=httpd state=removed

// 通过yum卸载
// name=httpd 卸载软件包为httpd
// state=removed 状态为安装
通过变量名进行软件包的安装与卸载
  • 安装
- hosts: slave1
  remote_user: root
  vars:
    - package: vsftpd
    - service: vsftpd
  tasks:
    - name: install vsftp package // 安装最新版本的vsftp
      yum: name={{package}} state=latest   
    - name: start vsftp server  // 开启服务
      service: name={{service}} enabled=true state=started    
  • 卸载
---
- hosts: slave1
  remote_user: root
  vars:
    - package: vsftpd
    - service: vsftpd

  tasks:
    - name: stop vsftp // 停止服务
      service: name={{service}} state=stopped
    - name: remove vsftp // 删除软件包
      yum: name={{package}} state=absent

你可能感兴趣的:(Linux,学习)