1. 程式人生 > >ansible之變量使用與playbook的設置(重點)

ansible之變量使用與playbook的設置(重點)

exports 2個 yml oot check config 簡單 ota 文件

ansible之playbook

1.playbook的語法格式如下: - hosts: webservers 是定義的主機組 也就是playbook中的 play 角色的意思 註意":" 冒號後面也必須價格空格不然就報錯

  tasks: 註意 他前面是有2個空格的, ansible 是用空格來區分規格的, 記住不能使用tab按鍵

[[email protected] ~]# mkdir project1
[[email protected] ~]# cd project1/
[[email protected] project1]# cat p1.yml 
--- #play - hosts: webservers tasks: - name: Installed Httpd Server yum: name: httpd state: present - name: Start Httpd Server systemd: name: httpd state: started enabled: yes

2.檢查語法,只檢查是否是yaml語法格式。並不做邏輯校驗。  (記住這個要經常使用,他是判斷語法是否正確!!!)

[[email protected] project1]# ansible-playbook --syntax-check p1.yml 

3.模擬執行(不是真的執行)

[[email protected] project1]# ansible-playbook -C  p1.yml 

4.真實的描述狀態(被控端的狀態必須與控制端描述的狀態一致)

[[email protected] project1]# ansible-playbook p1.yml
技術分享圖片
[[email protected] project1]# cat
p1.yml --- #play - hosts: webservers tasks: - name: Installed Httpd Server yum: name=httpd state=present - name: Start Httpd Server systemd: name=httpd state=started enabled=yes - name: Start Firewalld Server systemd: name=firewalld state=started enabled=yes - name: Configure Firewalld Server firewalld: service=http immediate=yes permanent=yes state=enabled - hosts: web01 tasks: - name: Configure web01 Website copy: content=This is Web01 dest=/var/www/html/index.html - hosts: web02 tasks: - name: Cofnigure webi-2 weisite copy: content=This is Web02 dest=/var/www/html/index.html
多paly語法示例 技術分享圖片
    1.安裝
    2.配置
        用戶
        /data
    3.啟動
    
#記得重啟你的nfs
[[email protected] project1]# cat nfs.yml 
- hosts: web01

  tasks:
    - name: Install NFS-utils Server
      yum: name=nfs-utils state=present

    - name: Configure Nfs-utils Server
      copy: src=./exports.j2 dest=/etc/exports owner=root group=root mode=0644

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

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

    - name: Create Data Directory
      file: path=/data state=directory owner=www group=www mode=0755 recurse=yes

    - name: Start NFS Server
      systemd: name=nfs state=started enabled=yes

- hosts: web02
  tasks:
    - name: Mount NFS Server
      mount: path=/opt src=172.16.1.7:/data fstype=nfs opts=defaults state=mounted
安裝NFS服務 技術分享圖片
1.使用yum安裝 httpd、php、php-mysql、mariadb、firewalld等
2.啟動httpd、firewalld、mariadb等服務
3.添加防火墻規則,放行http的流量,並永久生效
4.使用get_url下載 http://fj.xuliangwei.com/public/index.php 文件

[[email protected] project1]# cat lamp.yml 
#- hosts: webservers
- hosts: otherservers
  tasks:
    - name: Installed Web Packages
      yum: name=httpd,mariadb-server,php,php-mysql,php-pdo state=present

    - name: Start Web Serivce
      service: name=httpd state=started

    - name: Start Mariadb Service
      service: name=mariadb state=started

    - name: Get Wordpress
      unarchive: src=./wordpress-5.0.3-zh_CN.tar.gz dest=/var/www/html/ copy=yes mode=0755


  #  - name: Copy Index.php
  #    copy: src=./index.php.j2 dest=/var/www/html/index.php


 #   - name: Get Url index.php
 #     get_url: url="http://fj.xuliangwei.com/public/index.php" des
使用AnsiblePlaybook方式構建LAMP架構,具體操作步驟如下:

ansible之編寫playbook的變量形式

變量
1、playbook變量可以通過多種方式進行定義,最簡單的方式就是在playbook的開頭通過vars進行定義

#安裝兩個軟件包使用變量方式
[[email protected] project1]# cat  p2.yml 
- hosts: webservers
  vars:
    - web_package: httpd
    - ftp_package: vsftpd
  tasks:
    - name: Installed Packages
      yum: 
        name: 
          - "{{ web_package }}"
          - "{{ ftp_package }}"
        state: present

