ANSIBLE(Ansible)自动化运维

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

Ansible示图:

一、

install 部署(下面通过详细步骤来认识学习Ansible)

dns resolve          环境

ansible 服务器

10.1.1.64        192.168.1.115

ansible 客户机(无需配置)

10.1.1.84        192.168.1.104

10.1.1.82        192.168.1.105

10.1.1.83        192.168.1.108

10.1.1.95        192.168.1.109

ansible服务器配置:

#vim /etc/hosts

192.168.1.115 ansible

192.168.1.104 hots1

192.168.1.105 host2

192.168.1.108 host3

192.168.1.109 host4

#install ansible

yum install -y epel-release

        #环境配置

        rm  -rf /etc/yum.repos.d/*

        wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

        wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 

yum install -y ansible

        #配置

        rpm -ql ansible        #列出所有文件

        rpm -qc ansible        #查看配置文件

        ansible --help        #查看ansible帮助

        ansible-doc -l         #查看所有模块

ssh-key(可选)

免密码ssh-key方式        ssh-kengen        ssh-copy-id IP地址(推送公钥)

ansible基础命令测试

定义主机清单

vim /etc/ansible/hosts

#故意去掉host4,方便示范

host1

host2

host3

测试连通性

ansible localhost -m ping        # "-m" 指定模块,“ping”连通性模块,还有“yum”“shell”等模块

        简洁输出(host1的连通性测试)ansible host1 -m ping -o

know_hosts

1)ansible host2 -m ping #失败了

2)ansible host2 -m ping -u root -k -o #增加用户名和密码选项

3)vim /etc/ssh/ssh_config

      StrictHostKeyChecking no

      systemctl restart sshd        #取消输出“yes/no”询问选项

错误示范

ansible host4 -m ping -u root -k -o        #失败,主机清单未标注该主机

###请注意ping和ssh

关闭主机host1的sshd,进行ping测试,结论是ansible的ping是检测ssh程序是否连接。

Inventory-主机清单

vim /etc/ansible/hosts        #添加主机组

[webserver]

host1

hos2

host3

host4

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"}

增加用户名和密码

vim /etc/ansible/hosts

添加:

[webserver]
host[1:4] ansible_ssh_user='root' ansible_ssh_pass='666666'

ansible webservers -m ping -o #免用户名和密码成功

#主机和主机的用户名密码不一样设置

vim /etc/ansible/hosts

#添加

[webservers]
host1 ansible_ssh_user='root' ansible_ssh_pass='777777'
host[2:4] ansible_ssh_user='root' ansible_ssh_pass='666666'

增加端口

vim /etc/ssh/sshd_config

Port 2222

systemctl restart sshd

ansible webservers -m ping -o        #失败,因为默认端口已更改

vim /etc/ansible/hosts

[webserver]
host1 ansible_ssh_user='root' ansible_ssh_pass='777777' ansible_ssh_port='2222'
host[2:4] ansible_ssh_user='root' ansible_ssh_pass='666666'

组:变量

常用变量例图:

子分组示例

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='666666'

自定义主机列表

vim hostlist                #需要注意密码

[dockers]
host1
host2
[dockers:vars]
ansible_ssh_user='root'
ansible_ssh_pass='666666'

ansible -i hostlist dockers -m ping -o

Ad-Hoc-点对点模式

在ansible中是指需要快速执行的单条命令,并且不需要保存的命令。对于复杂的命令则写 playbook。

shell模块

ansible-doc shell        #帮助

ansible webserver -m shell -a "hostname" -o        #获取主机名

ansible webserver -m shell -a 'hostname' -o -f 2        # -f 2 指定线程数

简介: -f FORKS, --forks=FORKS  Ansible一次命令执行并发的线程数。NUM被指定为一个整数,默认是5
specify number of parallel processes to use
 (default=5) 

ansible host2 -m shell -a 'yum -y install httpd' -o        #创建apache

ansible host3 -m shell -a "uptime" -o        #查询系统负载

copy复制模块

使用:

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

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

user用户模块

ansible webserver -m user -a 'name=lsy state=present'        #创建用户

ansible webserver -m user -a 'name=lsy state=absent'        #删除用户

生成加密码:

echo '777777' | openssl passwd -1 -stdin

得到加密密码:$JakSjdHj&ajhdjakhdj.259972

修改密码:

ansible webserver -m user -a 'name=qianfeng password="$JakSjdHj&ajhdjakhdj.259972"'

修改shell

ansible webserver -m user -a 'name=lsy shell=/sbin/nologin append=yes'

yum软件包模块

ansible-doc yum        #帮助

升级所有软件包

ansible host1 -m yum -a 'name="*" state=latest'

安装apache

ansible host2 -m yum -a 'name="httpd" state=latest'

service服务模块

ansible host2 -m service -a 'name=httpd state=started'        #启动

ansible host2 -m service -a 'name=httpd state=started enabled=yes'        #开机自启

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'        #开机禁止启动

file文件模块

ansible host1 -m file -a 'path=/tmp/lsy.txt mode=888 state=touch'        #创建文件

ansible host1 -m file -a 'path=/tmp/66 mode=888 state=directory'        

setup收集模块

ansible host2 -m setup        #查询所有信息

ansible host3 -m setup -a 'filter=ansible_all_ipv4_addresses'

通过yum编写playbook

#清理环境

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 修改端口

编写剧本:

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.yaml

#登录

http://IP:8080(端口号)

Role

任务:通过role远程部署nginx

定义:roles则是在ansible中,playbooks的目录组织结构。将代码或文件进行模块化,成为roles的文件目录组织结构,易读,代码可重用,层次清晰。

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 1108 > roles/nginx/files/index.html                #最后网页验证成功的输出结果
yum install -y nginx && cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2

编写task:

vim roles/nginx/tasks/main.yaml

##########

---
- name: install nginx packge
  yum: name={{ item }} state=latest
  with_items:
  - epel-release
  - nginx

- 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

配置文件:
vim roles/nginx/templates/nginx.conf.j2

######
    worker_processes  {{ ansible_processor_cores }};        #  调用内部已知变量
      
    worker_connections {{ worker_connections }};        #自定义变量

#######

编写变量:

vim roles/nginx/vars/main.yaml
    worker_connections: 10240  

编写处理程序:

vim roles/nginx/handlers/main.yaml
    ---
- name: restart nginx
  service: name=nginx state=restarted

#####

编写剧本playbook:

vim roles/site.yaml

#####
    - hosts: host4
  roles:
  - nginx

#####

验证实施

cd roles
ansible-playbook site.yaml --syntax-check        #测试
ansible-playbook site.yaml        #实施剧本
验证host4
 https://IP(web界面输出1108)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好好学技术oH

你的鼓励是一起学习的动力何阶梯

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值