Skip to content

Commit

Permalink
added file create remote
Browse files Browse the repository at this point in the history
  • Loading branch information
jfcgsilva committed Aug 30, 2023
1 parent b4751e6 commit e0c4082
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
44 changes: 43 additions & 1 deletion 07-file-deployment/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# File Deployment

## Modules stat and file

Several modules can be used to depoly file, depending on the objectives.
| module | Usage |
| ------ | ----- |
Expand All @@ -14,6 +16,7 @@ Several modules can be used to depoly file, depending on the objectives.
| stat | gets stats on file |

### stat - manage file attributes

stat modules gets files status and is mainly used to get infofrom the file and is used in tandem with the register and when keywords in ansible

```yaml
Expand All @@ -31,7 +34,9 @@ stat modules gets files status and is mainly used to get infofrom the file and i
when:
content.stat.exists
```
you can also fail controlling the flow
```yaml
---
- name: playbook demonstrating stat usage and controling the flow
Expand All @@ -54,7 +59,9 @@ you can also fail controlling the flow
```
### file
file module is used to change file attributes, works great with stat module to conditionally change something
file module is used to change file attributes, works great with stat module to conditionally change something. You can also use it to create files and dirs, or remove them.
```yaml
---
- name: playbook demonstrating stat and file module
Expand All @@ -79,9 +86,16 @@ file module is used to change file attributes, works great with stat module to c
when:
content.stat.pw_name != "root" and content.stat.mode != "0600"
```
* creating and removing files
## File contents
### lineinfile
using regex to change a line in some file
```yaml
---
- name: lineinfile usage example
Expand Down Expand Up @@ -111,3 +125,31 @@ using regex to change a line in some file
name: NetWorkManager
state: restarted
```
### blockinfile
blockinfile inserts a block delimited by markers. These markers can be changed with *marker*
Also, using the "|" char after block appends the newline char at de end of the line. Using ">" would concatenate the different lines in a single one in the resulting file.
```yaml
---
- name: lineinblock usage example
hosts: localhost
vars_files:
vars/lineinblock
tasks:
- name: copy /etc/hosts
copy:
src: /ect/hosts
dest: /home/{{ user }}/hosts
- name: managing /etc/hosts copy example
# don't forget to add the pipe and permissions are {{ content.stat .permissions }}
blockinfile:
path: /home/{{ user }}/hosts
block: |
192.168.0.23 host1.rhce-ex194.com
192.168.0.24 host2.rhce-ex194.com
192.168.0.25 host3.rhce.ex194.com
state: present
```
16 changes: 16 additions & 0 deletions 07-file-deployment/blockinfile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
- name: lineinblock usage example
hosts: localhost
vars_files:
vars/lineinblock
tasks:
- name: managing /etc/hosts copy example
# don't forget to add the pipe and permissions are {{ content.stat .permissions }}
blockinfile:
path: /home/{{ user }}/hosts
block: |
192.168.0.23 host1.rhce-ex194.com
192.168.0.24 host2.rhce-ex194.com
192.168.0.25 host3.rhce.ex194.com
state: present

20 changes: 20 additions & 0 deletions 07-file-deployment/file-create-remove.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
- name: using the file module
hosts: localhost
vars:
fullpath: /tmp/newdir/newsubdir
vars_files:
vars/lineinblock
tasks:
- name: create dir and subdir
file:
path: "{{ fullpath }}"
state: directory #could be file, directory, touch or absent
mode: 0770
owner: "{{ user }}"
group: "{{ user }}"
recurse: true # needed when state is set to directory
- name: create a file
file:
path: "{{ fullpath }}/newfile"
state: touch
6 changes: 6 additions & 0 deletions 07-file-deployment/vars/lineinblock
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
$ANSIBLE_VAULT;1.1;AES256
30656164363831323231333437393338366539326264306264653963653032623631623533656239
3463616262333632323139353131653962313962626336360a363931623265663534613736336361
33653435343933386561366639353161633533643938323164346363313439383831323334303933
3966366330383139350a343362356165313732623165373132376237393339333636646363346632
33636331396266333034386535616437363561346232386165373236373438643465

0 comments on commit e0c4082

Please sign in to comment.