Linux Ansible(自动化运维)的使用

Ansible linux服务 批量运维使用

Ansible临时命令的语法
	
	1:保存ip地址的hosts文件
	[webservers]
		192.168.144.155  ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='Liang7890569'
		192.168.144.100  ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='Liang7890569'
		192.168.144.199  ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='Liang7890569'
	2:ansible命令语法
		ansible <保存ip地址的hosts文件中的webservers> -m <选择使用哪个模块> -a <执行需要的命令> -i <hosts文件>
    3:playbook.yml文件的使用
    	ansible playbook.yml -i hosts
    	ansible A.yml -i ./hosts

Ansible模块的使用

Copy模块

---
- hosts: webservers
  remote_user: root
  tasks:
    - name: Copy is use
      copy: src=/root/Shell dest=/root/Shell/Hello
		copy src=			要copy的文件 dest=copy到什么位置
		hosts:				指定hosts文件中的webservers
		remote_user:		指定为root用户
		name:				task的名字

Shell 和 Command 模块

---
- hosts: webservers
  remote_user: root
  tasks:
    - name: Copy is use
      shell: systemctl start httpd.service
	  command:systemctl start nginx.service
     shell和command模块差不多,不过shell模块都可以做到,就没必要使用command模块了
     shell:		要执行的shell命令 例如echo,创建文件,目录等等;

Yum模块

- hosts: webservers
  remote_user: root
  tasks:
    - name: wget useing
      yum:
         name:
         - nginx
         state: present
	name
	- nginx :		安装nginx服务
	state:present :	安装最新的nginx服务

Service模块

---
- hosts: webservers
  remote_user: root
  tasks:
    - name: Service status check
      service: name=nfs-server.service  state=restarted
	service:这里是开启 nfs服务 服务

Roles的使用

	相当于编程语言中的class,对于一些重复的动作,采用调用的方式,写一遍可以多处调用
---
 - hosts: webservers
   roles: 
   - websrvs
   roles的调用 websrvs.yml文件

roles目录结构

Roles

    files 		保存文件
    handlers    文件修改时,重启服务文件
    tasks		保存任务文件
    templates   保存模板,nginx httpd nfs sab dns 等等

    遇到复杂的运维工作时,可以将所有的服务分多个文件写入到task中,模板文件写入templates中
    然后文件发生改动时,服务需要重启,在handlers中设置好重启服务的yml文件就可以了
    最后在和role同级目录中建立yml文件 以及hosts文件(一定要是同级目录下,否则可能找不到yml文件)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45005209/article/details/107282172