2.也可以在playbook中使用vars_files指定文件作為變量文件,好處就是其他的playbook也可以調

[[email protected] project1]# cat vars.yml 
web_package: httpd
ftp_package: vsftpd

[[email protected] project1]# cat p2.yml 
- hosts: webservers
  vars_files: ./vars.yml
  tasks:
    - name: Installed Packages
      yum: 
        name: 
          - "{{ web_package }}"
          - "{{ ftp_package }}"
        state: present

4.更好的方式是在ansible的項目目錄中創建額外的兩個變量目錄,分別是host_vars和group_vars

技術分享圖片
group_vars目錄下必須存放和inventory清單文件中定義的組名一致,如下
[[email protected] project1]# cat /etc/ansible/hosts 
[webservers]
web01 ansible_ssh_host=172.16.1.7
web02 ansible_ssh_host=172.16.1.8

[[email protected] project1]# cat group_vars/webservers 
web_package: httpd
ftp_package: vsftpd
註意:系統提供了特殊的組,all,也就說在group_vars目錄下創建一個all文件,定義變量對所有的主機都生效


[[email protected] project1]# cat host_vars/web01 
web_package: zlib-static
ftp_package: zmap

[[email protected] project1]# cat group_vars/webservers 
web_package: httpd
ftp_package: vsftpd


[[email protected] project1]#  cat p4.yml 
- hosts: webservers
#- hosts: otherservers
  tasks:
    - name: Installed Packages
      yum: 
        name: 
          - "{{ web_package }}"
          - "{{ ftp_package }}"
        state: present


[[email protected] project1]# ansible-playbook p4.yml 

PLAY [webservers] ********************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************
ok: [web02]
ok: [web01]

TASK [Installed Packages] ************************************************************************************************
ok: [web02]
changed: [web01]

PLAY RECAP ***************************************************************************************************************
web01                      : ok=2    changed=1    unreachable=0    failed=0   
web02                      : ok=2    changed=0    unreachable=0    failed=0   
View Code

6.變量優先級測試
命令行變量--->play中的vars_files--->play中的vars變量-->host_vars中定義的變量--->group_vars/組--->group_vars/all

6.變量註冊register

- hosts: webservers
  tasks:
    - name: Get Network Port Status
      shell: netstat -lntp
      register: net_port

    - name: OutPut Network Port Status
      debug:
        msg: "{{ net_port.stdout_lines }}"

7.變量也支持層級定義,使用"."可能會有問題,建議使用"[]"代替。

[[email protected] project1]# cat vars1.yml 
rainbow:
  web:
    web_package: httpd
    db_package: mariadb

code:
  web:
    filename: code_web_filename

    
[[email protected] project1]# cat p8.yml 
- hosts: webservers
  vars_files: ./vars1.yml
  tasks:
    - name: Install Package
      yum: name= "{{ rainbow[‘web‘][‘web_package‘] }}"

    - name: create filename
      file: 
        path: /tmp/{{ code.web.filename }}
        state: touch
        

facts (setup模塊)  重點!!!! 他可以獲取被控端的主機的變量信息, 還可以通過變量的運算得到相應的值

  當使用facts 就需要使用template 這個模塊

[[email protected] project1]# cat p10.yml 
- hosts: webservers
  #gather_facts: no   關閉facts采集
  vars: 
    - zabbix_server: 172.16.1.71
  tasks:
    - name: Copy Zabbix Agent Configure
      template: src=./zabbix_agentd.conf dest=/tmp/zabbix_agent.conf

playbook安裝一個memcached : 這裏就用到了變量的運算,獲取對應的值的信息

[[email protected] project1]# cat memcached.j2 
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="{{ ansible_memtotal_mb //2 }}"
OPTIONS=""

[[email protected] project1]# cat p11.yml 
- hosts: webservers
  tasks:
    - name: Installed Memcached
      yum: name=memcached state=present

    - name: Configure Memcached
      template: src=./memcached.j2 dest=/etc/sysconfig/memcached

    - name: Start Memcached
      service: name=memcached state=started enabled=yes

ansible之變量使用與playbook的設置(重點)