使用WHEN以不同于root的其他用户身份登录服务器

时间:2019-07-18 15:14:08

标签: ansible

我有一个小问题,因为我想拆分配置以获得更好的语法:

命令我如何运行ansible:

ansible-playbook -i hosts.inv setup.yml

我的setup.yml文件:

- hosts: aws-centos
  user: centos
  roles:
    - { role: preconfigure-aws-centos,         tags: preconfigure-aws-centos }

- hosts: aws-debian
  user: debian
  roles:
    - { role: preconfigure-aws-debian,         tags: preconfigure-aws-debian }

我想做一个可识别的OS版本,如果是centos,则使用用户“ centos”。如果是debian,则使用用户“ debian”。

所以我准备了这样的东西: setup.yml:

- hosts: aws-all
  user: root
  roles:
    - { role: preconfigure-aws,         tags: preconfigure-aws }

roles / preconfigure-aws / tasks / main.yml:

- include_tasks: centos.yml
  when: ansible_distribution == 'CentOS'

- include_tasks: debian.yml
  when: ansible_distribution == 'Debian'

roles / preconfigure-aws / tasks / centos.yml:

- name: Activate root account
  shell: cp -r /home/centos/.ssh/authorized_keys /root/.ssh/authorized_keys
  become: yes
  become_user: root
  vars:
    ansible_user: centos

现在:

ansible-playbook -i hosts.inv setup.yml
致命:[tstserver]:无法达到! => {“已更改”:false,“ msg”:“无法通过ssh连接到主机: root @ tstserver :权限被拒绝(publickey,gssapi-keyex,gssapi-with-mic)。 “,” unreachable“:true}

这是因为ansible仍尝试以“ root”身份而非“ centos”身份运行。

你有什么主意吗?

0 个答案:

没有答案