ansible自动化运维

简介

ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
无客户端。

install-部署

:
1.dns resolve:配置host(主)
2.install ansible:
yum install -y epel-release
yum install -y ansible

列出所有文件:rpm -ql ansible
查看配置文件:rpm -qc ansible
看yum模块,了解其功能:ansible-doc -s yum

ssh-key(可选)

ansible基础:


1.定义主机清单:
vim /etc/ansible/hosts
添加主机域名

2.测试连通性:
ansible host1 -m ping -u root -k -o
-m:指定模块 -u:用户名 -k:密码 -o:简洁输出

3.know_hosts:
去掉(yes/no)的询问:
vim /etc/ssh/ssh_config
StrictHostKeyChecking no

Ad-Hoc-点对点模式


1.shell模块:ansible-doc shell
获取主机名:
ansible webserver -m shell -a 'hostname' -o
-f 2 指定线程数:
ansible webserver -m shell -a 'hostname' -o -f 2
部署apache:
ansible host2 -m shell -a 'yum -y install httpd' -o
查询系统负载:
ansible host3 -m shell -a 'uptime' -o
2.复制模块:ansible-doc copy
ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777 backup=yes'
3.用户模块:ansible-doc user
创建:
ansible webserver -m user -a 'name=qianfeng state=present'
删除:
ansible webserver -m user -a 'name=qianfeng state=absent'
追加:
ansible webserver -m user -a 'name=qianfeng shell=/sbin/nologin append=yes'
生成加密密码值:echo '777777' | openssl passwd -1 -stdin:
ansible webserver -m user -a 'name=qianfeng password="$1$XVzsJMDr$5wI4oUaQ.emxap6s.N272."'
4.软件包管理:ansible-doc yum
升级所有包:
ansible host1 -m yum -a 'name="*" state=latest'
安装apache:
ansible host2 -m yum -a 'name="httpd" state=latest'
5.服务模块:ansible-doc service
启动:
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'
6.文件模块:ansible-doc file
创建文件:
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'
7.收集模块:
查询所有信息:
ansible host3 -m setup
ansible host3 -m setup -a 'filter=ansible_all_ipv4_addresses'

主机清单:


1 增加主机组:
vim /etc/ansible/hosts
[webserver]
host1
host2
host3
host4
ansible webserver -m ping -u root -k -o

2 增加用户名 密码:
[webserver]
host[1:4] ansible_ssh_user='root' ansible_ssh_pass='666666'

3 增加端口:
请将host1的sshd程序端口修改为2222
vim /etc/ssh/sshd_config Port 2222
vim /etc/ansible/hosts
[webserver]
host1 ansible_ssh_user='root' ansible_ssh_pass='777777' ansible_ssh_port='2222'
host[2:4] ansible_ssh_user='root' ansible_ssh_pass='666666'

4 变量:
[webserver]
host[1:4]
[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='666666'

ansible自动化运维_第1张图片
5 子分组:
[apache]
host[1:2]
[nginx]
host[3:4]
[webserver:children]
apache
nginx
[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='666666'

6 自定义主机列表:
vim hostlist
[dockers]
host1
host2
[dockers:vars]
ansible_ssh_user='root'
ansible_ssh_pass='666666'
ansible -i hostlist dockers -m ping -o

YAML-YAML Ain’t Markup Language-非标记语言:


语法:
列表: 字典:
fruits: martin:
- Apple name: sdsafsaf
- Orange job: sfdsfsdfds
- Strawberry
- Mango
ansible:
1.创建一个文件夹:apache
vim apache.yaml:
- hosts: host2 连接主机清单名
tasks: 任务
- name: install apache packges
yum: name=httpd state=present
- name: copy apache conf
copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: ensure apache is running
service: name=httpd state=started enabled=yes

2.测试:
检验语法:
ansible-playbook apache.yaml --syntax-check
列出任务:
ansible-playbook apache.yaml --list-tasks
列出主机:
ansible-playbook apache.yaml --list-hosts
执行:
ansible-playbook apache.yaml
handlers:
ansible自动化运维_第2张图片

Role-角色扮演:


1.目录结构:
配置文件目录:templates
vim roles/nginx/templates/nginx.conf.j2
调用内部已知变量:worker_processes {{ ansible_processor_cores }};
自定义变量:worker_connections {{ worker_connections }};
编写剧本:site.yaml
编写任务目录:tasks
编写处理程序目录:handlers
编写变量目录:vars

你可能感兴趣的:(ansible)