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 !

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" }
  • 1
    You are welcome! I have rejected the approved edit. Approved changes do no work and the playbook crashed with error: "fatal: [localhost]: FAILED! => {"msg": "'facts' is undefined"}". – Vladimir Botka Jun 26 '18 at 15:10
  • 1
    I've changed the code to match the updated question. – Vladimir Botka Jun 26 '18 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.