ansible 基础安装 环境配置
环境要求
系统版本是 centos7.6
使用 python3.6版本
需要对系统进行初始化的基本安装包 使用"sysinit.sh"这个脚本初始化系统
sysinit.sh 脚本内容
#!/bin/bash
#######################
# sysinit_centos7_x. sh
# Version 0.1
#######################
LOCAL_IP=`curl whatismyip.akamai.com`
echo "# $LOCAL_IP localhost" >> /etc/hosts
KERNEL_VERION_1=`uname -a | awk '{print $3}' | awk -F"-" '{print $1}'`
if [ ${KERNEL_VERION_1} = '3.10.0' ]
then
OS_VERSION="RHEL7"
else
echo "This script is not suitable for the running os version."
exit 104
fi
if [ X$(id -u) != "X0" ]
then
echo 'This script MUST be run as root!'
exit 105
fi
echo "#############Install wget tools####################"
yum -y install wget
echo "#############update epel yum####################"
yum repolist | grep epel
if [ X$? != "X0" ]
then
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all && yum repolist
fi
echo "#############disabled selinux####################"
repaddline "^SELINUX=" "SELINUX=disabled" /etc/selinux/config
setenforce 0
echo -e "\n########## Install lrzsz ##########"
yum install lrzsz -y
echo -e "\n########## Config NTP shanghai ##########"
yum install -y ntp
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ntpdate time.asia.apple.com
systemct start ntpd && systemctl enable ntpd
ntpq -p
echo '* */30 * * * /usr/sbin/ntpdate time.asia.apple.com 2>&1 ' >> /var/spool/cron/root
echo -e "\n########## install Performance tool ##########"
yum install yum-utils bind-utils lsof iftop telnet net-tools sysstat iotop inotify-tools gcc-c++ openssl-devel -y
echo -e "\n##########25########"
yum -y remove mariadb-libs
echo -e "\n##########hosts########"
chattr +i /etc/hosts
编译安装 python3.6版本
Ansible 安装步骤
wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz
tar -xvf Python-3.6.0.tgz
cd Python-3.6.0
./configure --prefix=/usr/local/python3 --with-ssl --enable-optimezations
make
make install
/usr/local/python3/bin/pip3 install ansible #安装 anible
ln -sf /usr/local/python3/bin/* /usr/loca/bin
mkdir /etc/ansible
touch /etc/ansible/{ansible.cfg,hosts}
Ansible.cfg 配置模块
[defaults] inventory = /etc/ansible/hosts forks = 5 default_sudo_user = root remote_port = 22 host_key_checking = False timeout = 20 log_path = /var/log/ansible.log private_key_file = ~/.ssh/id_rsa
Asnbiel.cfg配置文件详解
1)inventory 该参数表示资源清单inventory文件的位置,资源清单就是一些Ansible需要连接管理的主机列表 inventory = /root/ansible/hosts 2)library Ansible的操作动作,无论是本地或远程,都使用一小段代码来执行,这小段代码称为模块,这个library参数就是指向存放Ansible模块的目录 library = /usr/share/ansible 3)forks 设置默认情况下Ansible最多能有多少个进程同时工作,默认设置最多5个进程并行处理。具体需要设置多少个,可以根据控制主机的性能和被管理节点的数量来确定。 forks = 5 4)sudo_user 这是设置默认执行命令的用户,也可以在playbook中重新设置这个参数 sudo_user = root //注意:新版本已经作了修改,如ansible2.4.1下已经为: default_sudo_user = root 5)remote_port 这是指定连接被关节点的管理端口,默认是22,除非设置了特殊的SSH端口,不然这个参数一般是不需要修改的 remote_port = 22 6)host_key_checking 这是设置是否检查SSH主机的密钥。可以设置为True或False host_key_checking = False 7)timeout 这是设置SSH连接的超时间隔,单位是秒。 timeout = 20 8)log_path Ansible系统默认是不记录日志的,如果想把Ansible系统的输出记录到人i治稳健中,需要设置log_path来指定一个存储Ansible日志的文件 log_path = /var/log/ansible.log 另外需要注意,执行Ansible的用户需要有写入日志的权限,模块将会调用被管节点的syslog来记录,口令是不会出现的日志中的 9)private_key_file 在使用ssh公钥私钥登录系统时候,使用的密钥路径。 private_key_file=/path/to/file.pem
hosts文件配置详解
[test] 组配置 192.168.192.1:22 ansible_ssh_user=root ansible_ssh_pass='123456' 密码 192.168.192.1:22 ansible_ssh_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa #秘钥配置 test1 ansible_ssh_host=192.168.192.1 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa #别名配置 192.168.192.1:22 ansible_ssh_user=root ansible_ssh_pass='123456' 密码 [test_group1] 192.168.192.1:22 ansible_ssh_user=root ansible_ssh_pass='123456' 密码 [test_group2] #引用其他组配置 test test_group1 192.168.192.1:22 ansible_ssh_user=root ansible_ssh_pass='123456' [test_grouop3:vars] touch_file=jeson3 资产定义更新
ansible 基本使用方法案例
ansible test_group1 -m shell -a "echo $HOSTNAME" -f 5 ansible test_group1 -m copy -a "esrc=/tcp/host dest=/tmp" -f 5 -l 192.168.192.1 #复制文件 ansible test_group1 -m setup #查看系统版本 ansible test_group1 -m setup -a "filter=ansible_distribution*" #只取出系统版版本信息 ansible test_group1 -m yum -a "name=nginx state=presnt" -f 5 -l 192.168.192.1 #没有安装 就安装 安装了的 就升级 ansible test_group1 -m service -a "name=nginx state=started" -f 5 -l 192.168.192.1 #启动nginx服务 ansible test_group1 -m git -a "repo=https://github.com/iopsgroup/imoocc dest=/opt/imoocc version=HEAD" -f 5 -l 192.168.192.1 #代码拉去

浙公网安备 33010602011771号