Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables Ansible:操纵变量名_Variables_Ansible - Fatal编程技术网

Variables Ansible:操纵变量名

Variables Ansible:操纵变量名,variables,ansible,Variables,Ansible,我正在使用hosts\u vars文件中包含的变量列表(在to\u install下)为我的deploy.yml剧本提供素材。 我的主机\u变量: --- clients: cl1: to_install: - banana 8.1 - Firefox 46.0.1 to_uninstall: - null 我的剧本: - name: Deploy hosts: win_clones vars_files: - ./hosts_var

我正在使用
hosts\u vars
文件中包含的变量列表(在
to\u install
下)为我的
deploy.yml
剧本提供素材。 我的
主机\u变量

---
clients:
  cl1:
    to_install:
    - banana 8.1
    - Firefox 46.0.1
    to_uninstall:
    - null
我的剧本:

- name: Deploy
  hosts: win_clones
  vars_files:
    - ./hosts_vars
  tasks:
    - name: Fetching and copying the file on the client ...
      win_get_url:
        url: 'ftp://172.20.0.5/choco-repo/{{ item }}'
        dest: 'C:\Test\{{ item }}'
      with_items: "{{ clients[machine].to_install }}"
    - name: Installing the package ...
      win_chocolatey:
        name: "{{ item }}"
        state: present
        with_items: "{{ clients[machine].to_install }}"
我使用以下命令运行此播放:

ansible-playbook deploy.yml -e machine=cl1
对于我的剧本的第一个任务,我使用
win\u get\u url
,我想将我的变量名称转换为:

banana8.1.nupkg
Firefox46.0.1.nupkg
对于第二个任务,我使用
win_chocolate
,只传递变量的名称就足够了(没有版本号):

我想魔法应该发生在
带有{u项:{{clients[machine].{u install}}“
。如何在ansible中执行此操作?

您可以使用regex\u replace来执行此操作

这:

将打印:

TASK [debug] *******************************************************************
ok: [win_clones] => (item=banana 8.1) => {
    "item": "banana 8.1", 
    "msg": "banana8.1.nupkg"
}
ok: [win_clones] => (item=Firefox 46.0.1) => {
    "item": "Firefox 46.0.1", 
    "msg": "Firefox46.0.1.nupkg"
}

您可以使用regex_replace来实现这一点

这:

将打印:

TASK [debug] *******************************************************************
ok: [win_clones] => (item=banana 8.1) => {
    "item": "banana 8.1", 
    "msg": "banana8.1.nupkg"
}
ok: [win_clones] => (item=Firefox 46.0.1) => {
    "item": "Firefox 46.0.1", 
    "msg": "Firefox46.0.1.nupkg"
}

谢谢,这适用于第一个任务。我将如何做第二个,即删除版本号以仅包含名称?与第一个完全相同,通过使用regex_replace。如果你没什么事可做,那就没意思了,对吧?如果你找不到解决方案,我会帮助你,但我认为你可以解决这个问题。好吧,它只需要使用
debug:msg=“{{item | regex_replace(''',)}}”
就可以工作,但这似乎有点奇怪。这难道不只是删除空间吗?所以我应该得到banana8.1和Firefox46.0.1是的,这真的很奇怪,你应该得到banana8.1。不,我得到的是
“msg”:“banana8.1”
“msg”:“Firefox46.0.1”
,正如预期的那样(ansible 2.1.0.0)谢谢,这对第一个任务有效。我将如何做第二个,即删除版本号以仅包含名称?与第一个完全相同,通过使用regex_replace。如果你没什么事可做,那就没意思了,对吧?如果你找不到解决方案,我会帮助你,但我认为你可以解决这个问题。好吧,它只需要使用
debug:msg=“{{item | regex_replace(''',)}}”
就可以工作,但这似乎有点奇怪。这难道不只是删除空间吗?所以我应该得到banana8.1和Firefox46.0.1是的,这真的很奇怪,你应该得到banana8.1。不,我得到的是
“msg”:“banana8.1”
“msg”:“Firefox46.0.1”
,正如预期的那样(ansible 2.1.0.0)
TASK [debug] *******************************************************************
ok: [win_clones] => (item=banana 8.1) => {
    "item": "banana 8.1", 
    "msg": "banana8.1.nupkg"
}
ok: [win_clones] => (item=Firefox 46.0.1) => {
    "item": "Firefox 46.0.1", 
    "msg": "Firefox46.0.1.nupkg"
}