Ansible Playbook

1.什么是playbook?

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

2.playbook和Ad-Hoc的区别?

image.png

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

image.png

4.实例

使用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 [email protected]

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](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

你可能感兴趣的:(Ansible Playbook)