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添加的環境變量.