Ansible-Playbook

Ansible Playbook

  • 1.什么是playbook?

    • playbook 剧本 <—文件 YAML
      • play 找谁 明星 找那个主机 web01
      • task 做什么 干什么事情 yum copy service
    • 找多个明星,做多件事情
    • 找一个明星,做多件事情
  • 2.playbook和Ad-Hoc的区别?

Ad-Hoc是一次性的,命令行式的,用完一次,下次用找起来不方便
playbook复用性比较高,可以在不同的环境运行
  • 3.Playbook三板斧:缩进 冒号 短横线 (语法格式)

使用playbook编写一个创建文件的yml

[root@manage project]# ls
ansible.cfg  backup  f1.yml  file  hosts
[root@manager project1]# cat f1.yml 

- hosts: webservers
  tasks:

    - name: Create New File
      file: path=/tmp/123.txt state=touch owner=root group=root mode=600

#有两种写法,但是一般都按照下面写法写,比较清晰
    - name: Create New File2
      file:
        path: /tmp/456.txt
        state: touch
        owner: root
        group: root
        mode: 0666

案例一、使用ansible安装并配置nfs服务

#172.16.1.31   nfs
#172.16.1.7    clinet
#172.16.1.8    clinet


#1.新增一台nfs服务器
[root@manager project1]# cat hosts 
[nfsservers]
172.16.1.31

[webservers]
172.16.1.7
172.16.1.8
[root@manager project1]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.31

#2.测试三台主机是否通
[root@manager project1]# ansible all -m ping -i hosts

#3.编写一个nfs-server的yml
	1.安装nfs			yum
	2.配置nfs			copy
	3.初始化环境		
		用户			group  user
		目录			file
		授权			file
	4.启动服务		   systemd

[root@manager project1]# cat nfs_server.yml 
- hosts: nfsservers
  tasks:
    - name: Installed NFS Server
      yum:
        name: nfs-utils
        state: present

    - name: Configure NFS Server
      copy:
        src: ./file/exports.j2 
        dest: /etc/exports
        owner: root
        group: root
        mode: 0644
        backup: yes

    - name: Create NFS Group www
      group:
        name: www
        gid: 666

    - name: Create NFS User www
      user:
        name: www
        group: www
        uid: 666
        create_home: no
        shell: /sbin/nologin

    - name: Create NFS Share Directory
      file:
        path: /ansible_data
        state: directory
        owner: www
        group: www
        mode: 0755
        recurse: yes

    - name: Systemd NFS Server 
      systemd:
        name: nfs
        state: restarted
        enabled: yes


#4.编写一个nfs-clinet的yml
[root@manager project1]# cat nfs_client.yml 
- hosts: webservers
  tasks:

    - name: Mount NFS Server share directory
      mount:
        src: 10.0.0.31:/ansible_data
        path: /mnt
        fstype: nfs
        opts: defaults
        state: mounted

案例二、使用ansible安装并配置nginx服务

1.安装		yum
2.配置		copy
3.启动		systmd
handlers

[root@manager project1]# cat nginx.yml 
- hosts: webservers
  tasks:

    - name: Installed Nginx Server
      yum:
        name: nginx
        state: present

    - name: Configure Nginx Server
      copy:
        src: ./file/nginx.conf.j2
        dest: /etc/nginx/nginx.conf
        owner: root
        group: root
        mode: 0644
        backup: yes
      notify: Restart Nginx Server
      
    - name: Systmd nginx Server
      systemd:
        name: nginx
        state: started
        enabled: yes

  handlers:
    - name: Restart Nginx Server
      systemd:
        name: nginx
        state: restarted

案例三、使用AnsiblePlaybook方式构建LAP架构,具体操作步骤如下:

1.使用yum安装 httpd、php、firewalld等 7.1 5.3

2.使用get_url下载http://fj.xuliangwei.com/public/index.php文件

3.启动httpd、firewalld、等服务

4.添加防火墙规则,放行http的流量*

[root@manager project1]# cat hosts 
[nfsservers]
172.16.1.31

[backupservers]
172.16.1.41

[web:children]
nfsservers
backupservers

[webservers]
172.16.1.7
172.16.1.8


#具体配置
[root@manager project1]# cat lamp.yml 
- hosts: web
  tasks:
    - name: Installed Httpd Server
      yum: 
        name: httpd
        state: present

    - name: Installed PHP Server
      yum: 
        name: php
        state: present

    - name: Configure Httpd WebSite
      get_url:
        url: http://fj.xuliangwei.com/public/index.php
        dest: /var/www/html/index.php
        mode: 0644

    - name: Systemd Httpd Server
      systemd:
        name: httpd
        state: started

    - name: Systemd Firewalld Server
      systemd:
        name: firewalld
        state: started


    - name: Configure Firewalld Rule
      firewalld:
        service: http
        state: enabled

