json Ansible playbook额外变量与循环的杰森字符串格式化[关闭]

vql8enpb  于 2023-05-08  发布在  其他
关注(0)|答案(1)|浏览(152)

**已关闭。**此问题不符合Stack Overflow guidelines。目前不接受答复。

这个问题似乎不是关于在help center定义的范围内编程。
2天前关闭。
Improve this question
我需要从playbook中删除变量,并将它们作为字符串放在ansible-playbook -e选项中。下面的playbook使用playbook中声明的变量。游戏结束时首先使用-e选项,然后能够在Tower中启动playbook,这样变量就可以插入Tower中的额外变量框中。这是我的设置和结果

  • $cat symphony.yml
--- 
- name: Test key value playbook
  hosts: localhost
  vars:
   repos:
     repo1:
       - "samples1"
       - "app1"
       - "git1"
       - "test1"
     repo2:
    - "samples2"
       - "app2"
       - "git2"
       - "test2"
     repo3:
        - "samples1"
        - "app1"
        - "git1"
        - "nonprod1"
  tasks:
  # execute the roles
  tasks:
  - name: loop role
    include_role:
      name: symphonyrole  # This is the name of the role in the roles directory
    vars:
      item_set: "{{ item.value }}"
    loop: "{{ repos | dict2items }}"

当我执行时,这本剧本就起作用了。$ansible-playbook symphony.yml
任务[交响乐角色:set_vars]***************************************************************************************************************************************确定:[本地主机]
任务[交响乐角色:运行Python远程脚本]***************************************************************************************************************************已更改:[本地主机]
播放重播
***************************************************************************************************************************************************本地主机:ok=7已更改=3无法访问=0失败=0已跳过=0已挽救=0已忽略=0
现在我修改了playbook并删除了vars部分,如下所示
$cat symphonye.yml

---

 - name: Test key value playbook
   hosts: localhost

   tasks:

   # execute the roles
   tasks:
   - name: loop role
     include_role:
     name: symphonyrole  # This is the name of the role in the roles directory
     vars:
       item_set: "{{ item.value }}"
     loop: "{{ repos | dict2items }}"

当我使用-e选项中的变量执行playbook时,我得到一个错误。我知道json字符串不包括repo 1,repo 2,repo 3和are missing。如果这些是必需的,我如何将它们合并到json字符串中。当我运行它时,它失败了。
$ ansible-playbook symphonye.yml -e“repos=[{“sample1”,“app1”,“git1”,“test1”},{“sample2”,“app2”,“git2”,“test2”},{“sample3”,“app3”,“git3”,“test3”}]”
ok:[localhost] meta:运行处理程序
TASK [循环角色]*************************************************************************************任务路径:/home/abeadm/ans_dev/abe/movetst/playbooks/test/symphonye.yml:30 fatal:[localhost]:失败!=> {“msg”:“在({{ repos| dict2items }}):dict 2 items需要一个字典,改为获取<type 'unicode'>。”}
Play RECAP
**************************************************************************************************************************本地主机:ok=1已更改=0无法访问=0失败=1已跳过=0已挽救=0已忽略=0
错误出现在“”中:第1行,第72列,但是根据确切的语法问题,也可以在文件中的其它地方。
playbook是否包含所有需要的代码,-e字符串的格式是否正确?
我已经通过谷歌广泛搜索,斌和ddg试图得到这一点。我觉得很接近了只需要稍微调整一下。任何帮助将不胜感激。

vaqhlq81

vaqhlq811#

下面的Python脚本从playbook创建 varsplay 文件。属性 varsplays 中删除。

shell> cat convert.py
#!/usr/bin/python3.9

import argparse
import os
from ruamel.yaml import YAML
yaml = YAML()

argParser = argparse.ArgumentParser()
argParser.add_argument("-p", "--playbook", help="playbook file", required=True)
args = argParser.parse_args()

with open(args.playbook, 'r') as f:
    data = yaml.load(f)

filename, file_extension = os.path.splitext(args.playbook)

for idx, play in enumerate(data):

    idx = idx + 1
    vars = play.pop('vars', {})
    vars_file = filename + '_vars_' + str(idx) + '.yml'
    with open(vars_file, 'w') as f:
        yaml.dump(vars, f)

    play_file = filename + '_play_' + str(idx) + '.yml'
    with open(play_file, 'w') as f:
        yaml.dump([play], f)

例如,给定测试的剧本

shell> cat pb.yml 
- name: Play1
  hosts: all
  vars:
    v1: test
  tasks:
    - debug:
        var: v1

- name: Play2
  hosts: all
  vars:
    v2: test
  tasks:
    - debug:
        var: v2

- name: Play3
  hosts: all
  tasks:
    - debug:
        msg: No vars in this play.

转换行动手册

shell> ./convert.py -p pb.yml

创造

shell> ls -1 pb_*
pb_play_1.yml
pb_play_2.yml
pb_play_3.yml
pb_vars_1.yml
pb_vars_2.yml
pb_vars_3.yml
shell> ls -1 pb_play_* | xargs cat
- name: Play1
  hosts: all
  tasks:
  - debug:
      var: v1

- name: Play2
  hosts: all
  tasks:
  - debug:
      var: v2

- name: Play3
  hosts: all
  tasks:
  - debug:
      msg: No vars in this play.
shell> ls -1 pb_vars_* | xargs cat
v1: test
v2: test
{}

这些剧本可以分开上演

shell> ansible-playbook -e @pb_vars_1.yml pb_play_1.yml

PLAY [Play1] *********************************************************************************

TASK [debug] *********************************************************************************
ok: [localhost] => 
  v1: test

PLAY RECAP ***********************************************************************************
localhost: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

,也可以导入到单个剧本中

shell> cat pb_play_all.yml
- import_playbook: pb_play_1.yml
- import_playbook: pb_play_2.yml
- import_playbook: pb_play_3.yml

给予

shell> ansible-playbook -e @pb_vars_1.yml -e @pb_vars_2.yml -e @pb_vars_3.yml pb_play_all.yml

PLAY [Play1] *********************************************************************************

TASK [debug] *********************************************************************************
ok: [localhost] => 
  v1: test

PLAY [Play2] *********************************************************************************

TASK [debug] *********************************************************************************
ok: [localhost] => 
  v2: test

PLAY [Play3] *********************************************************************************

TASK [debug] *********************************************************************************
ok: [localhost] => 
  msg: No vars in this play.

PLAY RECAP ***********************************************************************************
localhost: ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

警告:如果 * var * 相互覆盖,则不会得到相同的结果。

相关问题