Ansible-Playbook

Posted yang-dan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ansible-Playbook相关的知识,希望对你有一定的参考价值。

1.什么是playbook?

playbook      剧本              <---文件     YAML     
    play    找谁       明星      找那个主机     web01
    task       做什么             干什么事情    yum  copy service

找多个明星,做多件事情
找一个明星,做多件事情

2.playbook和Ad-Hoc的区别?

技术图片

3.Playbook三板斧? 缩进 冒号 短横线 (语法格式)

技术图片

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

[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: 172.16.1.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  解压

以上是关于Ansible-Playbook的主要内容,如果未能解决你的问题,请参考以下文章

[Docker][ansible-playbook]3 持续集成环境初探

Ansible-playbook自动部署MySQL主从

在命令“ansible-playbook”中加密用户私钥文件

ansible-playbook调试

ansible-playbook

ansible-playbook 变量(vars)