0

我在清单文件中提到了主机名

[PROD_NEW]
PROD_NEW-31 ansible_ssh_host=xx.xx.xx.xx ansible_ssh_pass=abcdefg ansible_ssh_user=root somename=PROD_NEW31
PROD_NEW-32 ansible_ssh_host=xx.xx.xx.xx ansible_ssh_pass=abcdefg ansible_ssh_user=root somename=PROD_NEW32

在角色模板目录中,我有一个下面的脚本

{{ansible_ssh_host}}_tag.sh

export SERVER_TAG={{somename}}

如何使用脚本文件名 {{ansible_ssh_host}} 将此导出变量推送到我的清单中的所有主机

我在下面回答但收到错误

The error appears to have been in '/opt/SP/users/tooladm/GSS/Pre-Checks/pushtags/tasks/main.yml': line 6, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:
       src: script_tag.sh.j2
       dest: /opt/SP/{{ item }}_tag.sh
      ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

exception type: <class 'yaml.parser.ParserError'>
exception: while parsing a block mapping
  in "<unicode string>", line 3, column 6
did not find expected key
  in "<unicode string>", line 6, column 7
4

1 回答 1

0

你的模板在templates/script_tag.sh.j2

export SERVER_TAG={{ somename }}

你的剧本

---
- hosts: PROD_NEW
  tasks:
    - name: push my template file to hosts in play
      template:
        src: script_tag.sh.j2
        dest: "/some/path/on/host/{{ ansible_ssh_host }}_tag.sh"

然后只需使用您的库存运行您的剧本:

ansible-playbook -i your_inventory.ini your_playbook.yml
于 2020-06-30T10:48:07.537 回答