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  解壓


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM