0%

Ansible-part3: how to use modules - yum apt package

packaging module: yum

yum is CentOS Linux package management tool.

1
2
3
4
5
6
7
8
9
tasks:
- name: upgrade all packages
yum:
name: '*' # update all packages
state: latest
- name: install a package
yum:
name: git
state: latest

output is:

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

PLAY [test packageing modules] **************************************************

TASK [upgrade all packages] *****************************************************
changed: [cube4200]

TASK [install a package] ********************************************************
changed: [cube4200]

PLAY RECAP **********************************************************************
cube4200 : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[anna@cube4200 ~]$ which git
/usr/bin/git
[anna@cube4200 ~]$ git --version
git version 1.8.3.1

Notice: the update task took long time to update all packages.

packaging module: apt

apt is Ubuntu package management tool.

In this example, I am going to install git in two different linux, one use yum on CentOS, anther one use apt on Debian. So I need to add some condition to let ansible know which node should use yum or apt.
Before running site.yml I can use ansible web1 -m gather_facts |grep distribution to see the OS of nodes.

1
2
3
4
5
6
7
8
9
10
11
tasks:
- name: test yum module
yum:
name: git
state: latest
when: ansible_facts['distribution'] == "CentOS" # this is from gather_facts
- name: test apt module
apt:
name: git
state: latest
when: ansible_facts['distribution'] == "Debian"

During running, I can see ansible will judge which command (apt or yum) to use.

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/packaging_modules/inventory$ ansible-playbook site.yml 

PLAY [test packageing modules] **************************************************

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

TASK [test yum module] **********************************************************
skipping: [ansible-node1]
changed: [cube4200]

TASK [test apt module] **********************************************************
skipping: [cube4200]
ok: [ansible-node1]

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

cube4200 uses CentOS, so when testing apt command, it is skipped; ansible_node1 uses Debian, which is skipped when testing yum.

packaging module: package

package module is more general and simplify the installing packages

1
2
3
4
5
tasks:
- name: test package module
package:
name: git
state: latest

Notice: sometime package will have different name in different OS, under this situation, package module will not work, I still need to use yum or apt with gather_facts conditon to install packages.