一、简介
基于Python开发的自动化运维工具,实现了批量系统配置,批量程序部署,批量运行命令等功能。无客户端
二、安装
yum install -y epel-release ansible的扩展源
yum install -y ansible
扩展:查看ansible支持的模块:ansible-doc -l
三、使用
1、ssh-key
1)免密方式
生成密钥:ssh-keygen
发送密钥:ssh-copy-id ip
2、主机清单
配置需要管理的主机清单文件:/etc/ansible/hosts
测试连接(实际依赖ssh进程,而非ping指令):ansible ip -m ping -o -u root -k
说明:-m为指定使用的模块,-o为简洁输出,ip可为目标ip或组名,-u指定登录用户,-k使用密码登录
1)设置组
[webserver] //组名
192.168.1.110
2)声明账户和密码
[webserver]
192.168.1.110 ansible_ssh_user='root' ansible_ssh_pass='Changeme_123'
3)定义连接端口
ssh服务端口被变更(/etc/ssh/sshd_config)为2222
[webserver]
192.168.1.110 ansible_ssh_user='root' ansible_ssh_pass='Changeme_123' ansible_ssh_port='2222'
4)组变量(以webserver组为例,应用给组内所有成员)
[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='Changeme_123'
5)子分组
[webservers:children]
webserver
...其他组名
6)自定义清单文件
ansible -i filepath groupname|ip -m ping -o
3、点对点模式
查看指令说明:ansible-doc 模块名
1)复制模块copy
ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/test.txt owner=root group=root mode=755 baskup=yes'
说明:-a表示补充参数,backup表示是否在对同名文件操作时进行备份
2)用户模块user
创建用户:ansible webserver -m user -a 'name=test'
修改密码:openssl passwd -1 -table '密码' //用来生成密码密文
ansible webserver -m user -a 'name=test password="密文"'
修改登录shell:ansible webserver -m user -a 'name=test shell=/sbin/nologin append=yes'
当前设置成了不能登录
删除用户:ansible webserver -m user -a 'name=test state=absent remove=yes'
说明:state默认值为present,表示存在或创建用户,absent表示删除用户
3)软件包管理yum
ansible webserver -m yum -a 'name="软件名|*" state=latest'
说明:软件安装时state值为present或installed或latest,软件更新使用latest,软件删除使用absent或removed
4)服务模块service
ansible webserver -m service -a 'name="服务名" state=started enabled=yes'
说明:enabled表示是否设置为开机启动,state的值为started或stopped或restarted或reloaded
5)文件模块file
ansible webserver -m file -a 'path=/temp/test.txt mode=755 state=touch'
说明:state的取值为删除操作absent, 创建目录directory, 修改文件file, 硬链接hard, 软链接link, 创建文件touch
6)收集模块setup
ansible webserver -m setup //收集所有信息
ansible webserver -m setup -a 'filter=属性名'
7)拉取文件fetch
ansible ip -m fetch -a 'src=文件源地址 dest=文件目标地址'
说明:拉去的文件会以ip分类存储
8)定时任务管理cron
创建定时任务:ansible webserver -m cron -a 'name="任务说明" minute="*/10" job="ls -alh > /dev/null"'
删除定时任务:ansible webserver -m cron -a 'name="任务说明" state=absent'
说明:时间属性包括minute,hour,day,month,weekday,还可以设置special_time属性的值在特殊时间点执行(annually, daily, hourly, monthly, reboot, weekly, yearly)
9)用户组模块group
用法用2)用户模块
10)脚本模块script
将本地脚本在指定机器上执行
ansible webserver -m script -a '/some/local/script.sh --some-argument 1234'
说明:chdir参数可以指定远端执行脚本的目录
11)解压缩模块unarchive
ansible webserver -m unarchive -a 'src=压缩包路径 dest=解压到指定的目录'
说明:默认压缩包在本地,加压给各远端机器,如果将remote_src参数值设置为yes,则表示压缩包在远端,直接解压即可
12)shell模块
可以直接在远端执行shell命令
ansible webserver -m shell -a 'cat /etc/passwd'
4、YAML
1)语法
列表
Animal:
- Cat
- Dog
- Tigger
注:字段对齐要求,前面有两个空格,-后有一个空格
字典
Cat:
name: mimi
age: 6
注:字段对其要求,前面有两个空格,:后有一个空格
2)示例
ansivle服务器上安装httpd服务:yum install -y httpd
创建临时存放目录:mkdir temp
进入临时目录:cd temp
拷贝http服务的配置文件:cp -rf /etc/httpd/conf/httpd.conf .
修改httpd.conf文件中的Listen端口80为8080
编写剧本temp.yaml,内容如下
- hosts: 指定的IP或组名
tasks:
- name: install httpd package
yum: name=httpd state=present
- name: copy httpd conf
copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: start httpd service
service: name=httpd state=started enabled=yes
检查语法:ansible-playbook temp.yaml --syntax-check
列出任务:ansible-playbook temp.yaml --list-tasks
列出主机:ansible-playbook temp.yaml --list-hosts
执行任务:ansible-playbook temp.yaml
扩展:handlers 用来定义处理程序,比如不能重复restart服务时
- hosts: 指定的IP或组名
tasks:
- name: install httpd package
yum: name=httpd state=present
- name: copy httpd conf
copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart httpd service
- name: start httpd service
service: name=httpd state=started enabled=yes
handlers:
- name: restart httpd service
service: name=httpd state=restarted
说明:只有当notify所配置的操作块执行时,才会触发执行handlers块
5、角色
roles是ansible中playbooks的目录组织结构,将代码或文件进行模块化,成为roles的文件目录组织结构,提高可读性和重用性
示例:使用role远程部署nginx并配置
1)目录结构示例
roles
nginx //角色名
files //普通文件
index.html
handlers //触发器任务
main.yaml
tasks //主任务
main.yaml
templates //金甲模板(变量文件)
nginx.conf
vars //自定义变量
main.yaml
site.yaml //定义执行的剧本
创建文件夹:mkdir -p roles/nginx/{files,handlers,tasks,templates,vars}
创建文件:touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
echo HelloWorld > roles/nginx/files/index.html
部署nginx:yum install -y nginx
拷贝配置文件:cp /usr/local/nginx/conf/nginx.conf roles/nginx/templates/nginx.conf
2)任务编写
a)tasks/main.yaml
---
- name: install epel-release package
yum: name=epel-release state=latest
- name: install nginx package
yum: name=nginx state=latest
- name: copy index.html
copy: src=index.html dest=/usr/local/nginx/html/index.html
- name: copy nginx.conf
template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf
notify: restat nginx
- name: start nginx server
service: name=nginx state=started enabled=yes
b)handlers/main.yaml
---
- name: restat nginx
service: name=nginx state=restarted
c)templates/nginx.conf
worker_processes {{ ansible_processor_cores }} //使用ansible内置变量
worker_connections {{ worker_connections }} //使用自定义变量
说明:template主要是用来结合变量推送配置,推送前会检查并替换变量
d)vars/main.yaml
worker_connections: 10240
e)site.yaml
- host:webserver
roles:
- nginx
3)任务测试
cd roles
检查语法:ansible-playbook site.yaml --syntax-check
执行任务:ansible-playbook site.yaml
ansible copy模块 附加文件权限 ansible filesystem
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
ansible模块--copy模块
将主控端的文件复制到远程主机,只针对文件src 源文件路径dest 目标文件路径content 将指定内容覆盖写入到目标主机文件中force=no 当主控端拷贝的文件名和目标名一
linux python java mysql centos -
ansible 常用模块 copy
模块说明 该copy模块将文件从本地或远程机器复制到远程机器上的某个位置
Ansible 文件系统 字符串 -
ansible copy模块考题 ansible -m copy
背景ad-hoc ,通过一次执行一行命令,可以实现简单的文件管理、软件包管理、服务管理等;但是如果想要多次执行一个任务,或者一次执行多个任务,那么 ad-hoc 就显得有点繁琐和力不从心了,这时候就轮到 Playbook 登场了。Playbook 是由 yml 语法书写,结构清晰,可读性强,可以简单将其理解为一门编程语言(本身具有变量、分支、循环、监听器的概念)。在一个 Playbook 中可以包
ansible copy模块考题 Playbook Ansible安装JDK Ansible部署Boot项目 ad-hoc -
ansible 安装 copy模块 ansible的copy
今天,我们开始学习运维自动化工具Ansible。一、Ansible原理1.1 什么是AnsibleAnsible一种集成IT系统的配置管理、应用部署、执行特定任务的开源平台/框架。基于Python语言实现,核心模块包括:jinja2、PyYAML和paramiko。Ansible允许重复执行而不出错,客户端无agent,服务端无deamon进程。Ansible特点:Ansible支持
ansible 安装 copy模块 ansible copy模块 ansible file模块 ansible 修改文件变量 ansible安装部署