How to read a particular part of file in ansible

前端 未结 1 923
北海茫月
北海茫月 2021-01-16 17:25

I have a file which has the following data

!

multiply 4 and 5

multiply 5 and 6

!

add 3 to 4

<
相关标签:
1条回答
  • 2021-01-16 17:42

    Use with_lines. The play below

    - hosts: localhost
      vars:
        my_data_file: "{{ playbook_dir }}/data.txt"
        my_commands: []
      tasks:
        - set_fact:
            my_commands: "{{ my_commands + [ item ] }}"
          with_lines: "cat {{ my_data_file }}"
          when: item is search('^add')
        - debug:
            var: my_commands
    

    gives

    "my_commands": [
        "add 3 to 4", 
        "add 8 to 4"
    ]
    
    0 讨论(0)
提交回复
热议问题