德庆李宁 发表于 2023-8-22 07:10:25

ansible Ad-Hoc YAML剧本

ansible、Ad-Hoc、YAML剧本

1.简介

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

1.dns resolve
ansible服务器:192.168.70.42配置阿里yum源
ansible客户机:192.168.70.35192.168.70.36   配置阿里yum源
服务器和客户机都做好域名解析
vim /etc/hosts
192.168.70.35 host1
192.168.70.36 host2
192.168.70.42 ansible2.install ansible(ansible服务器)
yum install ansible -y3.ssh-key免密连接

ssh-keygen   //一路回车 有yes填yes

ssh-copy-id root@192.168.70.35        //发送公钥
ssh-copy-id root@192.168.70.36

ssh root@192.168.70.35   //测试连接
ssh root@192.168.70.364.ansible基础

1.自定义主机清单 (ansible机器)
vim /etc/ansible/hosts
host1
host22.测试连通性
ansible localhost -m ping//测试host1连通性 -m指定模块

ansible host1 -m ping o//一行简简洁输出3.know_hosts
ansible host2 -m ping -u root -k -o //没有配置ssh免密可以交互式密码验证登录 如果是第一次连接会报错 需使用ansible host2 -m ping 交互yes之后才能

`去掉(yes/no)的询问`
vim /etc/ssh/ssh_config
StrictHostKeyChecking no
systemctl restart sshd

ansible host2 -m ping -u root -k -o //再次测试4.请注意ping和ssh
ping的通不代表ansible连接的上,ping ICMP:网际消息管理协议
ansible的ping事探测ssh程序是否连接。不是icmp协议,如果关比sshd服务,ping可以成功,ansible测试会依旧失败。
5.Inventory主机清单

1.增加主机组
vim /etc/ansible/hosts

host1 ansible_ssh_user='root'
host2 ansible_ssh_user='root'

ansible webserver -m ping -o2.增加用户名没密码(没设置免密登录,可以增加用户名密码)
vim /etc/ansible/hosts
host1 ansible_ssh_user='root' ansible_ssh_pass='123456'
host2 ansible_ssh_user='root' ansible_ssh_pass='123456'

//或者
host ansible_ssh_user='root' ansible_ssh_pass='123456'3.增加端口(host)
vim /etc/ssh/sshd_config
Port 22 改成Port 2222
systemctl restart sshd

测试连接
ansible host1 -m ping//会发现失败 ansible默认sshd端口还是22

回到ansible 修改用户host的端口
vim /etc/ansible/hosts
host1 ansible_ssh_user='root' ansible_ssh_pass='123456' ansible_ssh_port='2222'//指定ssh端口

再次测试
ansible host1 -m ping4.组:变量

host1 ansible_ssh_port='2222'
host2 ansible_ssh_port='2222'


ansible_ssh_user='root'
ansible_ssh_pass='123456'5.子分组 (将不同的分组进行组合) 为了实验方便我们先把host的sshd 端口改回默认
vim /etc/ansible/hosts

host1

host2

apache
nginx

ansible webserver -m ping -o//测试6.自定义主机列表
vim hostlist

host1
host2

ansible -i hostlist dockers -m ping -o//测试6.Ad-Hoc点对点模式

临时的,在ansible中是指需要快速执行的单条命令,并且不需要保存的命令。对于复杂的命令则为 playbook。
1.shell模块
ansible webserver -m shell -a 'hostname' -o//查看主机名称

ansible webserver -m shell -a 'yum install mariadb -y' -o//安装mariadb

ansible webserver -m shell -a 'touch /tmp/777.txt' -o//创建文件2.复制模块
ansible-doc copy//查看帮助文档

ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt'//copy 如果和原先文件一样 不会进行拷贝

ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/3.txt owner=root group=root mode=777'

ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/3.txt owner=root group=root mode=777 backup=yes'3.用户模块
ansible-doc user//查看帮助文档

ansible webserver -m user -a 'name=xux state=present'//创建用户

echo '123456' | openssl passwd -1 -stdin//系统存储的是密文 需要将输入的密码转化为加密后的密文
ansible webserver -m user -a 'name=xux password="$1$g93P3CoY$YPuV8anNPa8HBhnfMncB60"'//修改密码

ansible webserver -m user -a 'name=xux shell=/sbin/nologin append=yes'//修改用户属性

ansible webserver -m user -a 'name=xux state=absent'//删除用户4.软件包管理
ansible-doc yum//查看帮助文档

ansible webserver -m yum -a 'name="*" stale=lastest'//升级所有包

