2017-03-21 36 views
1

这是一步一步的我想要通过Ansible做到:如何在第一遍时以root身份运行Ansible?

  1. SSH作为根
  2. 安装Python(在Ubuntu中不存在),以及其他基本包。
  3. 创建新的deploy用户和配置/etc/ssh/sshd_config这样PasswordAuhentication noPermitRootLogin no
  4. 重新启动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 
+0

我错过了禁用根访问的部分。在这种情况下,我认为你最好的选择就是单独制作剧本。 – larsks

回答

1

创建定义相同的主机组的两个清单文件:

    中的第一个( bootstrap)在第二个( inventory)限定 ansible_user=regular_user_with_sudo_permissions定义 ansible_user=root

将第二个(inventory)定义为ansible.cfg中的默认库存文件。

无论何时您需要引导新机器,都可以使用选项。在其他情况下省略该选项。

相关问题