1
这是一步一步的我想要通过Ansible做到:如何在第一遍时以root身份运行Ansible?
- SSH作为根
- 安装Python(在Ubuntu中不存在),以及其他基本包。
- 创建新的
deploy
用户和配置/etc/ssh/sshd_config
这样PasswordAuhentication no
和PermitRootLogin no
。 - 重新启动ssh服务。
后来我更新我的新任务,角色,等剧本所以我想重新对运行在同一台服务器的剧本(其中有root
通路受阻),只是这次访问为新创建的用户。
由于Ansible试图以root身份进行访问,我预计会返回Permission denied
访问权限。
问题:
- 如何我只是这样做第一遍为根,然后跳过根本任务(pre_tasks在这种情况下)上的下一个剧本运行?
其中一种选择是将它制作成两个单独的剧本:一个用于省钱,一个用于其余。
# playbook.yml
---
- name: Prepare server
hosts: webserver
gather_facts: False
pre_tasks:
- name: Install python for Ansible
remote_user: root
raw: type /usr/bin/python || (apt -y update && apt install -y python)
- name: Create user
remote_user: root
include_role:
name: deploy-user
roles:
# Future roles here
#roles/deploy-user/tasks/main.yml
---
- group:
name: deploy
state: present
- name: Create Deploy user
user:
name={{ deploy_user }}
comment="Deploy User"
groups="sudo,deploy"
password="{{ deploy_password | password_hash('sha512') }}"
shell=/bin/bash
update_password=on_create
- name: Set authorized key took from files
authorized_key:
user: "{{ deploy_user }}"
state: present
key: "{{ lookup('file', item) }}"
with_items:
- '{{ ssh_authorized_keys }}'
- name: Disallow password authentication
lineinfile:
dest: /etc/ssh/sshd_config
regexp: "^PasswordAuthentication"
line: "PasswordAuthentication no"
state: present
- name: Disallow root SSH access
lineinfile:
dest: /etc/ssh/sshd_config
regexp: "^PermitRootLogin"
line: "PermitRootLogin no"
state: present
- name: restart-sshd
remote_user: root
service: name=ssh state=restarted
我错过了禁用根访问的部分。在这种情况下,我认为你最好的选择就是单独制作剧本。 – larsks