ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,
实现了批量系统配置、批量程序部署、批量运行命令等功能。
无客户端。
yum install -y epel-release
安装epel源,如果您在非学校环境,请使用下方阿里YUM
rm ?-rf /etc/yum.repos.d/*
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo?
yum install -y ansible
检测部署是否完成
rpm -qc ansible查看配置文件
ansible --help查看ansible帮助
ansible-doc -l看所有模块(A10,华为,docker,EC2,aws等等广大厂商设备)
ansible-doc -s yum看yum模块,了解其功能
vim /etc/ansible/hosts
host1
host2
host3
ansible ? localhost ? -m ping
ansible host1 -m ping ?-o?
ansible host2 -m ping -u root -k -o?
-u增加用户名选项
-k增加密码选项
去掉(yes/no)的询问
vim /etc/ssh/ssh_config
StrictHostKeyChecking no
systemctl restart sshd
ping
ICMP:网际消息管理协议
ansible的ping,是探测ssh程序是否连接。不是icmp协议
官方链接
http://docs.ansible.com/ansible/intro_inventory.html#
vim /etc/ansible/hosts
ansible webserver ?-m ping ?-o
vim /etc/ansible/hosts
ansible webservers ?-m ping -o
vim /etc/ansible/hosts
ansible内部变量可以帮助我们简化主机清单的设置
vim /etc/ansible/hosts
常用变量
vim /etc/ansible/hosts
vim hostlist
ansible -i ?hostlist dockers ?-m ping ?-o
临时的,在ansible中是指需要快速执行的单条命令,并且不需要保存的命令。对于复杂的命令则为 playbook。
ansible webserver -m shell -a 'hostname' -o 获取主机名
ansible webserver -m shell -a 'hostname' -o -f 2? ? ? f 2 ? 指定线程数
ansible host2 -m shell -a 'yum -y install httpd' -o
ansible host3 -m shell -a 'uptime' -o
ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777'
ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777 backup=yes'
创建用户
ansible webserver -m user -a 'name=qianfeng state=present'
修改密码
1.生成加密密码
echo '512050951' | openssl passwd -1 -stdin
2.修改密码
ansible webserver -m user -a 'name=qianfeng password="$1$XVzsJMDr$5wI4oUaQ.emxap6s.N272."'
修改shell
ansible webserver -m user -a 'name=qianfeng shell=/sbin/nologin append=yes'
删除用户
ansible webserver -m user -a 'name=qianfeng state=absent'
ansible host1 -m yum -a 'name="*" state=latest'
ansible host2 -m yum -a 'name="httpd" state=latest'
ansible host2 -m service -a 'name=httpd state=started'
ansible host2 -m service -a 'name=httpd state=started enabled=yes'
ansible host2 -m service -a 'name=httpd state=stopped'
ansible host2 -m service -a 'name=httpd state=restarted'
ansible host2 -m service -a 'name=httpd state=started enabled=no'
ansible host1 -m file -a 'path=/tmp/88.txt mode=777 state=touch'
ansible host1 -m file -a 'path=/tmp/99 mode=777 state=directory'
ansible host3 -m setup
ansible host3 -m setup -a 'filter=ansible_all_ipv4_addresses'