Páginas

Monday, 23 September 2019

Populate a file with the values registered from a variable

Sometimes you need to run a command and redirect the output to a file. You can use the shell module but if you want to follow best practices and avoid the shell module, you can use Jinja templates


 Using a Jinja template

  •     Identify the name of the variable on the remote host that you want to use
              ansible zubi -m setup | grep mounts

              "ansible_mounts": [


  • Create the Jinja template and give it some format

           cat /etc/ansible/templates/df.jinja2
           {{ ansible_mounts | json_query('[].mount') | to_nice_yaml }}


  • Create the playbook that will populate the file. Using the template module
---
- hosts: zubi
  become: yes
  become_user: root
  become_method: sudo
  tasks:

   - name: build filesystem list
     template:
          src: /etc/ansible/templates/df.jinja2
          dest: /var/lsoffs


Result:
[ec2-user@zubi ~]$ cat /var/lsoffs
- /
- /var
- /opt
- /tmp


  

No comments:

Post a Comment