简介
简单的介绍
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
无客户端。
工作原理
基础及主机清单
1.服务器进行批量操作的时候,对客户机进行免密 ssh-key 推送公钥。
2.定义主机清单可以批量进行,操作更加简便
3.主机清单列表还可自定义使用 -i 参数进行调用
[root@localhost ~]# vim /etc/ansible/hosts #配置主机清单文件
[apache] #主机组
host[1:2]
[nginx] #主机组
host[3:4]
[webserver:children] #子分组
apache
nginx
[webserver:vars] #添加ansible内部变量
ansible_ssh_user='root' #以root身份登陆
ansible_ssh_pass='1' #密码为1
[root@localhost ~]# ansible -i hostlist dockers -m ping -o
##-i 自定义主机清单文件名 dockers为主机组 -m 调用模块 -o简洁输出
ansible模块
需要快速执行并且不需要进行保存的命令
- shell模块
ansible-doc shell 帮助
ansible host2 -m shell -a 'yum -y install httpd' -o
- copy 复制模块
ansible-doc copy 帮助
ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777'
- user 用户模块
ansible-doc user 帮助
ansible webserver -m user -a 'name=qianfeng state=present'
- yum 软件包管理模块
ansible-doc yum 帮助
ansible host2 -m yum -a 'name="httpd" state=latest'
- service 服务模块
ansible-doc service 帮助
ansible host2 -m service -a 'name=httpd state=started enabled=yes'
- file 文件模块
ansible-doc file 帮助
ansible host1 -m file -a 'path=/tmp/88.txt mode=777 state=touch'
- setup 收集模块
ansible-doc setup 帮助
ansible host1 -m setup -a 'filter=ansible_all_ipv4_addresses'