22 June, 2016

adding authorized keys using ansible


You can use jinja template engine to set this

vars:
  ssh_user_key:
    - user: ritesh
      exclusive: no
      keys:
        - ritesh.centos
        - ritesh.fedora

- name: ssh user key upload
  authorized_key:
    user: "{{ item.user }}"
    exclusive: "{{ item.exclusive if item.exclusive is defined else 'no' }}"
    key: "{% for file_name in item['keys'] %} {{ key|default('') + lookup('file', file_name) }}{% if not loop.last %}{{ '\n' }}{% endif %} {% endfor %}"
  become_user: "{{ item.user }}"
  become: yes

  with_items:
    - "{{ ssh_user_key }}"
  tags:
    - setup-user-ssh-client-key