ansible01-ansible安装配置

中文权威指南
http://www.ansible.com.cn/docs/playbooks_variables.html#yaml

安装

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
wget -O /etc/yum.repos.d/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo
依赖   paramiko、PyYAML、Jinja2、httplib2、six
yum install ansible

配置

vim /etc/ansible/ansible.cfg
inventory = /etc/ansible/hosts    # 指定分组文件路径
host_key_checking = False         # ssh主机不用输入yes

加快ssh连接速度

vim /root/ssh/sshd_config
UseDNS no

定义主机

vim /etc/ansible/hosts
[all:children]
web
db
other

[web]
web1
web2

[db]
db[1:20]    # 指定多台主机

[other]
cache ansible_ssh_user="root" ansible_ssh_pass="password"  # 自定义ssh参数

列出所有主机

ansible all --list-hosts

批量部署证书文件

主机无密钥连接其他设备
ssh-keygen -t rsa -b 2048 -N '' -f ~/.ssh/id_rsa -q
ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(cat ~/.ssh/id_rsa.pub)'" -k

# -a 后的参数用空格分开,写在一行
# -k 输入的是被连接的主机密码,如果密码不同,将密码定义在host文件中,如下所示

例如
[test]
10.xxx ansible_connection=ssh ansible_ssh_user=xxx ansible_ssh_pass="xxxxxx"
10.xxx ansible_connection=ssh ansible_ssh_user=xxx ansible_ssh_pass="xxxxxx"

# user=root              给哪个用户部署公钥
# exclusive=true         密钥追加在已部署密钥之后
# manage_dir=true        自动赋权和创建文件夹
# key='$(cat ~/.ssh/id_rsa.pub)'    指定公钥的内容

2.详细配置 用法 原理

命令选项

ansible命令格式:  ansible 主机组 选项

选项
-i 指定主机文件路径,默认/etc/ansible/hosts
-M 指定模块的路径
-m 使用模块,默认command模块
-a 模块参数
-k 使用交互式登陆密码
-K 询问root用户的ssh密码
-b 以root身份访问host主机
-e 定义变量
-v 详细信息 
-vvvv 开启debug模式

ansible配置文件查找顺序

(1)环境变量 ANSIBLE_CONFIG定义的配置文件,ANSIBLE_CONFIG环境变量可以改变,后边加ansible.cfg的路径
(2)当前目录下的./ansible.cfg文件
(3)当前用户家目录下~/ansible.cfg文件
(4)/etc/ansible/ansible.cfg文件(默认配置文件路径)

inventory文件参数说明(解决特殊的网络环境)

ansible_ssh_host
将要连接的远程主机名与你想要的设定的主机别名不同,可以通过此变量设置

ansible_ssh_port
ssh端口号:如果不是默认的端口号,通过此变量设置

ansible_ssh_user
默认的ssh用户名

ansible_ssh_pass
ssh密码(这种方式并不安全,我们强烈建议使用--ask-pass或SSH密钥

ansible_sudo_pass
sudo密码(建议使用--ask-sudo-pass)

ansible_sudo_exe(new in version 1.8)
sudo命令路径(适用于1.8及以上版本)

ansible_connection
与主机的连接类型,如:local,ssh或paramiko,1.2以前默认使用patamilk,1.2。以后默认使用‘smart’,它会根据是否支持ControlPersist来判断‘ssh’方式是否可行

ansible_ssh_private_key_file
ssh使用的私钥文件,使用于有多个密钥,而你不想使用SSH代理的情况

Ansible_shell_type
目标喜用的shell类型,默认情况下,命令的执行使用sh语法,可设置为csh或fish

ansible_python_interpreter
目标主机的python路径,使用情况:系统中有多个Python,或者命令路径不是“/usr /bin/python”

其他运维工具

Saltstack    Chef

目录结构

/usr/bin/ansible              主程序,临时命令执行工具  
/usr/bin/ansible-doc          查看模块使用手册
/usr/bin/ansible-galaxy       优秀roles模块的官方平台
/usr/bin/ansible-playbook     定制自动化任务,编排剧本工具
/usr/bin/ansible-pull         远程执行命令的工具
/usr/bin/ansible-vault        文件加密工具(playbook中可能有明文,为了安全,加密)
/usr/bin/ansible-console      基于console界面与用户交互的执行工具

帮助

ansible-doc ping       # 显示ping模块详细帮助
ansibel-doc -s ping    # 显示ping模块简要帮助
ansible-doc -l         # 列出所有模块和简要说明

主机模式

all 或 *             # 表示inventory中的所有主机。
,                    # 单个主机用逗号间隔    例如   host1,host2
:                    # 取并集     例如  host1:host2:group1  表⽰2台主机加⼀个主机组。
:&                   # 取交集     例如  group1:&group2  表⽰两个主机组中都有的主机。
:!                   # 排除       例如  group1:\!host1  表⽰group1中排除host1主机的剩余主机。

通配符               # 例如   ansible web* -m ping
数字范围             # 例如   ansible web[0-5] -m ping
字母范围             # 例如   ansible web[a-d] -m ping
正则表达式           # 以"~"开头   例如 ansible ~web -m ping

ansible命令执行过程

1.加载自己的配置文件 默认/etc/ansible/ansible.cfg
2.加载对应的模块文件
3.通过ansible生成对应的临时python文件,并将该文件传输给程服务器的对应用户执行  ~/.ansible/tmp/ansible-tmp-随机数字/xxx.py
4.给文件执行权限
5.执行并返回结果
6.删除临时文件,sleep 0 退出

调用sleep 0可以释放cpu时间,让线程马上重新回到就绪队列而非等待队列,sleep 0释放当前线程所剩余的时间片(如果有剩余的话),这样可以让操作系统切换其他线程来执行,提升效率。

交互式命令

直接执行 ansible-console
命令行提示
root@all (3)[f:5]

说明
root        执行命令的用户
all         执行命令的主机,对应inventory中的主机
(3)         共有3个主机受控
[f:5]       并发线程数5

命令
list        列出当前主机列表
cd host     切换当前受控的主机列表为host主机组,也可切换单个主机,指定ip即可
forsk 10    修改并发数为10
?          列出所有内置命令
command hostname     用command模块执行hostname命令
posted @ 2024-05-10 09:49  立勋  阅读(33)  评论(0)    收藏  举报