配置文件詳解
配置文件中大多數都是註釋行默認配置項。文件遵循INI格式,分爲幾個類別的配置,下面會分別介紹。
在那之前,先看一下配置文件存放的位置,以及如何從官網獲取配置文件模板。
配置文件的位置
Ansible只有一個配置文件ansible.cfg。配置文件可以存在於多個位置,按下面的順序查找到的第一個生效:
- ANSIBLE_CONFIG (環境變量指定)
- ansible.cfg (當前命令執行目錄)
- ~/.ansible.cfg (用戶家目錄下)
- /etc/ansible/ansible.cfg
只有最先找到的配置文件會生效。另外環境變量的優先級比配置文件高,而且可以一個一個單獨設置。每個配置項對應的環境變量的名稱,可以在下面的官方文檔中查到:
https://docs.ansible.com/ansible/latest/reference_appendices/config.html#common-options
還有命令行參數指定的方式,這個是優先級比上面的配置方式高。不過playbook中指定的設置優先級更高,不會被命令行參數覆蓋。
獲取配置文件模板
Ansible安裝後,默認會在/etc/ansible/目錄下生成配置文件。但是如果是通過pip或者是源碼安裝的話,就沒有這個文件,需要手動創建。可以到官方的github上去下一個最新的:
$ mkdir /etc/ansible
$ cd /etc/ansible
$ wget https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg
[defaults]
定義常規的連接類配置:
[defaults]
# some basic default values...
#inventory = /etc/ansible/hosts # 定義Inventory,用於定義主機列表配置
#library = /usr/share/my_modules/ # 自定了lib庫存放目錄
#module_utils = /usr/share/my_module_utils/
#remote_tmp = ~/.ansible/tmp # 臨時文件遠程主機存放目錄
#local_tmp = ~/.ansible/tmp # 臨時文件本地存放目錄
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks = 5 # 默認開啓的併發數
#poll_interval = 15 # 默認輪詢時間間隔
#sudo_user = root # 默認sudo用戶
#ask_sudo_pass = True # 是否需要sudo密碼
#ask_pass = True # 是否需要密碼
#transport = smart
#remote_port = 22
#module_lang = C
#module_set_locale = False
# additional paths to search for roles in, colon separated
#roles_path = /etc/ansible/roles # 默認下載的Roles存在的目錄
# uncomment this to disable SSH key host checking
#host_key_checking = False # 首次連接是否需要檢查key認證,建議放開註釋設爲False
# SSH timeout
#timeout = 10 # 默認超時時間
# 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 # 默認執行模塊
# set plugin path directories here, separate with colons
#action_plugins = /usr/share/ansible/plugins/action # 各類插件的存放目錄
#become_plugins = /usr/share/ansible/plugins/become
#cache_plugins = /usr/share/ansible/plugins/cache
#callback_plugins = /usr/share/ansible/plugins/callback
#connection_plugins = /usr/share/ansible/plugins/connection
#lookup_plugins = /usr/share/ansible/plugins/lookup
#inventory_plugins = /usr/share/ansible/plugins/inventory
#vars_plugins = /usr/share/ansible/plugins/vars
#filter_plugins = /usr/share/ansible/plugins/filter
#test_plugins = /usr/share/ansible/plugins/test
#terminal_plugins = /usr/share/ansible/plugins/terminal
#strategy_plugins = /usr/share/ansible/plugins/strategy
# 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 # getfact 緩存的主機信息存放方式
# retry files
# When a playbook fails a .retry file can be created that will be placed in ~/
# You can enable this feature by setting retry_files_enabled to True
# and you can change the location of the files by setting retry_files_save_path
#retry_files_enabled = False
#retry_files_save_path = ~/.ansible-retry # 錯誤重啓文件存放目錄
上面這些配置多數都保持默認即可。只有一項host_key_checking = False
可以放開註釋。
[privilege_escalation]
處於安全考慮,有時候不希望直接以root用戶直接部署應用,這時候就需要給普通用戶sudo權限,這部分配置主要就是針對sudo用戶提權的配置:
[privilege_escalation]
#become=True # 是否sudo
#become_method=sudo # sudo方式
#become_user=root # sudo後變爲root用戶
#become_ask_pass=False # sodu後是否驗證密碼
[paramiko_connection]
這部分功能不常用了,瞭解下把:
[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 # 不記錄新主機的key以提升效率
# by default, Ansible requests a pseudo-terminal for commands executed under sudo. Uncomment this
# line to disable this behaviour.
#pty=False # 禁用sudo功能
[ssh_connection]
Ansible默認使用ssh連接主機,這裏是SSH連接的一些配置:
# 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 # 管道加速功能,需配合requiretty使用方可生效
這塊的配置項也不多,多數還是保持默認即可。
[accelerate]
Ansible連接加速相關配置:
[accelerate]
#accelerate_port = 5099 # 加速連接端口
#accelerate_timeout = 30 # 命令執行超時時間,單位秒
#accelerate_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
這裏的配置項在提供Ansible連接速度時會涉及,多數還是保持默認
[selinux]
selinux幾乎不會用,配置上還是保持默認:
# Set this to yes to allow libvirt_lxc connections to work without SELinux.
#libvirt_lxc_noseclabel = yes
[colors]
輸出結果顏色的設置。原本的配置已經很好了,幾乎不用修改,保持默認:
[colors]
#highlight = white
#verbose = blue
#warn = bright purple
#error = red
#debug = dark gray
#deprecate = purple
#skip = cyan
#unreachable = red
#ok = green
#changed = yellow
#diff_add = green
#diff_remove = red
#diff_lines = cyan
Inventory 配置詳解
Inventory是Ansible管理主機的配置文件,默認位置是/etc/ansible/hosts,這個在ansible.cfg配置文件的開頭有定義。
另外在ansible命令行中可以使用-i參數來指定要使用的Inventory文件。
註釋:使用#來寫註釋內容。
定義主機和組
寫在中括號中的內部表示組名。主機名可以是IP地址也可以是Hostname。主機名可以出現多次,這樣可以寫到多個組裏。
如果主機使用了非默認的SSH端口,則可以在主機名後加冒號指定SSH端口號。
配置示例::
# 配置示例
# 可以用IP地址
192.168.1.1
# 也可以使用Hostname
www.ansible.com
docs.ansible.com:2222
# 使用中括號表示一個分組的開始,主機與主機之間可以有空行不影響分組
[webservers]
web1.ansible.com
# 用[10:20]表示連續的數組,包括10和20
web[10:20].ansible.com
[dbservers]
db-a.ansible.com
# 也可以用中括號處理連續的字母
db-[b:f].ansible.com
自定義變量
對於一些非標準化的配置需求,可以在Inventory配置中進行設置。這樣可以滿足對於主機的一些個性化設置要求。
Ansible支持多種方式修改或自定義變量,Inventory是其中的一種修改方式。
定義主機變量
在定義主機時可以同時定義主機變量:
[webserverrs]
web1.ansible.com http_port=8000 # 自定義http_port的端口號爲8000
定義組變量
還可以定義組變量,同時對一組主機修改或自定義變量:
[groupservers]
web1.ansible.com
web2.ansible.com
[groupservers:vars]
http_port=8000
Default groups
Ansible還定義好了兩個默認組:
- all: 包含所有主機
- ungrouped: 包含沒有組的所有主機
組的嵌套
Inventory中的組還可以包含其他的組,就是嵌套。嵌套時要在大組的名字後加上:children,表示嵌套的成員是組名而不是主機名:
[apache]
httpd1.ansible.com
httpd2.ansible.com
[nginx]
ngx1.ansible.com
ngx2.ansible.com
[webservers:children]
apache
nginx
[webservers:vars]
ntp_server=ntp1.aliyun.com
對於嵌套的組也是可以設置組變量的,這個和普通的組變量一樣。
多重變量定義
變量除了可以在Inventory中定義,還可以獨立於Inventory文件之外單獨定義到一個配置文件中。
下面是設置變量的各種途徑,按優先級順序排列:
- command line values (eg “-u user”)
- role defaults
- inventory file or script group vars
- inventory group_vars/all
- playbook group_vars/all
- inventory group_vars/*
- playbook group_vars/*
- inventory file or script host vars
- inventory host_vars/*
- playbook host_vars/*
- host facts / cached set_facts
- play vars
- play vars_prompt
- play vars_files
- role vars (defined in role/vars/main.yml)
- block vars (only for tasks in block)
- task vars (only for the task)
- include_vars
- set_facts / registered vars
- role (and include_role) params
- include params
- extra vars (always win precedence)
比較常用的是如下4種:
- Inventory配置文件
- Playbook中vars定義的區域
- Roles中vars目錄下的文件(roles/X/vars/main.yml)
- group_vars和hosts_vars目錄下的文件
單獨定義vars配置文件,比如有個主機foosball,同時屬於raleigh和webservers組,那麼變量定義在以主機名或組名定義的如下3個文件中都有效:
- /etc/ansible/group_vars/raleigh
- /etc/ansible/group_vars/webservers
- /etc/ansible/host_vars/foosball
用於SSH連接的參數
Ansible基於SSH連接Inventory中指定遠程主機時,還內置了一些參數,用於指定連接時的交互方式,下面列舉了比較常用的參數:
- ansible_ssh_host: 指定連接主機
- ansible_ssh_port: 指定SSH連接端口,默認22
- ansible_ssh_user: 指定SSH連接用戶
- ansible_ssh_pass: 指定SSH連接密碼
- ansible_ssh_private_key_file: 指定私鑰文件
連接要用到連接插件,插件會用到自己的變量,比如上面的這幾個就是。下面的幾個是通用的連接變量,這些變量插件也是可以識別的,效果是一樣的。
下面3個是通用的連接用的變量:
- ansible_host
- ansible_port
- ansible_user
這裏的3個是通用的,優先級上應該會被上面的專用的覆蓋掉。
SSH插件一共有哪些變量,這些變量的設置方式和對應的名稱,包括默認值、環境變量、所有對應的變量名,可以查官方文檔:
https://docs.ansible.com/ansible/latest/plugins/connection/ssh.html#ssh-connection
所有的連接查看,看這裏:
https://docs.ansible.com/ansible/latest/plugins/connection.html?highlight=ansible_ssh_host