【问题标题】:Ansible: How to run command with shell outputAnsible:如何使用 shell 输出运行命令
【发布时间】:2019-07-11 06:21:31
【问题描述】:

不言自明。我想基于 $(which {{ item }}) 链接。

已经看到了 register 函数,但是因为我需要做一个嵌套循环,所以我不知道如何使用它。

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

【问题讨论】:

    标签: shell ansible command


    【解决方案1】:

    下面是剧情。避免使用命令模块进行链接。使用带有 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 }}"
    

    【讨论】:

    • 非常感谢!现在我对注册有了更多的了解;)
    猜你喜欢
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多