Ansible-playbook的基本使用

本文详细介绍如何使用Ansible进行自动化部署与配置管理,包括httpd的安装与优化,防火墙配置,以及通过模板和变量实现动态配置。同时,演示了如何配置负载均衡并测试服务可用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

指定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位置

在这里插入图片描述
vim配置两个tab

set smartindent
set tabstop=2
set shiftwidth=2
set expandtab
set softtabstop=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

在这里插入图片描述
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文件中设置
在这里插入图片描述

---
- 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'] }} 

在这里插入图片描述
server2主机查看结果
在这里插入图片描述
配置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 %}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值