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.2.3. Install HTTPd server¶
In some distributions
apache
is namedhttpd
- name: remove the apache package
package:
name: "{{ apache }}"
state: absent
3.2.4. Tasks¶
- name: "Install Nginx"
become: yes
package:
name: nginx
state: latest
- name: "Update Nginx config"
become: yes
copy:
src: ./conf/nginx-conf
dest: /etc/nginx/nginx.conf
notify:
- restart nginx
- name: "Create sites-available directory"
become: yes
file: path=/etc/nginx/sites-available state=directory
- name: "Create sites-enabled directory"
become: yes
file: path=/etc/nginx/sites-enabled state=directory
- name: "Update Nginx default config"
become: yes
copy:
src: ./conf/nginx-default
dest: /etc/nginx/sites-available/default
notify:
- restart nginx
- name: "Enable Nginx site config"
become: yes
file:
src: /etc/nginx/sites-available/default
dest: /etc/nginx/sites-enabled/default
state: link
notify:
- restart nginx
- name: "Restart nginx"
become: yes
service: name=nginx state=restarted
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