【问题标题】:Need help looping through Ansible msg output需要帮助循环通过 Ansible msg 输出
【发布时间】:2021-08-28 15:29:44
【问题描述】:

我需要帮助在 ansible 中完成此任务的输出,并且只为找到“{{ item }}”拉出路径。

尝试了多种方法,但收效甚微。仍在学习 ansible,在这种情况下,我正在尝试根据我的库存文件的输出创建一个新目录。 (现在是 tail 命令,因为我只需要在 10 个条目上测试它)

剧本: '''

  - name: Get the inventory file printout.
    command: tail "{{ build_dir }}{{ inventory_file }}"
    register: command_output

  - debug:
      msg: "{{ command_output.stdout_lines }}"

  - name : Find the RPMs in "{{ build_dir }}"
    find:
      paths: "{{ build_dir }}"
      patterns: "{{ item }}.rpm"
      recurse: yes
    with_items:
      - "{{ command_output.stdout_lines }}"
    register: found_pkgs

  - name: Just the Path for each Found Pkgs.
    debug:
      msg: "{{ item }}"
    loop:
      - "{{ found_pkgs }}"

输出:

"msg": {                                                                                                           
    "changed": false,                                                                                              
    "msg": "All items completed",                                                                                  
    "results": [                                                                                                   
        {                                                                                                          
            "ansible_loop_var": "item",                                                                            
            "changed": false,                                                                                      
            "examined": 15029,                                                                                     
            "failed": false,                                                                                       
            "files": [                                                                                             
                {                                                                                                  
                    "atime": 1629933434.2974539,                                                                   
                    "ctime": 1629814445.3359122,                                                                   
                    "dev": 64773,                                                                                  
                    "gid": 70000,                                                                                  
                    "gr_name": "engineering",                                                                      
                    "inode": 469762133,                                                                            
                    "isblk": false,                                                                                
                    "ischr": false,                                                                                
                    "isdir": false,                                                                                
                    "isfifo": false,                                                                               
                    "isgid": false,                                                                                
                    "islnk": false,                                                                                
                    "isreg": true,                                                                                 
                    "issock": false,                                                                               
                    "isuid": false,                                                                                
                    "mode": "0644",                                                                                
                    "mtime": 1629814445.3359122,                                                                   
                    "nlink": 1,                                                                                    
                    "path": "<REDACTED>/newISO/repos/zeek/packages/zeek-btest-4.0.2-1.1.x86_64.rpm",

【问题讨论】:

    标签: ansible


    【解决方案1】:

    您可以使用json_query filterresults[] 中提取每个文件的路径。一个set_fact 任务应该做如下:

        - name: save the files path in file_names var
          set_fact:
            file_paths: "{{ found_pkgs | json_query('results[].files[].path') }}"
    
        - name: show file names
          debug:
            var: file_paths
    

    【讨论】:

      猜你喜欢
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多