Ansible在循环中从许多本地运行一个命令

时间:2016-06-19 13:34:33

标签: amazon-web-services ansible ansible-playbook ansible-2.x

背景,

我试图创建一个循环来迭代来自qa.yml文件的哈希读取,并且对于列表中的每个用户,一旦找到该文件,它就会尝试在本地服务器(公钥)上查找文件它在远程计算机上创建用户并将公钥复制到远程计算机上的authorized_key。

我试图以迭代的方式实现它,因此为了更新密钥或添加更多用户密钥,我需要更改.yml列表并将公钥文件放在适当的位置。但是我无法让local_action +找到工作。

---
- hosts: tag_Ansible_CLOUD_QA

  vars_files:
    - ../users/qa.yml
    - ../users/groups.yml
  remote_user: ec2-user
  sudo: yes

  tasks:

  - name: Create groups
    group: name="{{ item.key }}" state=present
    with_dict: "{{ user_groups }}"

  - name: Create remote users QA
    user: name="{{ item.key }}" comment="user" group=users groups="qa"
    with_dict: "{{ qa_users }}"

  - name: Erase previous authorized keys QA
    shell: rm -rf /home/"{{ item.key }}"/.ssh/authorized_keys
    with_dict: "{{ qa_users }}"

  - name: Add public keys to remote users QA
    local_action:
      find: paths="{{'/opt/pubkeys/2016/q2/'}}" patterns="{{ item.key }}"
      register: result
    authorized_key: user="{{ item.key }}" key="{{ lookup('file', result) }}"
    with_dict: "{{ qa_users }}"

哈希:

qa_users:
  user1:
    name: User 1
  user2:
    name: User 2

2 个答案:

答案 0 :(得分:1)

您在最后一项任务中将两个任务塞进一个任务项目中,因此Ansible不会喜欢这样。

正确拆分任务应该有效:

  - name: Find keys
    local_action: find paths="{{'/opt/pubkeys/2016/q2/'}}" patterns="{{ item.key }}"
    register: result
    with_dict: "{{ qa_users }}"

  - name: Add public keys to remote users QA
    authorized_key: user="{{ item.0.key }}" key="{{ lookup('file', item.1.stdout) }}"
    with_together:
      - "{{ qa_users }}"
      - result

然后第二个任务使用with_together循环遍历字典和上一个任务的结果,该循环在步骤中前进两个数据结构。

然而,这看起来不太理想,无法解决您的问题。

如果你看看你在这里尝试做什么,你可以用这样的东西更简单地替换它:

  - name: Add public keys to remote users QA
    authorized_key: user="{{ item.key }}" key="{{ lookup('file', '/opt/pubkeys/2016/q2/' + item.key ) }}"
    with_dict:
      - "{{ qa_users }}"

您还可以通过简单地使用authorized_keys模块的exclusive参数来删除您清除用户以前密钥的任务:

  - name: Add public keys to remote users QA
    authorized_key: user="{{ item.key }}" key="{{ lookup('file', '/opt/pubkeys/2016/q2/' + item.key ) }}" exclusive=yes
    with_dict:
      - "{{ qa_users }}"

此外,您可能会尝试以奇怪的方式简化问题,但您正在使用的数据结构现在不太理想,所以如果那样,我会看一下&# 39;真的是他们的样子。

答案 1 :(得分:-2)

感谢@ydaetskcoR分享正确的方法,以下解决方案为我做了动态公钥分发,当文件驻留在本地计算机上并在远程EC2计算机上配置时:

---
- hosts: tag_Ansible_CLOUD_QA

  vars_files:
    - ../users/groups.yml
    - ../users/qa.yml
  remote_user: ec2-user
  become: yes
  become_method: sudo

  tasks:
  - name: Find user matching key files
    become_user: jenkins
    local_action: find paths="{{'/opt/pubkeys/2016/q1/'}}" patterns="{{ '*' + item.key + '*' }}"
    register: pub_key_files
    with_dict: "{{ qa_users }}"

  - name: Create groups
    group: name="{{ item.key }}" state=present
    with_dict: "{{ user_groups }}"

  - name: Allow test users to have passwordless sudo
    lineinfile: "dest=/etc/sudoers state=present regexp='^%{{ item.key }} ALL=.*ALL.* NOPASSWD: ALL' line='%{{ item.key }} ALL=(ALL) NOPASSWD: ALL'"
    with_dict: "{{ user_groups }}"

  - name: Create remote users qa
    user: name="{{ item.key }}" comment="user" group=users groups="qa"
    with_dict: "{{ qa_users }}"

  - name: Add public keys to remote users qa
    #debug: "msg={{ 'User:' + item.item.key + '  KeyFile:' + item.files.0.path }}"
    authorized_key: user="{{ item.item.key }}" key="{{ lookup('file', item.files.0.path) }}" exclusive=yes
    with_items: "{{ pub_key_files.results }}"

这是根据EC2标签获取动态广告资源的命令行:

ansible-playbook -i inventory/ec2.py --private-key <path to your key file> --extra-vars '{"QUATER":"q1"}' credentials/distribute-public-key.yml