ansible安装 使用 介绍

1.介绍安装

a.介绍
Ansible:—基于 Python paramiko 开发,分布式,无需客户端,轻量级,配置语法使用 YMAL 及 Jinja2模板语言,更强的远程命令执行操作。
b.安装
#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
#yum install ansible -y

2.配置

a.ansible程序

/usr/bin/ansible                主程序,临时命令执行工具
/usr/bin/ansible-doc            查看配置文档,模块功能查看工具
/usr/bin/ansible-galaxy         下载/上传优秀代码或Roles模块的官网平台
/usr/bin/ansible-playbook       playbook运行工具
/usr/bin/ansible-vault          文件加密工具
/usr/bin/ansible-console        基于Console界面与用户交互的执行工具    

b.配置文件ansible.cfg

Ansible的配置文件,配置文件可以随意放,但有查找顺序

$ANSIBLE_CONFIG#环境变量 ansible.cfg #当前目录下面查找 .ansible.cfg #当前用户的家目录下查找
/etc/ansible/ansible.cfg ansible all -m ping -i ./host -i:指定host文件

c.配置文件参数

#inventory      = /etc/ansible/hosts      #主机列表配置文件
#library        = /usr/share/my_modules/  #库文件存放目录
#remote_tmp     = ~/.ansible/tmp          #临时py文件存放在远程主机目录
#local_tmp      = ~/.ansible/tmp          #本机的临时执行目录
#forks          = 5                       #默认并发数
#sudo_user      = root                    #默认sudo用户
#ask_sudo_pass  = True                    #每次执行是否询问sudo的ssh密码
#ask_pass       = True                    #每次执行是否询问ssh密码
#remote_port    = 22                      #远程主机端口
host_key_checking = False                 #跳过检查主机指纹
log_path = /var/log/ansible.log           #ansible日志
[privilege_escalation]                    #如果是普通用户则需要配置提权
#become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False

更多参数访问官网
https://ansible-tran.readthedocs.io/en/latest/docs/intro_configuration.html#environmental-configuration

d.host文件连接主机样例

d.1 基于密码连接
    [webservers]
    web02 ansible_ssh_host=172.16.1.8 ansible_ssh_pass='123456'
d.2 基于密钥连接
    创建秘钥
    ssh-keygen
    发送公钥
    ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
d.3 主机组的配置信息
    [web]
    10.0.0.1
    [mysql]
    10.0.0.2
    server组成两个子组
    [server:children]
    web
    mysql

 一般都使用密钥链接 编写分发脚本

[root@ansible ~]# cat fenfa.sh 
#!/bin/bash

sed -i '/StrictHostKeyChecking/s/^#//; /StrictHostKeyChecking/s/ask/no/' /etc/ssh/ssh_config
sed -i "/#UseDNS/ s/^#//; /UseDNS/ s/yes/no/" /etc/ssh/sshd_config

cat hostsname.txt | while read ip pwd; do
  sshpass -p $pwd ssh-copy-id -i ~/.ssh/id_rsa.pub root@$ip &>/dev/null
  ssh -nq $ip "cat ~/.ssh/authorized_keys"
  echo "$ip $host 成功"
done

[root@ansible ~]# cat hostsname.txt 
10.0.0.20 123456
10.0.0.10 123456

猜你喜欢

转载自www.cnblogs.com/zuozhengjun/p/10650584.html