最近手里的数百台服务器需要改/etc/ssh/sshd_config的参数,禁止root直接登陆,也就是说

[root@t0 ~]# cat /etc/ssh/sshd_config | grep Root
#PermitRootLogin yes

得改成

[root@t1 ~]# cat /etc/ssh/sshd_config| grep ^PermitRoot
PermitRootLogin no

一台台登上去改简直要死,ansible自动化运维工具听说还不错,之前就会用命令直接使用shell模块执行sed 替换配置文件里面的参数

[root@t0 ansible]# ansible all -b --become-method=su --become-user=root -m shell -a "sed 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config" | grep -E "Root|10.0"
[WARNING]: Consider using the replace, lineinfile or template module rather than running sed. If you need to use
command because replace, lineinfile or template is insufficient you can add warn=False to this command task or set
command_warnings=False in ansible.cfg to get rid of this message.
10.0.0.52 | CHANGED | rc= >>
# $OpenBSD: sshd_config,v 1.93 // :: djm Exp $
PermitRootLogin no
# the setting of "PermitRootLogin without-password".
10.0.0.53 | CHANGED | rc= >>
# $OpenBSD: sshd_config,v 1.93 // :: djm Exp $
PermitRootLogin no
# the setting of "PermitRootLogin without-password".

大概看个效果,这样做不仅效率低,而且你不能保证每台服务器的配置文件都是

#PermitRootLogin yes

也有可能是PermitRootLogin yes

所以不一定能达到自己的要求

然后根据提示信息,我看到了
replace, lineinfile template这三个模块
感兴趣的朋友可以自行研究一下
我今天用的是playbook用正则表达式匹配要更改的项
首先自己写一个playbook
[root@t0 playbook]# cat change_ssh.yml
---
- hosts: test
gather_facts: false
# become: yes
# become_method: su
remote_user: root
tasks:
- name: "change file"
lineinfile:
path: /etc/ssh/sshd_config
regexp: '^PermitRootLogin'
line: 'PermitRootLogin yes'
state: present
# notify: restart sshd
# handlers:
# - name: restart sshd
# service: name=sshd state=restarted

之前写的是没有注释的那些,注释的内容是我后来加上去的,待会再解释注释的意思

然后我的hosts文件

[root@t0 playbook]# cat ../hosts
#[localhost]
#127.0.0.1
[test]
10.0.0.52
10.0.0.53
[test:vars]
ansible_ssh_pass=
ansible_ssh_user=test
ansible_become_pass=

执行以下命令

[root@t0 playbook]# ansible-playbook change_ssh.yml 

PLAY [test] ************************************************************************************************************

TASK [change file] *****************************************************************************************************
fatal: [10.0.0.53]: FAILED! => {"changed": false, "module_stderr": "Shared connection to 10.0.0.53 closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n File \"/home/test/.ansible/tmp/ansible-tmp-1555555638.07-154814815296297/AnsiballZ_lineinfile.py\", line 113, in <module>\r\n _ansiballz_main()\r\n File \"/home/test/.ansible/tmp/ansible-tmp-1555555638.07-154814815296297/AnsiballZ_lineinfile.py\", line 105, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File \"/home/test/.ansible/tmp/ansible-tmp-1555555638.07-154814815296297/AnsiballZ_lineinfile.py\", line 48, in invoke_module\r\n imp.load_module('__main__', mod, module, MOD_DESC)\r\n File \"/tmp/ansible_lineinfile_payload_1q9ATP/__main__.py\", line 524, in <module>\r\n File \"/tmp/ansible_lineinfile_payload_1q9ATP/__main__.py\", line 515, in main\r\n File \"/tmp/ansible_lineinfile_payload_1q9ATP/__main__.py\", line 257, in present\r\nIOError: [Errno 13] Permission denied: '/etc/ssh/sshd_config'\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": }
fatal: [10.0.0.52]: FAILED! => {"changed": false, "module_stderr": "Shared connection to 10.0.0.52 closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n File \"/home/test/.ansible/tmp/ansible-tmp-1555555638.06-7599860498373/AnsiballZ_lineinfile.py\", line 113, in <module>\r\n _ansiballz_main()\r\n File \"/home/test/.ansible/tmp/ansible-tmp-1555555638.06-7599860498373/AnsiballZ_lineinfile.py\", line 105, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File \"/home/test/.ansible/tmp/ansible-tmp-1555555638.06-7599860498373/AnsiballZ_lineinfile.py\", line 48, in invoke_module\r\n imp.load_module('__main__', mod, module, MOD_DESC)\r\n File \"/tmp/ansible_lineinfile_payload_5xlcxo/__main__.py\", line 524, in <module>\r\n File \"/tmp/ansible_lineinfile_payload_5xlcxo/__main__.py\", line 515, in main\r\n File \"/tmp/ansible_lineinfile_payload_5xlcxo/__main__.py\", line 257, in present\r\nIOError: [Errno 13] Permission denied: '/etc/ssh/sshd_config'\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": }
to retry, use: --limit @/etc/ansible/playbook/change_ssh.retry PLAY RECAP *************************************************************************************************************
10.0.0.52 : ok= changed= unreachable= failed=
10.0.0.53 : ok= changed= unreachable= failed=

