Ansible-playbook的基本使用

指定production组安装httpd

---
- hosts: production
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present

    - name: start httpd
      service:
        name: httpd
        state: started

运行文件

ansible-playbook playbook.yaml
ansible-playbook playbook.yaml --syntax-check #检查语法
ansible-playbook playbook.yaml --list-task # 检测任务
ansible-playbook playbook.yaml --list-hosts# 检查生效主机
ansible-playbook playbook.yaml --start-at-task= # 指定开始的task位置

Ansible-playbook的基本使用_第1张图片
vim配置两个tab

set smartindent
set tabstop=2
set shiftwidth=2
set expandtab
set softtabstop=2

Ansible-playbook的基本使用_第2张图片
httpd安装优化

---
- hosts: production
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present
    - name: conf file
      copy:
        src: files/httpd.conf
        dest: /etc/httpd/conf/httpd.conf
        owner: root
        group: root
        mode: 644
      notify: restart httpd # 触发器的使用
    - name: start httpd
      service:
        name: httpd
        state: started
  handlers: #触发器
    - name: restart httpd
      service:
        name: httpd
        state: restarted

Ansible-playbook的基本使用_第3张图片
httpd配置防火墙以及测试

---
- hosts: production
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present

    - name: copy index.html
      copy:
        src: files/index.html
        dest: /var/www/html/index.html

    - name: configure file
      copy:
        src: files/httpd.conf
        dest: /etc/httpd/conf/httpd.conf
        owner: root
        group: root
        mode: 644
      notify: restart httpd
    
    - name: start httpd and firewalld
      service:
        name: "{
    { item }}"
        state: started
      loop:
        - firewalld
        - httpd

    - name: configure firewalld
      firewalld:
         service: http
         permanent: yes
         immediate: yes
         state: enabled
                    
  handlers:
    - name: restart httpd
      service: 
        name: httpd
        state: restarted

- hosts: localhost
  become: no
  tasks:
    - name: test httpd
      uri:
        url: http://172.25.33.3
        status_code: 200

变量的使用方式
yaml文件中添加,以及inventory文件中设置
Ansible-playbook的基本使用_第4张图片

---
- hosts: test
  vars: 
    - http_port: 80
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present

    - name: copy index.html
      copy:
        src: files/index.html
        dest: /var/www/html/index.html

    - name: configure file
      template:
        src: template/httpd.conf.j2
        dest: /etc/httpd/conf/httpd.conf
        owner: root
        group: root
        mode: 644
      notify: restart httpd
    
    - name: start httpd and firewalld
      service:
        name: "{
    { item }}"
        state: started
      loop:
        - firewalld
        - httpd

    - name: configure firewalld
      firewalld:
         service: http
         permanent: yes
         immediate: yes
         state: enabled
                    
  handlers:
    - name: restart httpd
      service: 
        name: httpd
        state: restarted

- hosts: localhost
  become: no
  tasks:
    - name: test httpd
      uri:
        url: http://172.25.33.3
        status_code: 200

运行结果
在这里插入图片描述
返回变量的值,并写入文件

cat file.yaml 
---
- hosts: all
  tasks:
    - name: create file
      template:
        src: template/file.j2
        dest: /tmp/

cat template/file.j2 
主机名: {
    { ansible_facts['hostname'] }}
主机IP: {
    { ansible_facts['default_ipv4']['address'] }}
主机DNS: {
    { ansible_facts['dns']['nameservers'][0] }}
boot分区: {
    { ansible_facts['devices']['sda']['partitions']['sda1']['size'] }}
内核: {
    { ansible_facts['kernel'] }} 

Ansible-playbook的基本使用_第5张图片
server2主机查看结果
Ansible-playbook的基本使用_第6张图片
配置haproxy负载均衡

---
- hosts: test:production
  vars: 
    - http_port: 80
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present

    - name: copy index.html
      copy:
        content: "{
    { ansible_facts['hostname'] }}"
        dest: /var/www/html/index.html

    - name: configure file
      template:
        src: template/httpd.conf.j2
        dest: /etc/httpd/conf/httpd.conf
        owner: root
        group: root
        mode: 644
      notify: restart httpd
    
    - name: start httpd and firewalld
      service:
        name: "{
    { item }}"
        state: started
      loop:
        - firewalld
        - httpd

    - name: configure firewalld
      firewalld:
         service: http
         permanent: yes
         immediate: yes
         state: enabled
                    
  handlers:
    - name: restart httpd
      service: 
        name: httpd
        state: restarted

- hosts: localhost
  tasks:
    - name: test httpd
      uri:
        url: http://172.25.33.3
        status_code: 200
    - name: install haproxy
      yum:
        name: haproxy
        state: present

    - name: start haproxy
      service:
        name: haproxy
        state: started
    - name: configure haproxy
      copy:
        src: template/haproxy.cfg.j2
        dest: /etc/haproxy/haproxy.cfg
      notify: restart haproxy
  handlers:
    - name: restart haproxy
      service: 
        name: haproxy
        state: restarted

配置文件haproxy.cfg内容

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000
    stats uri /status

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:80
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server  app1 172.25.33.2:80 check
    server  app2 172.25.33.3:80 check

测试
在这里插入图片描述
修改haproxy文件设置负载均衡自动检测添加节点

backend app
    balance     roundrobin
    {% for host in groups['webserver'] %}
        server {
    { hostvars[host]['ansible_facts']['hostname'] }} {
    { hostvars[host]['ansible_facts']['eth0']['ipv4']['address'] }}:80 check
    {% endfor %}

Ansible-playbook的基本使用_第7张图片

你可能感兴趣的:(高级课程)