ansible教程,无客户端。2.域名解


ansible教程

一.yum安装ansible

1.ansible简介
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。无客户端。
2.域名解析
#ansible服务端配置,客户端无需配置
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.测试连通性
#-m 指定模块。什么功能
#ping只是其中一个模块,还有shell,yum等等
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.组:变量
#ansible内部变量可以帮助我们简化主机清单的设置
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模块
#使用shell模块查看主机信息
ansible webserver -m shell -a'hostname' -o
#使用shell模块安装httpd
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'
#安装Apache
ansible webservers -m yum -a'name="httpd" state=latest'
#卸载Apache
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.例
#通过YAML编写一个简单的副本,完成web的部署、配置、启动的全过程
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

相关内容