3. Tasks¶
3.1. Modules¶
Cloud modules
Clustering modules
Commands modules
Crypto modules
Database modules
Files modules
Identity modules
Inventory modules
Messaging modules
Monitoring modules
Net Tools modules
Network modules
Notification modules
Packaging modules
Remote Management modules
Source Control modules
Storage modules
System modules
Utilities modules
Web Infrastructure modules
Windows modules
3.2. Install Packages¶
3.2.1. Single Package¶
- name: install ntpdate
package:
name: ntpdate
state: present
3.2.2. Install Multiple Packages¶
- name: install the latest version of Apache and MariaDB
package:
name:
- httpd
- mariadb-server
state: latest
3.3. Running Commands¶
3.3.1. Copy¶
- name: Copy ansible inventory file to client
copy: src=/etc/ansible/hosts dest=/etc/ansible/hosts owner=root group=root mode=0644
- name: Copy ansible inventory file to client
copy:
src: /etc/ansible/hosts
dest: /etc/ansible/hosts
owner: root
group: root
mode: 0644
Code 3.17. Variables can be used in action lines. Suppose you defined a variable called vhost in the vars section¶
- name: create a virtual host file for {{ vhost }}
template:
src: myfile.j2
dest: /etc/httpd/conf.d/{{ vhost }}
owner: root
group: root
mode: 0644
3.3.2. Shell¶
- name: show date
shell: /bin/date
- name: run this command and ignore the result
shell: /usr/bin/somecommand || /bin/true
- name: run this command and ignore the result
shell: /usr/bin/somecommand
ignore_errors: True
3.3.3. Iptables¶
- name: allow access from 10.0.0.1
iptables:
chain: INPUT
jump: ACCEPT
source: 10.0.0.1
3.4. Order¶
inventory
- The default. The order is ‘as provided’ by the inventoryreverse_inventory
- As the name implies, this reverses the order ‘as provided’ by the inventorysorted
- Hosts are alphabetically sorted by namereverse_sorted
- Hosts are sorted by name in reverse alphabetical ordershuffle
- Hosts are randomly ordered each run
- hosts: all
order: sorted
gather_facts: False
tasks:
- debug:
var: inventory_hostname