Ansible Tutorial
we can create a new file with content required in ansible using the copy module.
Need to use content parameter in copy module to create new file with required content.
Lets see the ansible playbook example to create a new file,
- hosts: localhost connection: local gather_facts: no tasks: - name: initialize file path set_fact: file_path: "test/testing2.txt" - name: "Copy {{ file_path }} file" copy: content: "# Testing file creation in ansible." dest: "{{ file_path }}" - name: Print the msg debug: msg: "Copied {{ file_path }} file."
Output:
# ansible-playbook new_file.yml [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [localhost] ************************************************************************************************************************************************************ TASK [initialize file path] ************************************************************************************************************************************************* ok: [localhost] TASK [Copy test/testing2.txt file] ****************************************************************************************************************************************** changed: [localhost] TASK [Print the msg] ******************************************************************************************************************************************************** ok: [localhost] => { "msg": "Copied test/testing2.txt file." } PLAY RECAP ****************************************************************************************************************************************************************** localhost : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Let's check whether new file is created or not,
# cat test/testing2.txt # Testing file creation in ansible.
File is created with content parameter configured.
# ls -all test//testing2.txt -rw-r--r--. 1 root root 35 Nov 13 09:57 test//testing2.txt
Let's mention file permission in the same ansible code,
mode parameter is required to specify the file permission.
- hosts: localhost connection: local gather_facts: no tasks: - name: initialize file path set_fact: file_path: "test/testing2.txt" - name: "Copy {{ file_path }} file" copy: content: "# Testing file creation in ansible." dest: "{{ file_path }}" mode: "0661" - name: Print the msg debug: msg: "Copied {{ file_path }} file."
Output:
# ansible-playbook new_file.yml [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [localhost] ************************************************************************************************************************************************************ TASK [initialize file path] ************************************************************************************************************************************************* ok: [localhost] TASK [Copy test/testing2.txt file] ****************************************************************************************************************************************** changed: [localhost] TASK [Print the msg] ******************************************************************************************************************************************************** ok: [localhost] => { "msg": "Copied test/testing2.txt file." } PLAY RECAP ****************************************************************************************************************************************************************** localhost : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Lets see the file content and file permission configured,
# cat test/testing2.txt # Testing file creation in ansible.
ls -all test//testing2.txt -rw-rw---x. 1 root root 35 Nov 13 10:03 test//testing2.txt
File permission is created as specified read and write to owner and group users, execute only to others.
- hosts: localhost connection: local gather_facts: no tasks: - name: initialize file path set_fact: file_path: "test/testing2.txt" - name: "Copy {{ file_path }} file" copy: content: "# Testing file creation in ansible." dest: "{{ file_path }}" owner: user1 group: user1 mode: "0661" - name: Print the msg debug: msg: "Copied {{ file_path }} file."
Output:
# ansible-playbook new_file.yml [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [localhost] ************************************************************************************************************************************************************ TASK [initialize file path] ************************************************************************************************************************************************* ok: [localhost] TASK [Copy test/testing2.txt file] ****************************************************************************************************************************************** changed: [localhost] TASK [Print the msg] ******************************************************************************************************************************************************** ok: [localhost] => { "msg": "Copied test/testing2.txt file." } PLAY RECAP ****************************************************************************************************************************************************************** localhost : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Lets see the file content and file owner configured,
# cat test/testing2.txt # Testing file creation in ansible.
ls -all test//testing2.txt -rw-rw---x. 1 user1 user1 35 Nov 13 10:03 test//testing2.txt
File permission is created as specified read and write to owner and group users, execute only to others.
Ansible Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page