Ansible 常用模块

Utilities

fail

必须配合when语句,当匹配到when条件时则退出并打印msg中指定的信息

1
2
3
4
5
6
7
8
- hosts: localhost
vars:
- location: xxx
- service: xxx
tasks:
- name: determine if variable is defined
fail: msg="The variable location or service is not defined"
when: location == "xxx" or service == "xxx"

debug

fail模块类似,但是仅打印信息而不会退出程序。一般与when语句结合使用,打印出msg参数中指定的信息(var参数可指定一个变量)

1
2
3
4
5
6
7
8
- hosts: localhost
vars:
- location: xxx
- service: xxx
tasks:
- name: determine if variable is defined
debug: msg="The variable location or service is not defined"
when: location == "xxx" or service == "xxx"

Packaging

apt-key:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
- name: Add apt-keys
apt_key:
id: "{{ item.hash_id }}"
keyserver: "{{ item.keyserver | default(omit) }}"
data: "{{ item.data | default(omit) }}"
url: "{{ item.url | default(omit) }}"
state: "present"
register: add_keys
until: add_keys|success
ignore_errors: True
retries: 5
delay: 2
with_items: gpg_keys
vars:
gpg_keys:
- key_name: 'ceph'
keyserver: 'hkp://keyserver.ubuntu.com:80'
hash_id: '0xe84ac2c0460f3994'
- name: Remove revoked apt-keys
apt_key:
id: "17ED316D"
state: "absent"
register: revoke_keys

apt_repository

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- name: Add repo(s)
apt_repository:
repo: "{{ ceph_apt_repo.repo }}"
state: "{{ ceph_apt_repo.state }}"
register: add_repos
until: add_repos|success
retries: 5
delay: 2
tags:
- ceph-repos
vars:
ceph_apt_repo:
repo: "deb http://download.ceph.com/debian-/ {{ ansible_lsb.codename }} main"
state: "present"

yum_repository

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# ansible-playbook add_repo.yaml
# cat /etc/yum.repos.d/epel.repo
[epel]
baseurl = https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
name = EPEL YUM repo

# cat /etc/yum.repos.d/external_repos.repo
[epel]
baseurl = https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
gpgcheck = 0
name = EPEL YUM repo

[rpmforge]
baseurl = http://apt.sw.be/redhat/el7/en/$basearch/rpmforge
enabled = 0
mirrorlist = http://mirrorlist.repoforge.org/el7/mirrors-rpmforge
name = RPMforge YUM repo

# ansible-playbook rm_repo.yaml
# cat /etc/yum.repos.d/external_repos.repo
[rpmforge]
baseurl = http://apt.sw.be/redhat/el7/en/$basearch/rpmforge
enabled = 0
mirrorlist = http://mirrorlist.repoforge.org/el7/mirrors-rpmforge
name = RPMforge YUM repo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
- hosts: localhost
tasks:
- name: Add repository
yum_repository:
name: epel
description: EPEL YUM repo
baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/

- name: Add multiple repositories into the same file (1/2)
yum_repository:
name: epel
description: EPEL YUM repo
file: external_repos
baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
gpgcheck: no

- name: Add multiple repositories into the same file (2/2)
yum_repository:
name: rpmforge
description: RPMforge YUM repo
file: external_repos
baseurl: http://apt.sw.be/redhat/el7/en/$basearch/rpmforge
mirrorlist: http://mirrorlist.repoforge.org/el7/mirrors-rpmforge
enabled: no
1
2
3
4
5
6
7
8
9
10
11
12
- hosts: localhost
tasks:
- name: Remove repository
yum_repository:
name: epel
state: absent

- name: Remove repository from a specific repo file
yum_repository:
name: epel
file: external_repos
state: absent

yum

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- name: install the latest version of Apache
yum:
name: httpd
state: latest

- name: remove the Apache package
yum:
name: httpd
state: absent

- name: install one specific version of Apache
yum:
name: httpd-2.2.15-60.el6.centos.6
state: present

- name: upgrade all packages, excluding kernel & foo related packages
yum:
name: '*'
state: latest
exclude: kernel*,foo*
坚持原创技术分享,您的支持将鼓励我继续创作!
0%