Ansible:如何在shell输出中运行命令

时间:2019-07-11 06:21:31

标签: shell ansible command

自我解释。我想基于$(其中{{item}})进行链接。

已经看到了寄存器功能,但是由于需要执行嵌套循环,所以我不确定如何使用它。

name: Link bins to user path
  command: 'ln -s \$(which {{ item.1 }}) /home/{{ item.0 }}/bin/{{ item.1 }}'
  with_nested: 
     - "{{ jail_users }}"
     - "{{ jail_user_commands }}

输出:

 failed: [rousertest] (item=[u'bob', u'date']) => {"changed": true,
 "cmd": ["ln", "-s", "$(which", "date)", "/home/bob/bin/date"], "delta":
 "0:00:00.011825", "end": "2019-07-11 08:17:32.921705", "item": ["bob", "date"], "msg": "non-zero return code", "rc": 1, "start": "2019-07-11
 08:17:32.909880", "stderr": "ln: target ‘/home/bob/bin/date’ is not a 
directory", "stderr_lines": ["ln: target ‘/home/bob/bin/date’ is not a
 directory"], "stdout": "", "stdout_lines": []

我当然期望这样的事情:

sudo ansible server -i inventory -m  shell -a 'echo $(which date)'
rousertest | SUCCESS | rc=0 >>
/usr/bin/date

1 个答案:

答案 0 :(得分:0)

以下是该剧。避免使用命令模块进行链接。将文件模块与state = link一起使用。

- name: Link binary
  hosts: all
  gather_facts: true
  vars:
    files:
      - date
      - ls
    users:
      - user1
      - user2
  tasks:
    - name: Find paths
      command: which {{ item }}
      with_items:
        - "{{ files }}"
      register: result
    - name: Link bins to user path
      file:
        src: "{{ item.1.stdout }}"
        dest: "/home/{{ item.0 }}/bin/{{ item.1.item }}"
        owner: "{{ item.0 }}"
        group: "{{ item.0 }}"
        state: link
      with_nested:
        - "{{ users }}"
        - "{{ result.results }}"