0

I've written some custom facts which I reference to as values for a module in a task . When I have 1 custom fact I don't have a problem running the task , but the question is , what do I do when I have an unknown number of facts that I want to run the task for each one of them ? I have the following facts:

"ansible_facts": {
    "ansible_local": {
        "facts": {
            "finance": {
                "files": {
                    "file1": {
                        "dest": "/tmp/dir1",
                        "path": "/etc/finance/file1"
                    },
                    "file2": {
                        "dest": "/tmp/dir2",
                        "path": "/etc/finance/file2"
                    }
                }
            }
        }
    }
},

This is my task's code:

- name: Copy files
  copy:
    src: "/tmp/file1/{{ ansible_local.facts.finance.files.file1.path }}"
    dest: "/ghostcache/{{ ansible_local.facts.finance.files.file1.dest }}/"
    remote_src: yes

How do I iterate over all the items under "files" and take both values (dest and path) and place them as values to the copy module given that I have an unknown number of items under files ? I may have only 1 , i.e file1 or may have file1...fileN .

Thank you !

John Doe
  • 495
  • 1
  • 6
  • 12

1 Answers1

2

Is this the code that you are looking for?

- hosts: localhost                                                                           
  become: yes                                                                                
  become_method: sudo                                                                        
  become_user: root                                                                          
  vars:                                                                                      
    facts:                                                                                     
      finance:                                                                           
        files:                                                                               
          - file1:                                                                           
              dest: "/tmp/dir1"                                                              
              path: "/etc/finance/file1"                                               
          - file2:                                                                           
              dest: "/tmp/dir2"                                                              
              path: "/etc/finance/file2"                                               
  tasks:                                                                                     
    - debug:                                                                                 
        msg: "src: {{ item.value.path}} dest: {{ item.value.dest }}"                         
      with_dict: "{{ facts.finance.files }}"                                               

.

ansible-playbook list-dict.yml 
ok: [localhost] => (item=None) => {
"msg": "src: /etc/finance/file1 dest: /tmp/dir1" }
ok: [localhost] => (item=None) => {
"msg": "src: /etc/finance/file2 dest: /tmp/dir2" }
Vladimir Botka
  • 5,138
  • 8
  • 20