0%

Ansible part4: how to use modules - net tools get url

If I am going to download an python archived file from python.org, I need to use net tools and file module to achieve my goal.

Net tools module: get_url

playbook:

1
2
3
4
5
6
7
8
9
tasks:
- name: mkdir
file:
path: /tmp/python-download/ # use file module to create directory name /tmp/python-download
state: directory
- name: get url
get_url:
url: https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tgz # the download url
dest: /tmp/python-download/

output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
anna@ansible-controller:~/Desktop/ansible-code/inventory/modules/net-tools-modules$ ansible-playbook site.yml 

PLAY [test net tool get_url] ****************************************************

TASK [Gathering Facts] **********************************************************
ok: [ansible-node1]
ok: [cube4200]

TASK [mkdir] ********************************************************************
changed: [ansible-node1]
changed: [cube4200]

TASK [get url] ******************************************************************
changed: [ansible-node1]
changed: [cube4200]

PLAY RECAP **********************************************************************
ansible-node1 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
cube4200 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

we can check if the python file has been downloaded.

1
2
3
4
5
anna@ansible-controller:~/Desktop/ansible-code/inventory/modules/net-tools-modules$ ansible all -m shell -a "ls /tmp/python-download"
ansible-node1 | CHANGED | rc=0 >>
Python-3.8.3.tgz
cube4200 | CHANGED | rc=0 >>
Python-3.8.3.tgz

It shows Python-3.8.3.tgz already downloaded.

file modules: unarchive

In order to unarchive Python-3.8.3.tgz, I need to use unarchive module

1
2
3
4
5
- name: unarchive compress file
unarchive:
src: /tmp/python-download/Python-3.8.3.tgz
dest: /etc/tmp
remote_src: yes #indicate the archived file is already on the remote system and not local to the Ansible controller.

Notice: remote_src default is no, which means ansible is going to find Python-3.8.3.tgz at my local machine, but actually this file already is in remote, so I need to switch to yes.

output is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
anna@ansible-controller:~/Desktop/ansible-code/inventory/modules/net-tools-modules$ ansible-playbook site.yml 

TASK [unarchive compress file] *****************************************************************************************************************************************************************************************************************
changed: [cube4200]
changed: [ansible-node1]

PLAY RECAP *************************************************************************************************************************************************************************************************************************************
ansible-node1 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
cube4200 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

anna@ansible-controller:~/Desktop/ansible-code/inventory/modules/net-tools-modules$ ansible all -m shell -a "ls /etc/tmp"
cube4200 | CHANGED | rc=0 >>
Python-3.8.3
ansible-node1 | CHANGED | rc=0 >>
Python-3.8.3