案例四、搭建可道云网盘 31 41 apache+php

1.安装      apache+php
2,下载代码
3.启动      systemd
4.下载代码   wget  解压
[root@manage project]# cat kod.yml 
- hosts: web
  tasks:
    - name : Install Httpd Server
      yum:
        name: httpd
        state: present

    - name: Configure Httpd Server
      yum:
        name: php
        state: present

    - name: Get Kodcloud Code
      synchronize:
        src: ./file/kod
        dest: /var/www/html/kodcloud

    - name: Chmod Kodcloud
      file:
        path: /var/www/html/
        owner: root
        group: root
        mode: 0777
        recurse: yes

    - name: Systemd Httpd Server
      systemd:
        name: httpd
        state: restarted

案例五 Nginx+PHP 搭建可道云

  • 1.先手动实现

    • 1.配置yum源 nginx php
    • 2.安装软件包 (循环的方式)
      • nginx php71w
    • 3.创建用户 www 统一UID和GID
    • 4.配置nginx.conf配置文件,修改启用用户为www
    • 5.配置php的权限 /etc/php-fpm.d/www.conf
    • 6.添加虚拟主机 /etc/nginx/conf.d/xx.conf
    • 7.创建网站的站点目录
    • 8.传输代码至站点目录
    • 9.启动nginx和php
    • 10.修改配置还需要能够实现自动重启
    [root@manage project]# ls
    ansible.cfg    file(目录)  hosts   lnp.yml 
    
    [root@manage project]# tree -L 2
    .
    ├── ansible.cfg
    ├── file
    │   ├── exports.j2
    │   ├── kod
    │   ├── kodbox.1.22.zip
    │   ├── kod.cwq.com.j2
    │   ├── nginx.conf.j2
    │   └── www.conf.j2
    └── hosts
    
    [root@manage project]# cat hosts 
    [nfsservers]
    10.0.0.31
    
    [backupservers]
    10.0.0.41
    
    [web:children]
    nfsservers
    backupservers
    
    [webservers]
    10.0.0.7 
    10.0.0.8
    
    [root@manage project]# cat lnp.yml 
    - hosts: web
      tasks:
    #1.配置yum源 nginx
        - name: Installed Nginx repo
          yum_repository:
            name: nginx
            description: nginx repos
            baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
            gpgcheck: no
    
    #2.配置yum源 php
        - name: Installed PHP repo
          yum_repository:
            name: webtatic-php
            description: php repos
            baseurl: http://us-east.repo.webtatic.com/yum/el7/x86_64/
            gpgcheck: no
    
    #3.安装nginx和php
        - name: Install Nginx PHP
          yum:
            name: "{{ packages }}"
          vars:
            packages:
              - nginx
              - php71w
              - php71w-cli
              - php71w-common
              - php71w-devel
              - php71w-gd
              - mod_php71w
              - php71w-fpm
              - php71w-opcache
    
              
    #4.创建用户组
        - name: Create Group WWW
          group:
            name: www
            gid: 666
    
    #5.创建用户
        - name: Create User www
          user:
            name: www
            group: www
            uid: 666
            create_home: no
            shell: /sbin/nologin          
    
    #6.管理nginx配置文件
        - name: Configure Nginx.conf
          copy:
            src: ./file/nginx.conf.j2
            dest: /etc/nginx/nginx.conf
          notify: Restart Nginx Server
    #此处要与最底部handlers中写的名字对应
    
    #7.管理php-fpm配置文件
        - name: Configure php-fpm.conf
          copy:
            src: ./file/www.conf.j2
            dest: /etc/php-fpm.d/www.conf
          notify: Restart PHP-FPM Server
    #8.添加kodcloud虚拟主机
        - name: Add Nginx VirtHost kod.cwq.com
          copy:
            src: ./file/kod.cwq.com.j2
            dest: /etc/nginx/conf.d/kod.cwq.conf
          notify: Restart Nginx Server
    #9.Init nginx base env
        - name: Init Nginx BseENV
          file:
            path: /code
            state: directory
            owner: www
            group: www
            recurse: yes
    #10.Copy kodcloud
        - name: Push Kodcloud code
          synchronize:
            src: ./file/kod
            dest: /code/
    
    #目录授权
        - name: Chmod Kodcloud
          file:
            path: /code
            owner: www
            group: www
            mode: 0777
            recurse: yes
    
    #启动nginx
        - name: Systemd Nginx Server
          systemd:
            name: nginx
            state: started
            enabled: yes
    
        - name: Systemd Php-fpm Server
          systemd:
            name: php-fpm
            state: started
            enabled: yes
    
    #当配置文件发生变化会重启
      handlers:
        - name: Restart Nginx Server
          systemd:
            name: nginx
            state: restarted
    
        - name: Restart PHP-FPM Server
          systemd:
            name: php-fpm
            state: restarted
            
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值