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强公司。php
Ansible默认是经过SSH通道来管理的,也就是它所说的免客户端方式管理, 它底层是经过 paramiko 来实现的。
对Ansible底层工做机制感兴趣的能够参考这个:http://www.the5fire.com/explore-the-ansible.htmlhtml
ansible执行过程大致过程以下图,其中暖色调的表明已经模块化。node
wKiom1N1faXDDKHyAAJ2qmuQi9E804.jpg
ansible有以下优势:
1.3.一、轻量级,他不须要去客户端安装agent,更新时,只须要在操做机上进行一次更新便可
1.3.二、批量任务执行能够写成脚本,并且不用分发到远程就能够执行
1.3.三、使用python编写的,维护更简单
1.3.四、支持sudopython
缺点:
1.3.五、对于几千台、上万台机器的操做,还不清楚性能、效率状况如何,须要进一步了解。
环境释放linux
安装后hosts默认访问位置
/usr/local/etc/ansible/hostsnginx
ansible.cfg默认位置/etc/ansible/ansible.cfggit
# 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
简易配置:github
[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主机配置web
默认位置:/etc/ansible/hostsredis
# 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
user:user
1
2
3
4
添加用户:
删除用户:
playbook
playbook介绍
playbook是一个不一样于使用Ansible命令行执行方式的模式, 其功能是将大量命令行配置集成到一块儿造成一个可定制的多主机配置管理部署工具.
它经过YAML格式定义, 能够实现向多台主机的分发应用部署.
如下给你们详细介绍一个针对nginx嵌套复用结构的playbook部署实例:
1
2
3
4
5
2.定义host
1
2
3
4
5
6
7
8
9
[webservers]
client01.example.com
client02.example.com
[nginx01]
client01.example.com
[nginx02]
client02.example.com
3.定义变量
1
2
3
4
5
6
7
worker_processes: 4
num_cpus: 4
max_open_file: 65506
root: /data
remote_user: root
1
2
3
4
5
6
7
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
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
1
2
3
4
5
建立目录结构
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
7.定义files
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;
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
8.执行playbook
1
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
最终部署目录结构以下
/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添加的环境变量.