在Ansible中,將各個模塊組合起來成爲一個YAML格式的配置文件,這個配置文件叫作Playbook,html
Playbook和模塊的關係相似於shell腳本和Linux命令之間的關係。node
一個Playbook能夠包含多個Play,一個Play必須包含:python
當options較長時能夠採用縮進子塊的形式。mysql
- name: install apachenginx
yum:web
name: apache2sql
state: presentmongodb
一個Playbook能夠使用導入其它的Playbookshell
---apache
- inclued: db.yml
- include: web.yml
ansible的命令行參數:
Ansible有多種定義變量的方法,對於playbook,最簡單的就是定義在Playbook的vars項中;
- hosts vars: mysql_port: 80
當變量多時,能夠保存在一個獨立的文件中
--- - hosts: all vars: mysql_prot: 80 vars_file: - /vars/external_vars.yml
變量文件的格式:
--- process: 2000 username: scott
經過register獲取上條命令的執行結果。並在下一個task中引用該變量
- hosts: webservers tasks: - shell: /usr/bin/foo register: foo_result ignore_errors: True - shell: /usr/bin/bar when: foo_result.rc == 5
在Ansible中有些變量不須要進行任何設置就能直接使用,這些變量叫作Facts變量。
這些變量是Ansible從遠程服務器上獲取的系統信息。
能夠經過setup模塊查看。
ansible webservers -m setup
在Playbook中默認是收集遠程機器信息的,能夠設置爲no,提升Ansible的執行效率。
-- hosts: dbservers
gather_facts: no
--- - name: Install Mysql package yum: name={{ item }} state=installed with_items: - mysql-server - Mysql-python
--- - hosts: webservers tasks: - command: echo {{ item }} with_items: [ 0,2,4,6,8] when: item > 5
執行結果;
使用Playbook部署nginx
--- - hosts: webservers become: yes become_method: sudo vars: worker_connections: 1024 worker_processes: 4 max_open_files: 65506 tasks: - name: install nginx yum: name=nginx update_cache=yes state=present - name: copy nginx config file template: src=/root/study/Ansible/nginx.conf.j2 dest=/etc/nginx/nginx.conf notify: restart nginx - name: copy index.html template: src: /root/study/Ansible/index.html.j2 dest: /usr/share/nginx/www/index.html mode: 0644 notify: restart nginx handlers: - name: restart nginx service: name=nginx state=restarted
jinjia2模板文件:
nginx.conf.j2
worker_processes {{ worker_processes }}; worker_rlimit_nofile {{ max_open_files }}; events { worker_connections {{ worker_connections }}; } http { server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; listen 443 ssl; root /usr/share/nginx/html; index index.html index.htm; server_name localhost; location / { try_files $uri $uri/ =404; } } }
index.html.j2
<html> <head> <title>Welcome to ansible</title> </head> <body> <h1>nginx, configured by Ansible</h1> <p>If you can see this, Ansible successfully installed nginx.</p> <p>{{ ansible_hostname }}</p> </body> </html>
訪問主頁;
部署MongoDB
--- - hosts: dbservers become: yes become_method: sudo vars: mongodb_datadir_prefix: /data mongod_port: 27018 tasks: - name: Create the mongodb user user: name=mongodb comment="MongoDB" - name: Create the data directory for the namenode metadata file: path={{ mongodb_datadir_prefix }} owner=mongodb group=mongodb state=directory - name: Install the mongodb package apt: name={{ item }} state=installed with_items: - mongodb-server - mongodb-clients - rsyslog-mongodb - name: create data directory for mongodb file: path: "{{ mongodb_datadir_prefix }}/mongo-{{ ansible_hostname }}" state: directory owner: mongodb group: mongodb - name: create log directory for mongodb file: path=/var/log/mongo state=directory owner=mongodb group=mongodb - name: Create the mongodb startup file template: src=mongod.j2 dest=/etc/init.d/mongod-{{ ansible_hostname }} mode=0655 - name: Create the mongodb configuration file template: src=mongod.conf.j2 dest=/etc/mongod-{{ ansible_hostname }}.conf - name: Copy the keyfile for authentication copy: src=secret dest={{ mongodb_datadir_prefix }}/secret owner=mongodb group=mongodb mode=0400 - name: Start the mongodb service command: creates=/var/lock/subsys/mongod-{{ ansible_hostname }} /etc/init.d/mongod-{{ ansible_hostname }} start
配置文件:mongod.conf.j2
# mongo.conf smallfiles=true #where to log logpath=/var/log/mongo/mongod-{{ ansible_hostname }}.log logappend=true # fork and run in background fork = true port = {{ mongod_port }} dbpath={{ mongodb_datadir_prefix }}mongo-{{ ansible_hostname }} keyFile={{ mongodb_datadir_prefix }}/secret # location of pidfile pidfilepath=/var/run/mongod-{{ ansible_hostname }}.pid
另外還能夠將Playbook抽象成role。
能夠參考https://galaxy.ansible.com,下載別人寫好的role
初始化role
ansible-galaxy init /etc/ansible/roles/websrvs
安裝別人寫好的role
ansible-galaxy install -p /etc/ansible/roles bennojoy.mysql