ansible webserver -m yum -a 'name=vsftpd state=present'//安装vsftpd

ansible webserver -m yum -a 'name=vsftpd state=absent'//卸载vsftpd

ansible webserver -m yum -a 'name=vsftpd state=latest'//升级vsftpd5.服务模块
ansible-doc service//查看帮助文档

ansible webserver -m service -a 'name=vsftpd state=started'//开启httpd服务

ansible webserver -m service -a 'name=vsftpd state=stopped'//关闭httpd服务

ansible webserver -m service -a 'name=vsftpd state=restarted'//重启httpd服务

ansible webserver -m service -a 'name=vsftpd state=started enabled=yes'//开启开机自启

ansible webserver -m service -a 'name=vsftpd state=started enabled=no'//关闭开机自启6.文件模块
ansible-doc file//查看帮助文档

ansible webservice -m file -a 'path=/tmp/11.txt mode=771 state=touch'//创建文件

ansible webserver -m file -a 'path=/tmp/asb mode=770 state=directory'//创建目录7.收集模块
ansible-doc setup//查看帮助文档

ansible host1 -m setup//列出常见常熟

ansible host1 -m setup -a 'filter=ansible_all_ipv4_addresses'//查询ip地址7.YAML

YAML Ain’t Markup Language-非标记语言
示例:通过YAML编写一个简单的剧本,完成web的部署,配置,启动的全过程。
1.准备
ansible all -m yum -a 'name=httpd state=removed' -o//清理一下环境
yum install -y httpd//准备配置文件
mkdir apache
cd apache
cp -rf /etc/httpd/conf/httpd.conf .
grep '^Listen' httpd.conf
Listen 8080//修改端口2.编写剧本
编写
vim apache.yaml
- hosts: host2
tasks:
- name: install apache packages
    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
   
测试
ansible-playbook apache.yaml --syntax-check
ansible-playbook apache.yaml --list-tasks
ansible-playbook apache.yaml --list-hosts

执行
ansible-playbook apache.yaml3.优化
如果我们在配置文件中修改了apache端口号为9000,那么我们再次重启会发现文件会被拷贝过去,但是服务器并没没有重启。
因为剧本中写的服务启动,并没有重启服务。需要优化一下剧本代码,如果执行copy语句那么notify会触发,接着会执行handlers中的语句重启服务。
- hosts: webserver
tasks:
- name: install apache packges for xux
    yum: name=httpd state=latest
- name: copy apache conf file for xux
    copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify: restart apache service for xux
- name: ensure apache si running
    service: name=httpd state=started enabled=yes
handlers:
- name: restart apache service for xux
    service: name=httpd state=restarted8.Role-角色扮演
roles则是在ansible中,playbooks的目录组织结构。将代码或文件进行模块化,成为roles的文件目录组织结构,易读,代码可重用,层次清晰。
示例:通过role远程部署nginx并配置
1.准备目录
nginx 角色名
files普通文件
handlers触发器程序
tasks主任务
templates 金甲模板(有变量的文件)
vars 自定义变量# 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

# tree roles/
roles/
├── nginx
│   ├── files
│   │   └── index.html
│   ├── handlers
│   │   └── main.yaml
│   ├── tasks
│   │   └── main.yaml
│   ├── templates
│   └── vars
│     └── main.yaml
└── site.yaml2.编写任务
vim roles/nginx/tasks/main.yaml
- name: install epel-release packge
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=yesTemplate:
Template模板在运用时与copy模块类似,区别在于可以在Ansible的Playbook执行的时候,根据一定的条件灵活的设置要复制文件中的部分关键内容。
在Ansible中,Templat模板使用jinjia2语言进行配置,模板文件的后缀名为.j2。3.准备配置文件
vim roles/nginx/templates/nginx.conf.j2
worker_processes auto; 改成 worker_processes {{ ansible_processor_cores }};//ansible内置变量

worker_connections 1024; 改成 worker_connections {{ worker_connections }};//自定义变量4.编写变量
vim roles/nginx/vars/main.yaml
worker_connections: 102405.编写处理程序
vim roles/nginx/handlers/main.yaml
---
- name: restart nginx
service: name=nginx state=restarted6.编写剧本
vim roles/site.yaml
- hosts: webserver
roles:
- nginx7.实施
cd roles
ansible-playbook site.yaml --syntax-check//测试
ansible-playbook site.yaml//实施剧本文章内容主要参考:b站磊哥5.Ansible 自动化运维实战

来源:https://www.cnblogs.com/xuxuxuxuxu/p/17647465.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: ansible Ad-Hoc YAML剧本