ansible 配置了端口在host文件但是还要走22 ip:60001 ansible_ssh_port=60001

本文记录了一次使用Ansible进行远程主机管理时遇到的连接失败问题及其解决方法,并介绍了如何通过Ansible更新目标主机的sudoers文件以确保特定命令无需密码即可执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fatal: [101.251.194.102]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 101.251.194.102 port 22: Connection refused\r\n", "unreachable": true}

 

ansible_ssh_port=60001

 

- hosts: zss
  tasks:
    - name: step1 
      ping:
    - name: step2 统计
      shell: egrep KFQX /etc/sudoers|wc -l
      ignore_errors: True
      register: check_value
    - name: step3 
      shell: echo "Cmnd_Alias KFQX =/usr/local/bin/supervisorctl, /usr/bin/supervisorctl, /bin/cat, /bin/ls, /usr/bin/head, /usr/bin/tail, /bin/grep, /usr/local/php/bin/php, /usr/bin/crontab, /usr/bin/python2.7, /usr/bin/python, /usr/bin/python3, /usr/bin/svn" >>/etc/sudoers
      when: check_value.stdout|int < 1

 

转载于:https://www.cnblogs.com/gaoyuechen/p/9674033.html

--- - hosts: all remote_user: root gather_facts: no vars: mysql_start_timeout: 120 # MySQL启动超时时间(秒) mysql_check_interval: 5 # 检查间隔(秒) tasks: #-------------1. Shutdown source slave and notify - name: shutdown source slave instance shell: /usr/local/mysql5.1/bin/mysqladmin -S /tmp/mysql-{{ port }}.sock -ugyop -p"o3Q!$udHUrh7cEC@" shutdown when: group_names.0 == "source_host" ignore_errors: yes - name: send wechat notification for slave shutdown uri: url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=47ca4cfb-c6dc-49d6-ab82-619aea42e986" method: POST body_format: json body: msgtype: "markdown" markdown: content: |- **<font color="warning">级别:S3</font>** **标题: 从库关闭通知** **区组id**: {{ dist_id }} **数据库**: {{ app_name }} **端口**: {{ port }} **实例状态**: <font color="red">已关闭</font> **操作**: 从库已关闭准备备份 **主机**: {{ hostvars[groups['source_host'][0]]['ansible_ssh_host'] }} when: group_names.0 == "source_host" #-------------2. Register backup directory names - name: register data dir name shell: echo "data-2-{{ dist_id }}-{{ app_name }}-{{ port }}" register: data_dir_name - name: register log dir name shell: echo "log-2-{{ dist_id }}-{{ app_name }}-{{ port }}" register: log_dir_name #-------------3. Target server prepare MySQL instance - name: get mysql deploy script shell: wget http://208.gyyx.cn/games/wd/mysql/install_mysql_wd_5.7_v1.sh -O /root/install_mysql_wd_5.7_v1.sh when: group_names.0 == "target_host" - name: running script to deploy mysql instance shell: bash /root/install_mysql_wd_5.7_v1.sh 2 {{ dist_id }} {{ app_name }}_s {{ port }} 5.7.36 wd_5.7 {{ stat_ip }} when: group_names.0 == "target_host" - name: replace conf innodb_buffer_pool_size adb shell: sed -i 's/innodb_buffer_pool_size = 6G/innodb_buffer_pool_size = 3G/' /home/mysql/etc/2-{{ dist_id }}-{{ app_name }}-{{ port }}.cnf when: group_names.0 == "target_host" and app_name == "adb" - name: replace conf innodb_buffer_pool_size ldb shell: sed -i 's/innodb_buffer_pool_size = 4G/innodb_buffer_pool_size = 2G/' /home/mysql/etc/2-{{ dist_id }}-{{ app_name }}-{{ port }}.cnf when: group_names.0 == "target_host" and app_name == "ldb" #-------------4. Setup SSH key transfer for secure copy - name: delete old ssh_key shell: 'rm -f ./centos_key*' when: group_names.0 == "source_host" - name: Generate RSA key pair on the source server shell: | ssh-keygen -t rsa -b 2048 -f ~/.ssh/centos_key -N "" when: group_names.0 == "source_host" - name: Fetch public key from the source server fetch: src: ~/.ssh/centos_key.pub dest: ./keys/ flat: yes when: group_names.0 == "source_host" - name: Add public key to authorized_keys on the target server lineinfile: path: ~/.ssh/authorized_keys line: "{{ lookup('file', './keys/centos_key.pub') }}" create: yes when: group_names.0 == "target_host" - name: target_host add ssh port 8002 shell: sed -i '$a\Port 8002' /etc/ssh/sshd_config when: group_names.0 == "target_host" - name: restart sshd service service: name=sshd state=restarted when: group_names.0 == "target_host" #-------------5. Transfer data and log directories - name: transfer data directory shell: scp -P 8002 -i /root/.ssh/centos_key -o "StrictHostKeyChecking no" -rp /home/mysql/{{ data_dir_name.stdout }} root@{{ target_ip }}:/home/mysql/ when: group_names.0 == "source_host" - name: transfer log directory shell: scp -P 8002 -i /root/.ssh/centos_key -o "StrictHostKeyChecking no" -rp /home/mysql/{{ log_dir_name.stdout }} root@{{ target_ip }}:/home/mysql/ when: group_names.0 == "source_host" #-------------6. Start MySQL instance on target and check port - name: start mysql instance on target shell: chown -R mysql.mysql /home/mysql/{{ data_dir_name.stdout }} /home/mysql/{{ log_dir_name.stdout }} && /usr/local/mysql5.7/bin/mysqld_safe --defaults-file=/home/mysql/etc/2-{{ dist_id }}-{{ app_name }}-{{ port }}.cnf --user=mysql & when: group_names.0 == "target_host" async: "{{ mysql_start_timeout }}" poll: 0 register: target_mysql_start - name: check if target mysql port is listening shell: | # 检查端口是否监听 end_time=$(( $(date +%s) + {{ mysql_start_timeout }} )) while [ $(date +%s) -lt $end_time ]; do if netstat -tln | grep -q :{{ port }}; then echo "PORT_LISTENING" exit 0 fi sleep {{ mysql_check_interval }} done echo "PORT_NOT_LISTENING" exit 1 when: group_names.0 == "target_host" register: target_port_check failed_when: "'PORT_NOT_LISTENING' in target_port_check.stdout" - name: send wechat notification for target mysql startup success uri: url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=47ca4cfb-c6dc-49d6-ab82-619aea42e986" method: POST body_format: json body: msgtype: "markdown" markdown: content: |- **<font color="info">级别:S3</font>** **标题: 目标MySQL实例启动成功** **区组id**: {{ dist_id }} **数据库**: {{ app_name }} **端口**: {{ port }} **实例状态**: <font color="green">端口监听正常</font> **主机**: {{ hostvars[groups['target_host'][0]]['ansible_ssh_host'] }} **操作结果**: ✅ 目标实例启动成功,端口 {{ port }} 已监听 when: - group_names.0 == "target_host" - target_port_check.rc == 0 - name: send wechat notification for target mysql startup failure uri: url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=47ca4cfb-c6dc-49d6-ab82-619aea42e986" method: POST body_format: json body: msgtype: "markdown" markdown: content: |- **<font color="warning">级别:S1</font>** **标题: 目标MySQL实例启动失败** **区组id**: {{ dist_id }} **数据库**: {{ app_name }} **端口**: {{ port }} **实例状态**: <font color="red">端口未监听</font> **主机**: {{ hostvars[groups['target_host'][0]]['ansible_ssh_host'] }} **错误信息**: 端口 {{ port }} 未在 {{ mysql_start_timeout }} 秒内开始监听 **紧急程度**: 🔴 需要立即处理 when: - group_names.0 == "target_host" - target_port_check.rc != 0 #-------------7. Cleanup SSH configuration - name: clean ssh config shell: sed -i '/Port 8002/d' /etc/ssh/sshd_config when: group_names.0 == "target_host" - name: restart sshd service service: name=sshd state=restarted when: group_names.0 == "target_host" - name: Remove public key from authorized_keys lineinfile: path: ~/.ssh/authorized_keys line: "{{ lookup('file', './keys/centos_key.pub') }}" state: absent when: group_names.0 == "target_host" - name: clean source_host ssh key shell: rm -f /root/.ssh/centos_key when: group_names.0 == "source_host" #-------------8. Restart source slave and check port - name: restart source slave instance shell: /usr/local/mysql5.7/bin/mysqld_safe --defaults-file=/home/mysql/etc/2-{{ dist_id }}-{{ app_name }}-{{ port }}.cnf --user=mysql & when: group_names.0 == "source_host" async: "{{ mysql_start_timeout }}" poll: 0 register: source_mysql_start - name: check if source mysql port is listening shell: | # 检查端口是否监听 end_time=$(( $(date +%s) + {{ mysql_start_timeout }} )) while [ $(date +%s) -lt $end_time ]; do if netstat -tln | grep -q :{{ port }}; then echo "PORT_LISTENING" exit 0 fi sleep {{ mysql_check_interval }} done echo "PORT_NOT_LISTENING" exit 1 when: group_names.0 == "source_host" register: source_port_check failed_when: "'PORT_NOT_LISTENING' in source_port_check.stdout" - name: send wechat notification for source slave startup success uri: url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=47ca4cfb-c6dc-49d6-ab82-619aea42e986" method: POST body_format: json body: msgtype: "markdown" markdown: content: |- **<font color="info">级别:S3</font>** **标题: 源从库启动成功** **区组id**: {{ dist_id }} **数据库**: {{ app_name }} **端口**: {{ port }} **实例状态**: <font color="green">端口监听正常</font> **主机**: {{ hostvars[groups['source_host'][0]]['ansible_ssh_host'] }} **操作结果**: ✅ 源从库已成功重启,端口 {{ port }} 已监听 when: - group_names.0 == "source_host" - source_port_check.rc == 0 - name: send wechat notification for source slave startup failure uri: url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=47ca4cfb-c6dc-49d6-ab82-619aea42e986" method: POST body_format: json body: msgtype: "markdown" markdown: content: |- **<font color="warning">级别:S2</font>** **标题: 源从库启动失败** **区组id**: {{ dist_id }} **数据库**: {{ app_name }} **端口**: {{ port }} **实例状态**: <font color="red">端口未监听</font> **主机**: {{ hostvars[groups['source_host'][0]]['ansible_ssh_host'] }} **错误信息**: 端口 {{ port }} 未在 {{ mysql_start_timeout }} 秒内开始监听 **紧急程度**: 🟡 需要尽快处理 when: - group_names.0 == "source_host" - source_port_check.rc != 0 ---检查端口监听逻辑如何优化
最新发布
08-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值