ansible2.9 官方文档理解看文档笔记

ansible概述

默认使用ssh协议管理机器,用中心站点管理整个远程机器群;
术语:控制节点,受控节点

ansible安装先决条件:

要求控制节点能使用ssh和托管节点继续通信,不支持win。
note:macos。默认配置少量句柄,openfile少。如果超过15或多fork。
sudo launchctl limit maxfiles unlimited
通信方式,ssh。默认sttp。如果不用sftp可以使用scp。可以在ansible.cfg配置中变更切换到 SCP;
默认情况下,Ansible 使用位于 的 Python 解释器/usr/bin/python来运行其模块
您可以设置ansible_python_interpreter清单变量,可以使用目的主机变量设置python执行版本:ansible_python_interpreter
Examples from an Ansible-INI host file:

配置ansible

配置文件:

/etc/ansible/ansible.cfg
如果是从pip安装ansible。需要创建此文件来覆盖ansible中的默认配置
示例文件:ansible/examples/ansible.cfg
https://github.com/ansible/ansible/blob/stable-2.9/examples/ansible.cfg
命令行选项:
可用选项的完整列表在ansible-playbook和ansible中。
ansible指南
基本清单:/etc/ansible/hosts
group配置:

yaml配置:

inventory
托管节点。默认配置文件/etc/ansible/hosts,也可以从命令行中指定不同的hosts文件,也可以使用多个hosts文件;
hosts中有2个默认组。all。ungrouped。
all包含所有主机,ungrouped包含处理group之外没有其他组的所有主机;
每个主机至少属于2个组。all。和ungrouped或者all以及其他一些组;
可以在配置中添加不同的配置:

ini:
[databases] //组名
db-[a:f].example.com //匹配到的hostname
yaml:
  webservers:    //组名 
hosts:       
www[01:50].example.com://匹配到的hostname
将变量分配给一台机器:主机变量
变量分配给单个主机,然后稍后在剧本中使用它。
ini:
[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909
yaml:
atlanta:
  host1:
    http_port: 80
    maxRequestsPerChild: 808
  host2:
    http_port: 303
    maxRequestsPerChild: 909

非标准ssh端口

badwolf.example.com:5309

连接变量也可以作为宿主变量使用:

[targets]

localhost              ansible_connection=local
other1.example.com     ansible_connection=ssh        ansible_user=myuser
other2.example.com     ansible_connection=ssh        ansible_user=myotheruser

使用多个库存来源

ansible-playbook get_logs.yml -i staging -i production

如果清单中存在变量冲突,他们将会合并变量,合并顺序由库存源参数的顺序控制
如果all:vars没有定义myvar = 1,生产定义了myvar = 2。playbook讲运行myvar = 2;

定义hosts目录:
ansible-playbook example.yml -i inventory
如果目录中存在冲突的的组依赖,其他hosts源,可以使用控制合并顺序很有用,清单根据文件名按照字母顺序来控制结果,可以添加文件名前缀

inventory/
01-openstack.yml # configure inventory plugin to get hosts from Openstack cloud
02-dynamic-inventory.py # add a dditional hosts with dynamic inventory script
03-static-inventory # add static hosts
group_vars/
all.yml # assign variables to all hosts

先执行01—02—03
If 01-openstack.yml defines myvar = 1 for the group all, 02-dynamic-inventory.py defines myvar = 2, and 03-static-inventory defines myvar = 3, the playbook will be run with myvar = 3.

hosts配置清单:
ansible_connection
ansible链接插件的名称,ssh协议类型是smart,ssh,paramike 默认是smart

ansible_host
ansible_port//ssh连接时候使用的ssh port—默认22for ssh
ansible_user //连接时候使用的用户名
ansible_password //连接时候使用的用户密码,比如说 groupname_password
密码最好的方式是创建一个子目录,group_vars/,named 在子目录中创建2个files,vars 和vault。
vars中 定义所有的需要的变量,包含任何敏感信息,然后cp 所有的敏感的var写到vault文件,vault文件中,使用变量前缀 vault_groupname_password。你可以使用这些变量在这个vars file中指向这个matching groupname_password 变量。

特定对于Specific to the SSH connection:
ansible_ssh_private_key_file
ansible_ssh_common_args
ansible_sftp_extra_args
ansible_scp_extra_args
ansible_ssh_extra_args
ansible_ssh_pipelining
这个配置决定是否采用ssh pipelining,这个配置可以覆盖掉在ansiblecfg中pipeling的的配置
ansible_ssh_executable (added in version 2.2)
ssh执行工具。默认是ssh

权限升级问题:
ansible_become
ansible_sudo
ansible_su
允许用户force执行权限升级;
sudo方式
ansible_become_method
sudo用户
ansible_become_user
ansible_sudo_user
ansible_su_user

ansible_become_password
ansible_sudo_password
ansible_su_password

ansible_become_exe
ansible_sudo_exe
ansible_su_exe

ansible_become_flags
ansible_sudo_flags
ansible_su_flags
此项也可以使用配置文件定义:
ansible.cfg in the sudo_flags option

ansible_shell_executable默认:/bin/sh.
ansible_shell_type //The shell type of the target system.
ansible_shell_executable 和此项一起用;

The target host python path.//目标主机python
ansible_python_interpreter

ansible_*_interpreter
匹配类型与python的二级制命令
Works for anything such as ruby or perl and works just like ansible_python_interpreter.

Examples from an Ansible-INI host file:

some_host ansible_port=2222 ansible_user=manager
aws_host ansible_ssh_private_key_file=/home/example/.ssh/aws.pem
freebsd_host ansible_python_interpreter=/usr/local/bin/python
ruby_module_host ansible_ruby_interpreter=/usr/bin/ruby.1.9.3

Ansible执行playbook使用ssh。但是不限于使用ssh。连接方式。可以with host 特定的连接方式
Non-SSH connection types

ansible_connection=

This connector can be used to deploy the playbook to the control machine itself.
ansible_connection=local

此项可以部署这个playbook,直接使用local本地的docker client来连接docker container。
ansible_connection=docker
下面的参数可以被使用在这个connector中:
ansible_host //The name of the Docker container to connect to.
ansible_user //The user name to operate within the container. The user must exist inside the container.

ansible_become

ansible_docker_extra_args

an example of how to instantly deploy to created containers:

  • name: create jenkins container
    docker_container:
    docker_host: myserver.net:4243
    name: my_jenkins
    image: jenkins

  • name: add container to inventory
    add_host:
    name: my_jenkins
    ansible_connection: docker
    ansible_docker_extra_args: "

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值