这是我通过Ansible尝试完成的一步一步:
deploy
用户并配置/etc/ssh/sshd_config
,以便PasswordAuhentication no
和PermitRootLogin no
。稍后,我正在使用新任务,角色等更新我的Playbook。所以我想针对同一服务器(已root
访问被阻止)重新运行该剧本,这次只是作为新访问创建用户。
由于Ansible正在尝试以root用户身份访问,因此我预计会返回Permission denied
访问权限。
问题:
一种选择是将它分成两个单独的剧本:一个用于配置,一个用于其余的。
# 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
答案 0 :(得分:3)
创建两个定义同一主机组的清单文件:
bootstrap
)中定义ansible_user=root
,inventory
)中定义ansible_user=regular_user_with_sudo_permissions
。将第二个(inventory
)定义为ansible.cfg
中的默认广告资源文件。
每当需要引导新计算机时,使用-i bootstrap
选项运行。在其他情况下省略该选项。
答案 1 :(得分:0)
#行下面的剧本将为您提供成为一个剧本内所有其他用户的愿望,如果您想对两本剧本进行此操作,则只需拆分文件就可以执行相同的操作。您正在寻找的是Ansible的begin_user。只要您知道该用户的密码信息就必须将其存储为变量,但您已经在自己的剧本中将其存储为变量,这样就可以成为任何人。我冒昧地向您展示了另一种方法,能够将密码传递给剧本并在将值传递给配置的计算机之前对其进行加密。您不必为了较低的块而使用该部分,我只是在尝试扩展您的知识库。抱歉,如果您已经知道其中一些内容。我确实担心您会关闭密码身份验证,并且没有为新的“ {{deploy_user}}”创建ssh密钥
---
- hosts: [some_server]
become: true
- vars_prompt:
- name: deploy_pass
prompt: "What is the password for the new user"
confirm: true
private: true
encrypt: "sha512_crypt"
salt_size: 7
- 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
python ubuntu ssh ansible devops
- hosts: [some_server]
become: true
become_user: "{{ deploy_user }}"