结果是失败的,根据报错信息

IOError: [Errno ] Permission denied: '/etc/ssh/sshd_config'

怀疑是权限的问题,因为根据我的hosts文件,默认是普通用户test去执行,所以在playbook里面加上两行

#  become: yes
# become_method: su

意思就是,我ansible远程操作的时候,用户使用的时管理员的权限,然后再次执行

[root@t0 playbook]# ansible-playbook change_ssh.yml 

PLAY [test] ************************************************************************************************************

TASK [change file] *****************************************************************************************************
changed: [10.0.0.52]
changed: [10.0.0.53] PLAY RECAP *************************************************************************************************************
10.0.0.52 : ok= changed= unreachable= failed=
10.0.0.53 : ok= changed= unreachable= failed=

查看远程主机的配置文件

[root@t1 ~]# cat /etc/ssh/sshd_config| grep ^PermitRoot
PermitRootLogin no

说明已经改成功了,但是如果配置文件有其他的设置,我们还需要其他的正则,而且sshd的配置文件改完之后需要重启才能生效,所以需要一个触发器,playbook需要做一下调整

---
#- hosts: 'test:!c2'
- hosts: c2
#- hosts: "`hosts`"
gather_facts: false
become: yes
become_method: su
remote_user: root
tasks:
- name: 'change sshd_config'
lineinfile:
dest: /etc/ssh/sshd_config
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
state: present
with_items:
#In this way
# - { regexp: "^#PermitRootLogin yes",line: "PermitRootLogin no" }
#or
# - regexp: "^#PermitRootLogin yes"
# line: "PermitRootLogin no"
#Both methods have the same effect
- { regexp: "PermitRootLogin yes",line: "PermitRootLogin no" }
notify: restart sshd
handlers:
- name: restart sshd
service: name=sshd state=restarted

总结:1. ansible如果hosts文件写的ansible_user时普通用户的时候,远程操作需要管理员权限,需要become的

  become: yes
become_method: su

或者是

  become: yes
become_method: sudo
remote_user: test

  2. 执行完操作之后,利用触发器notify重启sshd服务

  3. 需要继续学习,谢谢

