ansible教程
一.yum安装ansible
1.ansible简介
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。无客户端。
2.域名解析
vim /etc/hosts
192.168.0.151 ansible
192.168.0.152 host1
192.168.0.153 host2
3.ansible服务端安装
yum install -y epel-release
yum install -y ansible
4.检测是否安装完成
rpm -qc ansible
ansible --help
5.配置免密登录
ssh-keygen
ll .ssh/
ssh-copy-id 192.168.0.152
二.源码编译安装ansible
三.ansible基础
1.定义主机清单
vim /etc/ansible/hosts
[webservers]
host1
host2
2.测试连通性
ansible localhost -m ping
3.简洁输出
ansible localhost -m ping -o
ansible host2 -m ping -u root -k -o
4.去掉(yes/no)的询问
vim /etc/ssh/ssh_config
StrictHostKeyCheking no
systemctl restart sshd
四.Inventory-主机清单
1.增加主机组
vim /etc/ansible/hosts
[webserver]
host1
host2
ansible webserver -m ping -o
2.增加用户名 密码
vim /etc/ansible/hosts
[webserver]
host[1:2] ansible_ssh_user='root' ansible_ssh_pass='666666'
ansible webserver -m ping -o
3.增加端口
vim /etc/ansible/hosts
[webserver]
host[1:2] ansible_ssh_user='root' ansible_ssh_pass='666666' ansible_ssh_port='2222'
4.组:变量
vim /etc/ansible/hosts
[webserver]
host[1:2]
[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='123456'
5.子分组
vim /etc/ansible/hosts
[apache]
host[1:2]
[nginx]
host[3:4]
[webserver:children]
apache
nginx
[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='123456'
五.Ad-Hoc-点对点模式
1.shell模块
ansible webserver -m shell -a'hostname' -o
ansible webservers -m shell -a'yum -y install httpd' -o
2.复制模块
ansible webservers -m copy -a 'src=/tmp/1.txt dest=/tmp owner=root group=root mode=755'
ansible webservers -m copy -a 'src=/tmp/1.txt dest=/tmp owner=root group=root mode=755 backup=yes'
3.用户模块
ansible webservers -m user -a 'name=he_ber state=present'
echo '123456'|opensd passwd -1 -stdin
ansible webservers -m user -a'name=he_ber password="$1$4GUXJ/dE$Ruum9mCqQfnarcq1cp0G81"'
ansible webservers -m user -a'name=he_ber state=absent'
4.软件包管理
ansible webservers -m yum -a'name="*" state=latest'
ansible webservers -m yum -a'name="httpd" state=latest'
ansible webservers -m yum -a'name="httpd" state=removed'
5.服务模块
ansible webservers -m service -a'name=httpd state=started enabled=yes'
ansible webservers -m service -a'name=httpd state=stopped'
ansible webservers -m service -a'name=httpd state=restarted'
6.文件模块
ansible webservers -m file -a'path=/tmp/2.txt mode=777 state=touch'
ansible webservers -m file -a'path=/tmp/he_ber mode=777 state=directory'
7.收集模块
ansible webservers -m setup
ansible webservers -m setup -a'filter=ansible_all_ipv4_addresses'
六.YAML
1.语法
list;
-list1
-list2
Dicts:
k:v
k:v
2.例
vim apache.yaml
- hosts: webservers
tasks:
- name: install apache packages
yum: name=httpd state=present
- name: copy apache conf
copy: src=/apache/httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: ensure apache is running
service: name=httpd state=started enabled=yes
ansible-playbook apache.yaml --syntax-check
ansible-playbook apache.yaml --list-tasks
ansible-playbook apache.yaml --list-hosts
ansible-playbook apache.yaml
七.Role
1.目录结构
mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p
touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
echo 1234 >roles/nginx/files/index.html
yum install -y nginx && cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2
2.任务编写
vim roles/nginx/tasks/main.yaml
---
- name: install epel-release pasckge
yum: name=epel-release state=latest
- name: install nginx packge
yum: name=nginx state=latest
- name: copy index.html
copy: src=index.html dest=/usr/share/nginx/html/index.html
- name: copy nginx.conf template
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx
- name: make sure nginx service running
service: name=nginx state=started enabled=yes
3.准备配置文件
vim roles/nginx/templates/nginx.conf.j2
worker_processes {{ ansible_processor_cores }};
worker_connections {{ worker_connections }};
4.编写变量
vim roles/nginx/vars/main.yaml
worker_connections: 10240
5.编写处理程序
vim roles/nginx/handlers/main.yaml
---
- name: restart nginx
service: name=nginx state=restarted
6.编写剧本
vim roles/site.yaml
- hosts: webservers
roles:
- nginx
7.实施
ansible-playbook site.yaml --syntax-check
ansible-playbook site.yaml