跳转至

14. 部署静态网站ansible脚本

1. 配置ansible要操作的机器!

sudo -i
vim /etc/ansible/hosts
[myweb]
10.0.8.2
10.0.8.3

2. ssh免密登录

# 前面已经做过,可以忽略,直接第三步测试即可!
#1.生成密钥
ssh-keygen -f ~/.ssh/id_rsa -P '' >/dev/null 2>&1

#2.发送公钥 
ssh-copy-id ubuntu@10.0.8.2
ssh-copy-id ubuntu@10.0.8.3

#3.测试
ssh ubuntu@10.0.8.2
ssh ubuntu@10.0.8.3

3. ansible测试

ansible myweb -m shell -a "hostname"
root@4c16g:~# ansible myweb -m shell -a "hostname"
10.0.8.3 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: root@10.0.8.3: Permission denied (publickey,password).",
    "unreachable": true
}
10.0.8.2 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: root@10.0.8.2: Permission denied (publickey,password).",
    "unreachable": true
}
# 发现:ssh的用户是root!

修改hosts文件,指定登录用户!

vim /etc/ansible/hosts
[myweb]
10.0.8.2 ansible_user=ubuntu
10.0.8.3 ansible_user=ubuntu

再次测试!

root@4c16g:~# ansible myweb -m shell -a "hostname"
[DEPRECATION WARNING]: Distribution ubuntu 20.04 on host 10.0.8.3 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior Ansible
releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation warnings can
be disabled by setting deprecation_warnings=False in ansible.cfg.
10.0.8.3 | CHANGED | rc=0 >>
k8s-master
[DEPRECATION WARNING]: Distribution ubuntu 20.04 on host 10.0.8.2 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior Ansible
releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation warnings can
be disabled by setting deprecation_warnings=False in ansible.cfg.
10.0.8.2 | CHANGED | rc=0 >>
zabbix

去除warning信息

cd /etc/ansible
cp ansible.cfg{,.bak}
vim ansible.cfg
# 打开180行注释,将true改为false
180 deprecation_warnings = False

再次测试

root@4c16g:/etc/ansible# ansible myweb -m shell -a "sudo hostname"
[WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo
10.0.8.3 | CHANGED | rc=0 >>
k8s-master
10.0.8.2 | CHANGED | rc=0 >>
zabbix
# 发现warning已经没有了!!!
root@4c16g:/etc/ansible# ansible myweb -m shell -a "hostname"
10.0.8.3 | CHANGED | rc=0 >>
k8s-master
10.0.8.2 | CHANGED | rc=0 >>
zabbix

4. ansible脚本命令测试

#1. 查看当前用户是否为root
id -u
echo $(id -u)
if [ $(id -u)==0 ];then echo "当前用户是root";else echo "当前用户不是root";

#2. 删除站点目录
ansible myweb -m shell -a "sudo rm -rf /service/chupeng130/*"

#3. 拉取代码(前提:已经配置好免密拉取!)
ansible myweb -m shell -a "cd /service/chupeng130 && sudo git clone https://gitee.com/chupeng130/chupeng130.git"

# 补充:git免密拉取
sudo -i
mkdir -p /service/chupeng130
cd /service/chupeng130
rm -rf *
git config --global credential.helper store
git clone https://gitee.com/chupeng130/chupeng130.git
rm -rf *
git clone https://gitee.com/chupeng130/chupeng130.git

5. 编写脚本

cd /jenkins-scripts
vim ansible.sh
#!/bin/bash
#1.判断当前用户是否为root
if [ $(id -u)==0 ];then 
  echo "当前用户是root,可以继续执行!"
else 
  echo "当前用户不是root,本脚本需要root执行";
  exit 
fi 
#2. 删除站点目录
echo 'step1:开始删除旧的网站内容'
ansible myweb -m shell -a "sudo rm -rf /service/chupeng130/*"
if [  $? -eq 0  ];then
  echo 'step1:删除完成!'
else
  echo 'step1出现问题!'
  exit
fi 
#3. 拉取代码(前提:已经配置好免密拉取!)
echo 'step2:开始拉取最新代码'
ansible myweb -m shell -a "cd /service/chupeng130 && sudo git clone https://gitee.com/chupeng130/chupeng130.git"
if [  $? -eq 0  ];then
  echo 'step2:拉取完成!'
else
  echo 'step2出现问题!'
  exit
fi 

6. playbook剧本

编写剧本

cd /jenkins-scripts
vim deploy_chupeng130.yaml
# 待补充!
- hosts: myweb
  tasks:
    - name: step01,delete old chupeng130
      shell: sudo rm -rf /service/chupeng130/*
    - name: step02,pull latest chupeng130
      shell: cd /service/chupeng130 && sudo git clone https://gitee.com/chupeng130/chupeng130.git

执行剧本

# 检查语法
ansible-playbook  /jenkins-scripts/deploy_chupeng130.yaml --syntax-check

# 执行剧本
ansible-playbook  /jenkins-scripts/deploy_chupeng130.yaml

最后更新: 2022-02-25 03:53:42