ansible playbook批量改ssh配置文件,远程用户Permission denied的更多相关文章

  1. Ansible playbook 批量修改服务器密码 先普通后root用户

    fsckzy   Ansible playbook 批量修改服务器密码 客户的需求:修改所有服务器密码,密码规则为Rfv5%+主机名后3位 背景:服务器有CentOS6.7,SuSE9.10.11,r ...

  2. [转载] Ubuntu 12.04下安装git,SSH及出现的Permission denied解决办法

    如何安装ssh http://os.51cto.com/art/201109/291634.htm 仅需要阅读至成功开启ssh服务即可 http://www.linuxidc.com/Linux/20 ...

  3. 记一次root用户在本地登录及SSH连接均遭遇permission denied的问题排查经过

    某日一位老师反映,机房的6号节点无法登录了.一开始以为是为节点防火墙配置IP白名单时忘记了加进去,但随后发现此节点并未进行白名单配置,密码也一直未有变更,于是在自己的电脑上连接,发现终端里很快显示出了 ...

  4. ssh免密码登录Permission denied (publickey,gssapi-keyex,gssapi-with-mic) 的解决方案!

    当出现Permission denied (publickey,gssapi-keyex,gssapi-with-mic) 警告的时候,恭喜你,你已经离成功很近了. 远程主机这里设为slave2,用户 ...

  5. Mac ssh 连接报错 Permission denied (publickey)

    用的阿里云服务器,如果直接连接,会报下面错误: $ ssh root@47.94.132.115 Permission denied (publickey). 创建服务器的时候,连接秘钥会生成并下载到 ...

  6. linux环境中,ssh登录报错,Permission denied, please try again.

    问题描述: 今天早上一个同事反应一个问题,通过ssh登录一台测试机的时候,发现两个账号,都是普通账号,一个账号能够登录, 另外一个账号无法登录.问他之前有做过什么变更吗,提到的就是之前有升级过open ...

  7. Debian8 远程登录Permission Denied,please try again

    多数是系统没有开启Root登录的权限. 修改root的ssh权限: ① vi /etc/ssh/sshd_config ② 找到配置项PermitRootLogin 将此项的值改为yes ③ 重启ss ...

  8. 使用ssh连接数据库时出现Permission denied, please try again.解决方案

    安装ssh(如果已经安装则会覆盖)sudo apt-get install openssh-server找到/etc/ssh/sshd_config这个文件 将permitrootlogin的值设置为 ...

  9. 关于git的ssh permission denied原因汇总

    SSH关于公钥认证Permission denied (publickey,gssapi-with-mic的问题 http://h2appy.blog.51cto.com/609721/1112797 ...

随机推荐

  1. thinkphp5 去除缓存

    array_map('unlink', glob(TEMP_PATH . '/*.php')); rmdir(TEMP_PATH);

  2. hadoop生态之mapReduce-Yarn

    一.inputSplit 1.什么是block 块是以 block size 进行划分数据. 因此,如果群集中的 block size 为 128 MB,则数据集的每个块将为 128 MB,除非最后一 ...

  3. Github 开源项目(一)websocketd (实战:实时监控服务器内存信息)

    websocketd 是WebSocket守护进程,它负责处理WebSocket连接,启动您的程序来处理WebSockets,并在程序和Web浏览器之间传递消息. 安装:websocketd wget ...

  4. js 遍历集合删除元素

    js 遍历集合删除元素 /** * 有效的方式 - 改变下标,控制遍历 */ for (var i = 0; i < arr.length; i++) { if (...) { arr.spli ...

  5. vs 开发 win32 程序,调出控制台窗口,方便调试

    设置方法 项目 -> 属性 -> 生成事件 ->后期生成事件 -> 命令行 中添加 editbin /SUBSYSTEM:CONSOLE $(OutDir)\$(Project ...

  6. mysql的order by注入

    最近在做一些漏洞盒子后台项目的总结,在盒子众多众测项目中,注入类的漏洞占比一直较大.其中Order By注入型的漏洞也占挺大一部分比例,这类漏洞也是白帽子乐意提交的类型(奖金高.被过滤概览小).今天给 ...

  7. luogu P5302 [GXOI/GZOI2019]特技飞行

    传送门 强行二合一可还行 首先\(c\)的贡献是不会变的,先考虑求出多少交点被矩形覆盖,交点的话可以按左端点纵坐标从下到上顺序枚举一条线段,然后维护右端点纵坐标的set,把之前处理过线段的右端点放进s ...

  8. andriod webview和h5

    1.WebBrowserActivity extends BaseActivity 2.setContentView(R.layout.activity_web_html); <WebView ...

  9. 2019年一次java知识点总结

    java基础 数据类型 集合与数据结构 关键字(static,rty ...) IO和网络 多线程(并发与锁,死锁) 异常 简单算法,复杂度 JVM 类加载 java内存模型 对象监听器字节码 垃圾回 ...

  10. CentOS配代理服务器

    背景: 某云上有台Windows主机,为了省钱(...),购买的1M带宽... 然后日常只有我用,特别卡,嫌弃得不行. 最近接触到代理,琢磨代理连接到局域网内带宽大的主机,是否上网速度会蹭蹭得涨?实践 ...