LoginSignup
0
1

More than 5 years have passed since last update.

Ansible の authorized_key で複数の鍵を設置するときに exclusive を指定したい

Posted at

Ansible の authorized_key で複数の SSH 公開鍵を設置するとき、削除にも対応するために exclusive を指定したいですが、下記のようにすると with_file の1回ごとに exclusive になるので、最終的に最後の1個だけが残ります。

- name: Set authorized key took from files
  authorized_key:
    user: oreore
    exclusive: yes
    key: '{{ item }}'
  with_file: '{{ key_files }}'

下記のように keyfor するのが良さそうです。

- name: Set authorized key took from files
  authorized_key:
    user: oreore
    exclusive: yes
    key: |
      {% for item in key_files %}
      {{ lookup('file', item) }}
      {% endfor %}

がしかし、なぜか file ではなく url にすると、1つの URL に改行区切りで複数の鍵が含まれているときに、改行がカンマに置換されてしまい、正しく設定できません。

- name: Set authorized key took from urls
  authorized_key:
    user: oreore
    exclusive: yes
    key: |
      {% for item in key_urls %}
      {{ lookup('url', item) }}
      {% endfor %}

lookup('url', item) で改行が , に置換されるっぽい(with_url が行ごとにループすることに関係が?)。

- debug:
    msg: |
      {{ lookup('url', 'http://example.com') }}

1回 with_url で回しておいて、その結果を authorized_key に使えば良い?

- name: Fetch authorized key took from urls
  debug:
  with_url: '{{ key_urls }}'
  register: res

- name: Set authorized key took from urls
  authorized_key:
    user: oreore
    exclusive: yes
    key: "{{ res.results|map(attribute='item')|join('\n') + '\n' }}"

そもそも url だとチェックモードでも鍵がダウンロードされて遅いので、あからじめダウンロードしておいて file にするほうが良いかも。

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1