其他分享
首页 > 其他分享> > ansible-playbook

ansible-playbook

作者:互联网

ansible-playbook编辑

ansible与shell脚本是一样的,不过是前者更集成了一些,功能的实现更加系统,便于使用和排错,具体使用哪一种,还是觉得合适的才是好的。
一、立个小目标:
# 通过ansible和ssh实现同一个目标:部署一个nginx
# 任务需求:
1、指定主机安装部署nginx;
2、通过机器A完成对B、C机器的控制;
3、成功启动并定制index.html;
二、使用ansible:
1、目录结构
[root@ansible ~]# mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p
[root@ansible ~]# touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
[root@ansible ~]# echo 1234 > roles/nginx/files/index.html
[root@ansible ~]# tree roles/
roles/
├── nginx
│   ├── files
│   │   └── index.html
│   ├── handlers
│   │   └── main.yaml
│   ├── tasks
│   │   └── main.yaml
│   ├── templates
│   └── vars
│       └── main.yaml
└── site.yaml
[root@ansible ~]# yum install -y nginx && cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2
2、编写任务
[root@ansible ~]# 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.准备配置文件
[root@ansible ~]# vim roles/nginx/templates/nginx.conf.j2
worker_processes  {{ ansible_processor_cores }};      # 调用内部已知变量
worker_connections {{ worker_connections }};          # 自定义变量
4.编写变量
[root@ansible ~]# vim roles/nginx/vars/main.yaml
worker_connections: 10240
5.编写处理程序
[root@ansible ~]# vim roles/nginx/handlers/main.yaml
---
- name: restart nginx
  service: name=nginx state=restarted
6.编写剧本
[root@ansible ~]# vim roles/site.yaml
- hosts: host4
  roles:
  - nginx
7.实施
[root@ansible ~]# cd roles
[root@ansible ~]# ansible-playbook site.yaml --syntax-check  # 测试
[root@ansible ~]# ansible-playbook site.yaml                 # 实施剧本

# 到这里,剧本编制部署完成。任务完成。
[root@ansible ~]# 
三、shell脚本开始
[root@ansible ~]# cat install.sh 
#!/usr/bin/bash
# Time:2022/04/10
# Using:install nginx
# Editor: tanuki_11
# email: tanuki_11@163.com

#########################脚本开始###############################
yum -y install expect &>/dev/null
if [ ! -f ~/.ssh/id_rsa ];then
  ssh-keygen -P "" -f ~/.ssh/id_rsa
fi
for ip in `cat ip_list`
do
  {
    ping -c1 $ip &>/dev/null
      if [ $? -eq 0 ];then
        echo "$ip is up!" >>/tmp/ip_up.txt
expect << EOF
spawn ssh-copy-id -i $ip 

expect {
    "yes/no" {send "yes\r";exp_continue}
    "*assword" {send "1\n"}     
}

expect eof              
EOF
      fi
  } 
done
ssh $ip "yum -y install nginx && grep conne /etc/nginx/nginx.conf | sed -r s/[0-9]+/999/g"
#########################脚本结束###############################
三、简单小结
	和首富生活一段时间,就会觉得别墅,跑车不过而已,每天和首富在聊投资,期待星辰和大海,想着自己要开始培养眼光,这样才能抓住商机,成为真正的首富。这是好的,对的,但是对于还在吃窝头的你,有什么用呢?
	生活的难点在于,没有只将“对“”错“两个选项给你,而是给你两个甚至十个都对的选项...
	找一个适合自己的,别选适合首富的。

标签:name,roles,yaml,nginx,ansible,playbook,root
来源: https://www.cnblogs.com/tanukisama/p/16127736.html