尝试将两个人的Github公钥添加到用户的授权用户文件中。 我能够成功检索SSH密钥:
--- - hosts: 127.0.0.1 connection: local vars: my_users: belminf: "belminf" bob: "tmessins" tasks: - name: Retrieving all keys from GitHub shell: /usr/bin/curl https://github.com/{{ item.value }}.keys 2> /dev/null register: ssh_keys with_dict: my_users - debug: var=ssh_keys
但是,我不确定如何通过ssh_keys结果循环,并使用authorized_keys任务来添加检索到的密钥。
我可笑的尝试:
- name: Adding keys to authorized_keys authorized_key: user=belminf key="{{ item }}" path=/home/belminf/test_auth state=present with_items: ssh_keys.results
结果在invalid key specified 。 可以理解,但我没有想法。 任何人?
好的,我对你的剧本做了一些调整,这里是修改后的版本
--- - hosts: 127.0.0.1 connection: local vars: my_users: belminf: "belminf" bob: "tmessins" tasks: - name: Retrieving all keys from GitHub shell: /usr/bin/curl https://github.com/{{ item.value }}.keys 2> /dev/null register: ssh_keys with_dict: my_users - name: Adding keys to authorized_keys authorized_key: user=belminf key="{{ item.stdout }}" path=/home/belminf/test_auth state=present with_items: ssh_keys.results ignore_errors: yes
一些变化注意到:
authorized_key模块上,密钥已更改为item.stdout 。 标准输出是你需要的公钥。 authorized_key模块上,我定义了ignore_errors: yes以便在curl任务无法获取时,恢复playbook的执行,无论是internet还是404找不到(比如tmessins的密钥)。 当然,你可以通过控制什么来定义失败,以便在发生其他错误时仍然失败。 从Ansible 1.9开始, key的值可以是一个url,不需要通过shell模块来curlurl。
例:
- name: Add my SSH key authorized_key: user=jeffwidman key=https://github.com/jeffwidman.keys
现在非常简单:
- name: get github key(s) and update the authorized_keys file authorized_key: user: "{{ username }}" key: "https://github.com/{{ username }}.keys"
有关详细信息,请检查此github angular色