Difference between revisions of "Ansible File Commands"
Jump to navigation
Jump to search
(12 intermediate revisions by the same user not shown) | |||
Line 18: | Line 18: | ||
- name: change permission on remote | - name: change permission on remote | ||
hosts: all | hosts: all | ||
tasks: | tasks: | ||
- name: change permission | - name: change permission | ||
Line 26: | Line 26: | ||
group: root | group: root | ||
mode: 0666 | mode: 0666 | ||
=Run Shell Script on remote server= | |||
--- | |||
- name: run shell script | |||
hosts: all | |||
tasks: | |||
- name: run shell script | |||
shell: "~/cfile.sh" | |||
=Create a Cron Job= | |||
--- | |||
- name: Create a cron job | |||
hosts: server2 | |||
tasks: | |||
- name: scedule cron | |||
cron: | |||
name: job secudeled by ansible | |||
minute: "0" | |||
hour: "10" | |||
day: "*" | |||
month: "*" | |||
weekday: "4" | |||
user: root | |||
job: "~/cfile.sh" | |||
=Download Package from URL= | |||
--- | |||
- name: Download file from the internet | |||
hosts: all | |||
tasks: | |||
- name: create a directory to download the file | |||
file: | |||
path: ~/pdf | |||
state: directory | |||
mode: 0755 | |||
owner: robert | |||
group: robert | |||
- name: Download the file from the url | |||
get_url: https:www.web.com/one.pdf | |||
dest: ~/pdf | |||
mode: 0755 | |||
group: robert | |||
owner: robert | |||
=Find and Kill Running Process= | |||
<pre> | |||
--- | |||
- name: find a process and kill it | |||
hosts: server1 | |||
tasks: | |||
- name: get running process from remote host | |||
ignore_errors: yes | |||
shell: "ps -few | grep top | awk '{print $2}'" | |||
register: running_process | |||
- name: Kill running processes | |||
ignore_errors: yes | |||
shell: "kill {{ item }}" | |||
with_items: "{{ running_process.stdout_lines }}" | |||
</pre> | |||
=Pick and choose steps= | |||
'''Start a playbook at a specific task - use the above as example''' | |||
ansible-playbook playbookname.yml --start-at-task 'kill running process' --private-key ~/.ssh/keyname | |||
=[[Ansible| Ansible Menu]]= | |||
[[Category:Ansible]] |
Latest revision as of 14:54, 24 June 2022
Copy File to remote servers
--- - name: copy file from local to remote hosts: all tasks: - name: copy file copy: src: ~/photo.jpg dest: ~/ owner: root group: root mode: 0777
Change Permission of file on remote server
--- - name: change permission on remote hosts: all tasks: - name: change permission file: path: ~/photo.jpg owner: root group: root mode: 0666
Run Shell Script on remote server
--- - name: run shell script hosts: all tasks: - name: run shell script shell: "~/cfile.sh"
Create a Cron Job
--- - name: Create a cron job hosts: server2 tasks: - name: scedule cron cron: name: job secudeled by ansible minute: "0" hour: "10" day: "*" month: "*" weekday: "4" user: root job: "~/cfile.sh"
Download Package from URL
--- - name: Download file from the internet hosts: all tasks: - name: create a directory to download the file file: path: ~/pdf state: directory mode: 0755 owner: robert group: robert - name: Download the file from the url get_url: https:www.web.com/one.pdf dest: ~/pdf mode: 0755 group: robert owner: robert
Find and Kill Running Process
--- - name: find a process and kill it hosts: server1 tasks: - name: get running process from remote host ignore_errors: yes shell: "ps -few | grep top | awk '{print $2}'" register: running_process - name: Kill running processes ignore_errors: yes shell: "kill {{ item }}" with_items: "{{ running_process.stdout_lines }}"
Pick and choose steps
Start a playbook at a specific task - use the above as example
ansible-playbook playbookname.yml --start-at-task 'kill running process' --private-key ~/.ssh/keyname