Ansible部署

配置文件

建议使用~/.ansible.cfg作为配置文件使用,这样就可以实现每个用户都有自己独自的配置文件,不污染其它用户正常使用Ansible,同时也方便进行选项配置。

  • 优先级说明
  1. ANSIBLE_CFG:首先,Ansible会检查环境变量,及这个环境变量指向的配置文件
  2. ./ansible.cfg:其次,检查当前目录下的ansible.cfg配置文件
  3. ~/.ansible.cfg:再次,检查当前用户家目录下的 .ansible.cfg 配置文件
  4. /etc/ansible/ansible.cfg:最后,检查安装Ansible自动生产的配置文件
  • 配置项

    defaults配置

    配置项说明默认值
    inventoryansible inventory文件路径/etc/ansible/hosts
    libraryansible模块文件路径/usr/share/my_modules/
    remote_tmpansible远程主机脚本临时存放目录~/.ansible/tmp
    local_tmpansible管理节点脚本临时存放目录~/.ansible/tmp
    forksansible执行并发数5
    poll_intervalansible异步任务查询间隔15
    sudo_useransible sudo用户root
    ask_sudo_pass运行ansible是否提示输入sudo密码True
    ask_pass运行ansible是否提示输入密码True
    transportansible远程传输模式smart
    remote_port远程主机SSH端口22
    module_langansible模块运行默认语言环境C
    gatheringfacts信息收集开关定义smart
    roles_pathansible role存放路径/etc/ansible/roles
    timeoutansible SSH连接超时时间10
    remote_useransible远程认证用户root
    log_pathansible日志记录文件/var/log/ansible.log
    module_nameansible默认执行模块command
    executableansible命令执行shell/bin/sh
    hash_behaviouransible主机变量重复处理方式replace
    private_role_vars默认情况下,角色中的变量将在全局变量范围中可见。 为了防止这种情况,可以启用以下选项,只有tasks的任务和handlers得任务可以看到角色变量yes
    vault_password_file指定vault密码文件路径
    ansible_managed定义的一个Jinja2变量,可以插入到Ansible配置模版系统生成的文件中Ansible managed
    display_skipped_hosts开启显示跳过的主机True
    error_on_undefined_vars开启错误,或者没有定义的变量False
    action_pluginsansible action插件路径
    cache_pluginsansible cache插件路径
    callback_pluginsansible callback插件路径
    connection_pluginsansible connection插件路径
    lookup_pluginsansible lookup插件路径
    inventory_pluginsansible inventory插件路径
    vars_pluginsansible vars插件路径
    filter_pluginsansible filter插件路径
    terminal_pluginsansible terminal插件路径
    strategy_pluginsansible strategy插件路径
    fact_caching定义ansible facts缓存方式memory
    fact_caching_connection定义ansible facts缓存路径

    privilege_escalation配置

    配置项说明默认值
    become是否开启become模式True
    become_method定义become方式sudo
    become_user定义become方式root
    become_ask_pass是否定义become提示密码False

目录标准化

/opt/ansible/playbook  # playbook文件纳入本地git仓库管理
/opt/ansible/server/ansible-2.7.10  # ansible源码安装目录
/root/.ansible.cfg /opt/ansible/playbook/inventory  # 主机清单文件、ansible配置文件

环境变量配置

# /etc/profile
export PATH=$PATH:/opt/ansible/server/ansible-2.7.10/bin
source /etc/profile

常见文件存放位置

文件说明默认值
ansible.cfg用户家目录下的 .ansible.cfg/etc/ansible/ansible.cfg
group_vars和host_vars一般和inventory文件存在一个目录内/etc/ansible/{group_vars,host_vars}
host配置文件inventory参数配置/etc/ansible/hosts
roles_pathroles存放目录,一般优先查找playbook当前目录下roles,即使配置了roles_path/etc/ansible/roles(可配置多个,以:隔开)

配置文件示意

[defaults]

#主机清单配置

inventory = /opt/ansible/playbook/inventory

#插件库目录

library = /opt/ansible/playbook/module/ansible

#远端临时目录,ansible执行模块会将相应信息scp到远端临时目录
remote_tmp = $HOME/.ansible/tmp

#本地临时目录
local_tmp = $HOME/.ansible/tmp

#并行数,默认是5
forks = 20

#执行远程ssh的用户
remote_user = opsadmin

#通信机制,默认为smart,如果本地系统支持 ControlPersist技术的话,将会使用(基于OpenSSH)‘ssh’,如果不支持讲使用‘paramiko’

transport = smart

#默认为implicit每次都收集;smart表示如果facts有缓存,则使用缓存数据.
gathering = smart

#role目录,默认优先查找playbook同级的role目录
roles_path = /etc/ansible/roles

#日志路径
log_path = /var/log/ansible.log

#默认执行模块名

module_name = command

#playbook中role定义的var,只能在本role中运行

private_role_vars = yes

#执行任务不会显示skip状态主机信息
display_skipped_hosts = True

#如果ansible引入未定义变量,会报错
error_on_undefined_vars = True

#ssh key目录
private_key_file = /root/.ssh/opsadmin-id_rsa

#不开启ssh过程主机key检查
host_key_checking = False

#超时时间
timeout = 60

#fact缓存时间
fact_caching_timeout = 86400

#fact缓存方式,支持json文件、redis等(目前redis只能部署在控制机)
fact_caching = jsonfile

#fact缓存目录
fact_caching_connection = $HOME/.ansible/tmp
[privilege_escalation]

#支持sudo
become=True
become_method=sudo

#sudo用户
become_user=root

#sudo命令是否输入密码
become_ask_pass=False
[ssh_connection]

#开启ssh连接持久化
ssh_args = -C -o ControlMaster=auto -o ControlPersist=5m
control_path_dir = $HOME/.ansible/cp
control_path = %(directory)s/%%h-%%r

#开启pipelining:所有的任务都会放在一个管道执行,减少了ssh连接建立次数
pipelining = True

#默认临时文件通过sftp进行传递,如果没stfp,采用scp进行传递
#scp_if_ssh=False
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值