ansible自动化运维

Ansible

1、ansible基础

1)定义主机清单

vim /etc/ansible/hosts

2)连通性测试

##(-m 指定模块)
ansible localhost -m ping 

3)简洁输出

ansible host1 -m ping  -o

4)know_hosts

增加用户名选项和密码选项

ansible host2 -m ping -u root -k -o 

去掉(yes/no)的询问

vim /etc/ssh/ssh_config
       StrictHostKeyChecking no  #修改为no
systemctl restart sshd
ansible host2 -m ping -u root -k -o 
#成功不提示

2、Inventory-主机清单

1)定义主机组

vim /etc/ansible/hosts

eg:

[webserver]
host1
host2
host3
host4

事先做好服务器的域名解析

ansible webserver -m ping -o

输出提示

[root@localhost ~]# ansible webserver -m ping -u root -k -o
SSH password: 
host3 | SUCCESS => {"changed": false, "ping": "pong"}
host1 | SUCCESS => {"changed": false, "ping": "pong"}
host4 | SUCCESS => {"changed": false, "ping": "pong"}
host2 | SUCCESS => {"changed": false, "ping": "pong"}

2)增加用户名和密码

vim /etc/ansible/hosts
[webserver]
host[1:4] ansible_ssh_user='用户名' ansible_ssh_pass='密码'
ansible webservers -m ping -o  #免用户名和密码成功 

3)增加端口

将host1的sshd程序端口修改为2222

vim /etc/ansible/hosts
[webserver]
host1 ansible_ssh_user='用户名' ansible_ssh_pass='密码' ansible_ssh_port='2222'
host[2:4] ansible_ssh_user='用户名' ansible_ssh_pass='密码'

4)组:变量

vim /etc/ansible/hosts
[webserver]
host[1:4]
[webserver:vars]
ansible_ssh_user='用户名'
ansible_ssh_pass='密码'

5)子分组

将不同的分组进行组合

vim /etc/ansible/hosts
[apache]
host[1:2]
[nginx]
host[3:4]
[webserver:children]
apache
nginx
[webserver:vars]
ansible_ssh_user='用户名'
ansible_ssh_pass='密码'

6)自定义主机列表

vim hostlist
[dockers]
host1
host2
[dockers:vars]
ansible_ssh_user='用户名'
ansible_ssh_pass='密码'
ansible -i hostlist dockers -m ping -o

3、Ad-Hoc-点对点模式

1)shell模块

ansible webserver -m shell -a 'hostname' -o #获取主机名
ansible host2 -m shell -a 'yum -y install httpd' -o #部署apache
ansible host3 -m shell -a 'uptime' -o #查询系统负载

2)复制模块

ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777 backup=yes' #backup=yes 对文件进行备份

3)用户模块

#创建用户
ansible webserver -m user -a 'name=用户名 state=present'
#生成密码
echo '密码' | openssl passwd -1 -stdin
#修改密码
ansible webserver -m user -a 'name=用户名 password="密码"'
#修改shell
ansible webserver -m user -a 'name=用户名 shell=/sbin/nologin append=yes'
#删除用户
ansible webserver -m user -a 'name=用户名 state=absent'

4)软件包管理

#升级所有包
ansible host1 -m yum -a 'name="*" state=latest'
#安装apache
ansible host2 -m yum -a 'name="httpd" state=latest'

5)服务模块

#启动
ansible host2 -m service -a 'name=httpd state=started'
#开机自启
ansible host2 -m service -a 'name=httpd state=started'
#停止
ansible host2 -m service -a 'name=httpd state=stopped'
#重启
ansible host2 -m service -a 'name=httpd state=restarted'
#禁止开机自启
ansible host2 -m service -a 'name=httpd state=started enabled=no'

6)文件模块

#创建文件
ansible host1 -m file -a 'path=/tmp/88.txt mode=777 state=touch'
#创建目录
ansible host1 -m file -a 'path=/tmp/99 mode=777 state=directory'

7)收集模块

#查询所有信息
ansible host3 -m setup
ansible host3 -m setup -a 'filter=ansible_all_ipv4_addresses'

4、Role-角色扮演

以安装nginx为例

1)目录结构

在这里插入图片描述

#准备目录结构
mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p
touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
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 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=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: host4
  roles:
  - nginx

7)实施

cd rles
#测试
ansible-playbook site.yaml --syntax-check
#实施剧本
ansible-playbook site.yaml
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值