zoukankan      html  css  js  c++  java
  • linux 系统管理(5) ansible 批管理

    简介

    ansible是个什么东西呢?官方的title是“Ansible is Simple IT Automation”——简单的自动化IT工具。
    这个工具的目标有这么几项:让我们自动化部署APP;自动化管理配置项;自动化的持续交付;自动化的(AWS)云服务管理。
    AnsibleWorks成立于2012年,由自动化工具Cobbler及Func的开发者Michael DeHaan创建。其Ansible平台是一个开源的配置及计算机管理平台。可实现多节点的软件部署,执行特定任务并进行配置管理。
    Ansible跟其他IT自动化技术的区别在于其关注点并非配置管理、应用部署或IT流程工作流,而是提供一个统一的界面来协调所有的IT自动化功能,因此Ansible的系统更加易用,部署更快。
    受管理的节点无需安装额外的远程控制软件,由平台通过SSH(Secure SHell)对其进行管理,因此十分方便。其模块支持JSON等标准输出格式,可采用任何编程语言重写。
    Ansible可以让用户避免编写脚本或代码来管理应用,同时还能搭建工作流实现IT任务的自动化执行。IT自动化可以降低技术门槛及对传统IT的依赖,从而加快项目的交付速度。
    目前,Ansible已有30万用户,每月下载量接近3万次。其客户包括AppDynamics、Evernote 、MapR以及金融服务、电信、医疗保健、媒体业的财富500强公司。

    Ansible默认是通过SSH通道来管理的,也就是它所说的免客户端方式管理, 它底层是通过 paramiko 来实现的。
    对Ansible底层工作机制感兴趣的可以参考这个:http://www.the5fire.com/explore-the-ansible.html

    ansible执行过程大体过程如下图,其中暖色调的代表已经模块化。

    wKiom1N1faXDDKHyAAJ2qmuQi9E804.jpg
    ansible有如下优点:
    1.3.1、轻量级,他不需要去客户端安装agent,更新时,只需要在操作机上进行一次更新即可
    1.3.2、批量任务执行可以写成脚本,而且不用分发到远程就可以执行
    1.3.3、使用python编写的,维护更简单
    1.3.4、支持sudo

    缺点:
    1.3.5、对于几千台、上万台机器的操作,还不清楚性能、效率情况如何,需要进一步了解。
    环境释放

    more /System/Library/CoreServices/SystemVersion.plist

    安装

    ruby -e "$(curl --insecure -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

    brew update

    brew install Ansible

    安装后hosts默认访问位置
    /usr/local/etc/ansible/hosts

    配置

    ansible.cfg默认位置/etc/ansible/ansible.cfg

    # config file for ansible -- http://ansible.com/# ==============================================# nearly all parameters can be overridden in ansible-playbook 
    # or with command line flags. ansible will read ANSIBLE_CONFIG,
    # ansible.cfg in the current working directory, .ansible.cfg in# the home directory or /etc/ansible/ansible.cfg, whichever it
    # finds first
    
    [defaults]   --->通用默认配置
    # some basic default values...
    inventory      = /etc/ansible/hosts     这个是默认库文件位置,脚本,或者存放可通信主机的目录
    #library        = /usr/share/my_modules/   Ansible默认搜寻模块的位置
    remote_tmp     = $HOME/.ansible/tmp   Ansible 通过远程传输模块到远程主机,然后远程执行,执行后在清理现场.在有些场景下,你也许想使用默认路径希望像更换补丁一样使用
    pattern        = *    如果没有提供“hosts”节点,这是playbook要通信的默认主机组.默认值是对所有主机通信
    forks          = 5    在与主机通信时的默认并行进程数 ,默认是5d
    poll_interval  = 15    当具体的poll interval 没有定义时,多少时间回查一下这些任务的状态, 默认值是5秒
    sudo_user      = root   sudo使用的默认用户 ,默认是root
    #ask_sudo_pass = True   用来控制Ansible playbook 在执行sudo之前是否询问sudo密码.默认为no
    #ask_pass      = True    控制Ansible playbook 是否会自动默认弹出密码
    transport      = smart   通信机制.默认 值为’smart’。如果本地系统支持 ControlPersist技术的话,将会使用(基于OpenSSH)‘ssh’,如果不支持讲使用‘paramiko’.其他传输选项包括‘local’, ‘chroot’,’jail’等等
    #remote_port    = 22    远程SSH端口。 默认是22
    module_lang    = C   模块和系统之间通信的计算机语言,默认是C语言
    
    # plays will gather facts by default, which contain information about
    # the remote system.
    #
    # smart - gather by default, but don't regather if already gathered# implicit - gather by default, turn off with gather_facts: False
    # explicit - do not gather by default, must say gather_facts: True
    gathering = implicit   控制默认facts收集(远程系统变量). 默认值为’implicit’, 每一次play,facts都会被收集
    
    # additional paths to search for roles in, colon separated
    #roles_path    = /etc/ansible/roles   roles 路径指的是’roles/’下的额外目录,用于playbook搜索Ansible roles
    
    # uncomment this to disable SSH key host checking
    #host_key_checking = False    检查主机密钥
    
    # change this for alternative sudo implementations
    sudo_exe = sudo     如果在其他远程主机上使用另一种方式执sudu操作.可以使用该参数进行更换
    
    # what flags to pass to sudo   传递sudo之外的参数
    #sudo_flags = -H
    
    # SSH timeout    SSH超时时间
    timeout = 10# default user to use for playbooks if user is not specified
    # (/usr/bin/ansible will use current user as default)
    #remote_user = root   使用/usr/bin/ansible-playbook链接的默认用户名,如果不指定,会使用当前登录的用户名
    
    # logging is off by default unless this path is defined
    # if so defined, consider logrotate
    #log_path = /var/log/ansible.log     日志文件存放路径
    
    # default module name for /usr/bin/ansible
    #module_name = command     ansible命令执行默认的模块
    
    # use this shell for commands executed under sudo
    # you may need to change this to bin/bash in rare instances
    # if sudo is constrained
    #executable = /bin/sh     在sudo环境下产生一个shell交互接口. 用户只在/bin/bash的或者sudo限制的一些场景中需要修改
    
    # if inventory variables overlap, does the higher precedence one win
    # or are hash values merged together?  The default is 'replace' but
    # this can also be set to 'merge'.
    #hash_behaviour = replace    特定的优先级覆盖变量
    
    # list any Jinja2 extensions to enable here:
    #jinja2_extensions = jinja2.ext.do,jinja2.ext.i18n      允许开启Jinja2拓展模块
    
    # if set, always use this private key file for authentication, same as # if passing --private-key to ansible or ansible-playbook
    #private_key_file = /path/to/file         私钥文件存储位置
    
    # format of string {{ ansible_managed }} available within Jinja2 
    # templates indicates to users editing templates files will be replaced.
    # replacing {file}, {host} and {uid} and strftime codes with proper values.
    ansible_managed = Ansible managed: {file} modified on %Y-%m-%d %H:%M:%S by {uid} on {host}   这个设置可以告知用户,Ansible修改了一个文件,并且手动写入的内容可能已经被覆盖.
    
    # by default, ansible-playbook will display "Skipping [host]" if it determines a task
    # should not be run on a host.  Set this to "False" if you don't want to see these "Skipping" # messages. NOTE: the task header will still be shown regardless of whether or not the 
    # task is skipped.
    #display_skipped_hosts = True     显示任何跳过任务的状态 ,默认是显示
    
    # by default (as of 1.3), Ansible will raise errors when attempting to dereference 
    # Jinja2 variables that are not set in templates or action lines. Uncomment this line
    # to revert the behavior to pre-1.3.
    #error_on_undefined_vars = False      如果所引用的变量名称错误的话, 将会导致ansible在执行步骤上失败
    
    # by default (as of 1.6), Ansible may display warnings based on the configuration of the
    # system running ansible itself. This may include warnings about 3rd party packages or
    # other conditions that should be resolved if possible.
    # to disable these warnings, set the following value to False:
    #system_warnings = True    允许禁用系统运行ansible相关的潜在问题警告
    
    # by default (as of 1.4), Ansible may display deprecation warnings for language
    # features that should no longer be used and will be removed in future versions.
    # to disable these warnings, set the following value to False:
    #deprecation_warnings = True     允许在ansible-playbook输出结果中禁用“不建议使用”警告
    
    # (as of 1.8), Ansible can optionally warn when usage of the shell and
    # command module appear to be simplified by using a default Ansible module
    # instead.  These warnings can be silenced by adjusting the following
    # setting or adding warn=yes or warn=no to the end of the command line 
    # parameter string.  This will for example suggest using the git module
    # instead of shelling out to the git command.
    # command_warnings = False    当shell和命令行模块被默认模块简化的时,Ansible 将默认发出警告
    
    
    # set plugin path directories here, separate with colons
    action_plugins     = /usr/share/ansible_plugins/action_plugins  
    callback_plugins   = /usr/share/ansible_plugins/callback_plugins
    connection_plugins = /usr/share/ansible_plugins/connection_plugins
    lookup_plugins     = /usr/share/ansible_plugins/lookup_plugins
    vars_plugins       = /usr/share/ansible_plugins/vars_plugins
    filter_plugins     = /usr/share/ansible_plugins/filter_plugins
    
    # by default callbacks are not loaded for /bin/ansible, enable this if you
    # want, for example, a notification or logging callback to also apply to 
    # /bin/ansible runs
    #bin_ansible_callbacks = False    用来控制callback插件是否在运行 /usr/bin/ansible 的时候被加载. 这个模块将用于命令行的日志系统,发出通知等特性
    
    
    # don't like cows?  that's unfortunate.
    # set to 1 if you don't want cowsay support or export ANSIBLE_NOCOWS=1 #nocows = 1    默认ansible可以调用一些cowsay的特性   开启/禁用:0/1# don't like colors either?# set to 1 if you don't want colors, or export ANSIBLE_NOCOLOR=1#nocolor = 1  输出带上颜色区别, 开启/关闭:0/1# the CA certificate path used for validating SSL certs. This path 
    # should exist on the controlling node, not the target nodes
    # common locations:
    # RHEL/CentOS: /etc/pki/tls/certs/ca-bundle.crt
    # Fedora     : /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
    # Ubuntu     : /usr/share/ca-certificates/cacert.org/cacert.org.crt
    #ca_file_path =    # the http user-agent string to use when fetching urls. Some web server
    # operators block the default urllib user agent as it is frequently used
    # by malicious attacks/scripts, so we set it to something unique to 
    # avoid issues.
    #http_user_agent = ansible-agent
    
    # if set to a persistent type (not 'memory', for example 'redis') fact values
    # from previous runs in Ansible will be stored.  This may be useful when
    # wanting to use, for example, IP information from one group of servers
    # without having to talk to them in the same playbook run to get their
    # current IP information.
    fact_caching = memory
    
    
    # retry files
    #retry_files_enabled = False
    #retry_files_save_path = ~/.ansible-retry
    
    [privilege_escalation]
    #become=True
    #become_method=sudo
    #become_user=root
    #become_ask_pass=False
    
    [paramiko_connection]
    
    # uncomment this line to cause the paramiko connection plugin to not record new host
    # keys encountered.  Increases performance on new host additions.  Setting works independently of the
    # host key checking setting above.
    #record_host_keys=False
    
    # by default, Ansible requests a pseudo-terminal for commands executed under sudo. Uncomment this# line to disable this behaviour.
    #pty=False
    
    [ssh_connection]
    
    # ssh arguments to use
    # Leaving off ControlPersist will result in poor performance, so use 
    # paramiko on older platforms rather than removing it
    #ssh_args = -o ControlMaster=auto -o ControlPersist=60s
    
    # The path to use for the ControlPath sockets. This defaults to
    # "%(directory)s/ansible-ssh-%%h-%%p-%%r", however on some systems with
    # very long hostnames or very long path names (caused by long user names or 
    # deeply nested home directories) this can exceed the character limit on
    # file socket names (108 characters for most platforms). In that case, you 
    # may wish to shorten the string below.
    # 
    # Example: 
    # control_path = %(directory)s/%%h-%%r
    #control_path = %(directory)s/ansible-ssh-%%h-%%p-%%r
    
    # Enabling pipelining reduces the number of SSH operations required to 
    # execute a module on the remote server. This can result in a significant 
    # performance improvement when enabled, however when using "sudo:" you must 
    # first disable 'requiretty' in /etc/sudoers
    #
    # By default, this option is disabled to preserve compatibility with
    # sudoers configurations that have requiretty (the default on many distros).
    # 
    #pipelining = False
    
    # if True, make ansible use scp if the connection type is ssh 
    # (default is sftp)
    #scp_if_ssh = True
    
    [accelerate]
    accelerate_port = 5099accelerate_timeout = 30accelerate_connect_timeout = 5.0# The daemon timeout is measured in minutes. This time is measured
    # from the last activity to the accelerate daemon.
    accelerate_daemon_timeout = 30 # If set to yes, accelerate_multi_key will allow multiple
    # private keys to be uploaded to it, though each user must
    # have access to the system via SSH to add a new key. The default# is "no".
    #accelerate_multi_key = yes
    
    [selinux]
    # file systems that require special treatment when dealing with security context
    # the default behaviour that copies the existing context or uses the user default# needs to be changed to use the file system dependant context.
    #special_context_filesystems=nfs,vboxsf,fuse
    

    简易配置:

    [defaults]
    inventory      = /etc/ansible/hosts
    sudo_user=root
    remote_port=22host_key_checking=False
    remote_user=root
    log_path=/var/log/ansible.log
    module_name=command
    private_key_file=/root/.ssh/id_rsa
    no_log:True
    

    hosts主机配置

    默认位置:/etc/ansible/hosts

    # This is the default ansible 'hosts' file.
    #
    # It should live in /etc/ansible/hosts
    #
    #   - Comments begin with the '#' character
    #   - Blank lines are ignored
    #   - Groups of hosts are delimited by [header] elements
    #   - You can enter hostnames or ip addresses
    #   - A hostname/ip can be a member of multiple groups
    
    # Ex 1: Ungrouped hosts, specify before any group headers.
    
    green.example.com
    blue.example.com192.168.100.1192.168.100.10# Ex 2: A collection of hosts belonging to the 'webservers' group
    
    [webservers]
    alpha.example.org
    beta.example.org192.168.1.100192.168.1.110# If you have multiple hosts following a pattern you can specify
    # them like this:
    
    www[001:006].example.com
    
    # Ex 3: A collection of database servers in the 'dbservers' group
    
    [dbservers]
    
    db01.intranet.mydomain.net
    db02.intranet.mydomain.net10.25.1.5610.25.1.57# Here's another example of host ranges, this time there are no# leading 0s:
    
    db-[99:101]-node.example.com
    

    公私钥配置

    创建公私钥
    1
    ssh-keygen -t rsa -C 'yat_ho@163.com'
    -t 指定密钥类型,默认即 rsa ,可以省略
    -C 设置注释文字,比如你的邮箱

    默认存放位置

    1
    /Users/jenkins/.ssh/id_rsa
    将公钥复制到ssh服务器

    1
    ssh-copy-id jenkins@192.168.1.236
    hosts配置
    定义主机与组
    定义一个IP为192.168.1.21, SSH端口为2135的主机

    1
    192.168.1.21:2135
    定义一个别名为jumper, SSH端口为22, IP为192.168.1.50的主机

    1
    jumper ansible_ssh_port=22 ansible_ssh_host=192.168.1.50
    组成员主机名称范例:

    1
    2
    [test]
    jenkis236 ansible_ssh_port=22 ansible_ssh_host=192.168.1.236
    假如你有很多主机遵循某一种模式,你还可以这样来表示他们:

    1
    2
    3
    4
    5
    6
    [webservers]
    web[1:50].lightcloud.com

    [database]
    db-[a:f].lightcloud.com
    定义主机变量

    主机可以指定变量, 后面可以供Playbooks调用

    1
    2
    [test]
    jenkis236 ansible_ssh_port=22 ansible_ssh_host=192.168.1.236 http_port=8080 
    定义组变量

    [atlanta]
    host1
    host2
    [atlanta:vars]
    ntp_server=ntp.atlanta.example.com
    proxy=proxy.atlanta.example.com
    Ansible内置连接主机的变量

    ansible_ssh_host
    ansible通过ssh连接的IP或者FQDN
    ansible_ssh_port
    SSH连接端口
    ansible_ssh_user
    默认SSH连接用户
    ansible_ssh_pass
    SSH连接的密码(这是不安全的,ansible极力推荐使用--ask-pass选项或使用SSH keys)
    ansible_sudo_pass
    sudo用户的密码
    ansible_connection
    SSH连接的类型:local,ssh,paramiko,在ansible 1.2之前默认是paramiko,后来智能选择,优先使用基于ControlPersist的ssh(支持的前提)
    ansible_ssh_private_key_file
    SSH连接的公钥文件
    ansible_shell_type
    指定主机所使用的shell解释器,默认是sh,你可以设置成csh, fish等shell解释器
    ansible_python_interpreter
    用来指定python解释器的路径
    ansible_*_interpreter
    用来指定主机上其他语法解释器的路径,例如ruby,perl等
     Ansible常用模块及API
    command: 执行远程主机SHELL命令

    ansible all -i /Users/jenkins/jenkins/lirbary/ansible_hosts/hosts_test -m command -a "ifconfig"
    script: 远程执行MASTER本地SHELL脚本.(类似scp+shell)

    ansible test -i /Users/jenkins/jenkins/lirbary/ansible_hosts/hosts_test -m script -a "../Env_update_shell/test.sh"
    copy:实现主控端向目标主机拷贝文件, 类似scp功能.

    1
    ansible test -i /Users/jenkins/jenkins/lirbary/ansible_hosts/hosts -m copy -a "src=~/test.sh dest=/tmp/ owner=root group=root mode=0755"
    stat:获取远程文件状态信息, 包括atime, ctime, mtime, md5, uid, gid等信息.

    1
    ansible test -i /Users/jenkins/jenkins/lirbary/ansible_hosts/hosts_test -m stat -a "path=/Users/jenkins/jenkins/"
    get_url:实现在远程主机下载指定URL到本地.

    1
    ansible test -i /Users/jenkins/jenkins/lirbary/ansible_hosts/hosts_test -m get_url -a "url=http://www.cnblogs.com/yatho dest=/tmp/index.html mode=0400 force=yes"
    yum:Linux包管理平台操作, 常见都会有yum和apt, 此处会调用yum管理模式

    1
    ansible servers -m yum -a "name=curl state=latest"
    cron:远程主机crontab配置

    1
    ansible webservers -m cron -a "name='check dir' hour='5,2' job='ls -alh > /dev/null'"
    service:远程主机系统服务管理

    1
    2
    3

    ansible webservers -m service -a "name=crond state=stopped"

    ansible webservers -m service -a "name=crond state=restarted"

    ansible webservers -m service -a "name=crond state=reloaded"

    user:user

    1
    2
    3
    4
    添加用户:

    ansible webservers -m user -a "name=johnd comment='John Doe'"

    删除用户:

    ansible webservers -m user -a "name=johnd state=absent remove=yes"

     playbook

    playbook介绍

    playbook是一个不同于使用Ansible命令行执行方式的模式, 其功能是将大量命令行配置集成到一起形成一个可定制的多主机配置管理部署工具.

    它通过YAML格式定义, 可以实现向多台主机的分发应用部署.

    以下给大家详细介绍一个针对nginx嵌套复用结构的playbook部署实例:

    1. 构建目录结构

    1
    2
    3
    4
    5

    cd /etc/ansible/

    mkdir group_vars

    mkdir roles

    2.定义host

    1
    2
    3
    4
    5
    6
    7
    8
    9

    vi /etc/ansible/hosts

    [webservers]
    client01.example.com
    client02.example.com
    [nginx01]
    client01.example.com
    [nginx02]
    client02.example.com
    3.定义变量

    1
    2
    3
    4
    5
    6
    7

    vi /etc/ansible/group_vars/nginx01

    worker_processes: 4
    num_cpus: 4
    max_open_file: 65506
    root: /data
    remote_user: root
    1
    2
    3
    4
    5
    6
    7

    vi /etc/ansible/group_vars/nginx02

    worker_processes: 2
    num_cpus: 2
    max_open_file: 35506
    root: /www
    remote_user: root
    Tips:这里在group_vars下定义的文件名必须对应hosts文件下的group标签, 通过这里定义的不同参数从而部署不同类型的主机配置.

    4.创建roles入口文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    vi /etc/ansible/site.yml

    • hosts: webservers
      roles:
      • base_env
    • hosts: nginx01
      roles:
      • nginx01
    • hosts: nginx02
      roles:
      • nginx02
          

    Tips: 这里的roles:下的字符串需对应roles目录下的目录名.

    5.定义全局role base_env

    创建目录结构

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23

    mkdir -p /etc/ansible/roles/base_env/tasks

    vi /etc/ansible/roles/base_env/tasks/main.yml

    将EPEL的yum源配置文件传送到客户端

    • name: Create the contains common plays that will run on all nodes
      copy: src=epel.repo dest=/etc/yum.repos.d/epel.repo
    • name: Create the GPG key for EPEL
      copy: src=RPM-GPG-KEY-EPEL-6 dest=/etc/pki/rpm-gpg

    关闭SELINUX

    • name: test to see if selling is running
      command: getenforce
      register: sestatus
      changed_when: false

    删除iptables默认规则并保存

    • name: remove the default iptables rules
      command: iptables -F
    • name: save iptables rules
      command: service iptables save
      将对应需要拷贝到远程的文件复制到base_env/files目录下

    1
    2
    3
    4
    5

    mkdir -p /etc/ansible/roles/base_env/files

    cp /etc/yum.repos.d/epel.repo /etc/ansible/roles/base_env/files

    cp /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 /etc/ansible/roles/base_env/files

    1. 定义nginx01和ngnix02 role

    创建目录结构

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30

    mkdir -p /etc/ansible/roles/nginx{01,02}

    mkdir -p /etc/ansible/roles/nginx01/tasks

    mkdir -p /etc/ansible/roles/nginx02/tasks

    vi /etc/ansible/roles/nginx01/tasks/main.yml

    安装nginx最新版本

    • name: ensure nginx is at the latest version
      yum: pkg=nginx state=latest

    将nginx配置文件传送到远程目录

    • name: write the nginx config file
      template: src=nginx.conf dest=/etc/nginx/nginx.conf
      notify: restart nginx # 重启nginx

    创建nginx根目录

    • name: Create Web Root
      file: dest={{ root }} mode=775 state=directory owner=nginx group=nginx
      notify: reload nginx
    • name: ensure nginx is running
      service: name=nginx state=restarted

    cp /home/ansible/roles/nginx01/tasks/main.yml /home/ansible/roles/nginx02/tasks/main.yml

      

    7.定义files

    mkdir -p /etc/ansible/roles/nginx01/templates

    mkdir -p /etc/ansible/roles/nginx02/templates

    vi /etc/ansible/roles/nginx01/templates/nginx.conf

    For more information on configuration, see:

    user nginx;
    worker_processes {{ worker_processes }};
    {% if num_cpus == 2 %}
    worker_cpu_affinity 01 10;
    {% elif num_cpus == 4 %}
    worker_cpu_affinity 1000 0100 0010 0001;
    {% elif num_cpus >= 8 %}
    worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
    {% else %}
    worker_cpu_affinity 1000 0100 0010 0001;
    {% endif %}
    worker_rlimit_nofile {{ max_open_file }};

    error_log /var/log/nginx/error.log;

    error_log /var/log/nginx/error.log notice;

    error_log /var/log/nginx/error.log info;

    pid /var/run/nginx.pid;

    events {
    worker_connections {{ max_open_file }};
    }

    http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' 
                      '$status $body_bytes_sent "$http_referer" ' 
                      '"$http_user_agent" "$http_x_forwarded_for"'; 
    
    access_log  /var/log/nginx/access.log  main; 
    
    sendfile        on; 
    #tcp_nopush     on; 
    
    #keepalive_timeout  0; 
    keepalive_timeout  65; 
    
    #gzip  on; 
       
    # Load config files from the /etc/nginx/conf.d directory 
    # The default server is in conf.d/default.conf 
    #include /etc/nginx/conf.d/*.conf; 
    server { 
        listen       80 default_server; 
        server_name  _; 
    
        #charset koi8-r; 
    
        #access_log  logs/host.access.log  main; 
    
        location / { 
            root   {{ root }}; 
            index  index.html index.htm; 
        } 
    
        error_page  404              /404.html; 
        location = /404.html { 
            root   /usr/share/nginx/html; 
        } 
    
        # redirect server error pages to the static page /50x.html 
        # 
        error_page   500 502 503 504  /50x.html; 
        location = /50x.html { 
            root   /usr/share/nginx/html; 
        } 
    
    } 
    

    }
      

    Tip: worker_processes, num_cpus, max_open_file, root等参数会调用group_vars目录下配置文件中相应的变量值

    1

    cp /etc/ansible/roles/nginx01/templates/nginx.conf /etc/ansible/roles/nginx02/templates/nginx.conf

    8.执行playbook

    1

    ansible-playbook -i /etc/ansible/hosts /etc/ansible/site.yml -f 10

    Tips: -f 为启动10个并行进程执行playbook, -i 定义inventory host文件, site.yml 为入口文件

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

    GATHERING FACTS ***************************************************************
    ok: [client02.example.com]
    ok: [client01.example.com]

    TASK: [base_env | Create the contains common plays that will run on all nodes] ***
    ok: [client01.example.com]
    ok: [client02.example.com]

    TASK: [base_env | Create the GPG key for EPEL] ********************************
    ok: [client02.example.com]
    ok: [client01.example.com]

    TASK: [base_env | test to see if selling is running] **************************
    ok: [client01.example.com]
    ok: [client02.example.com]

    TASK: [base_env | remove the default iptables rules] **************************
    changed: [client02.example.com]
    changed: [client01.example.com]

    TASK: [base_env | save iptables rules] ****************************************
    changed: [client01.example.com]
    changed: [client02.example.com]

    PLAY [nginx01] ****************************************************************

    GATHERING FACTS ***************************************************************
    ok: [client01.example.com]

    TASK: [nginx01 | ensure nginx is at the latest version] ***********************
    ok: [client01.example.com]

    TASK: [nginx01 | write the nginx config file] *********************************
    ok: [client01.example.com]

    TASK: [nginx01 | Create Web Root] *********************************************
    ok: [client01.example.com]

    TASK: [nginx01 | ensure nginx is running] *************************************
    changed: [client01.example.com]

    PLAY [nginx02] ****************************************************************

    GATHERING FACTS ***************************************************************
    ok: [client02.example.com]

    TASK: [nginx02 | ensure nginx is at the latest version] ***********************
    ok: [client02.example.com]

    TASK: [nginx02 | write the nginx config file] *********************************
    ok: [client02.example.com]

    TASK: [nginx02 | Create Web Root] *********************************************
    ok: [client02.example.com]

    TASK: [nginx02 | ensure nginx is running] *************************************
    changed: [client02.example.com]

    PLAY RECAP ********************************************************************
    client01.example.com : ok=11 changed=3 unreachable=0 failed=0
    client02.example.com : ok=11 changed=3 unreachable=0 failed=0
    最终部署目录结构如下

    tree /etc/ansible/

    /etc/ansible/
    ├── ansible.cfg
    ├── group_vars
    │ ├── nginx01
    │ └── nginx02
    ├── hosts
    ├── hosts.bak
    ├── roles
    │ ├── base_env
    │ │ ├── files
    │ │ │ ├── epel.repo
    │ │ │ └── RPM-GPG-KEY-EPEL-6
    │ │ └── tasks
    │ │ └── main.yml
    │ ├── nginx01
    │ │ ├── tasks
    │ │ │ └── main.yml
    │ │ └── templates
    │ │ └── nginx.conf
    │ └── nginx02
    │ ├── tasks
    │ │ └── main.yml
    │ └── templates
    │ └── nginx.conf
    └── site.yml
    jenkins关联配置
    Choice Parameter
      deploy_environment  定义部署环境名 dev,test,uat,pdt
    Execute shell
      开头和结尾的set +x, set -x用来打开和关闭该部分的扩展参数及命令
      cd $WORKSPACE/leon-playbook-phpcms1.1
      ansible --version
      ansible-playbook -i inventory/$deploy_environment ./deploy.yml -e project=phpcms -e branch=$branch_selector -e env=$deploy_environment
      -i 用来自定义ansible host文件路径, ./deploy.yml为ansible-playbook入口文件, -e 后可跟给当前session添加的环境变量.

    欢迎评论交流
  • 相关阅读:
    Codeforces 451A Game With Sticks
    POJ 3624 Charm Bracelet
    POJ 2127 Greatest Common Increasing Subsequence
    POJ 1458 Common Subsequence
    HDU 1087 Super Jumping! Jumping! Jumping!
    HDU 1698
    HDU 1754
    POJ 1724
    POJ 1201
    CSUOJ 1256
  • 原文地址:https://www.cnblogs.com/linengier/p/11038030.html
Copyright © 2011-2022 走看看