Skip to content

Instantly share code, notes, and snippets.

@aminkhoshzahmat
Last active Jan 8, 2022
Embed
What would you like to do?
Ansible examples with modules
- name: ----- Create dir1 in /home -----
file:
path: /home/dir1
state: directory
owner: root
group: root
mode: 0755
tags: [create_dir1]
- name: ----- create dir2 in /home ------
file: path=/home/dir2 state=directory owner=root group=root mode=0755
tags: [create_dir2]
- name: ----- create file1 in /home/dir1 -----
file:
path: /home/dir1/file1
state: touch
owner: root
group: root
mode: 644
tags: [create_file]
- name: ----- create file2 in /home/dir2 ------
file:
path: /home/dir2/file2
state: touch
owner: root
group: root
mode: 0644
tags: [create_file]
- set_fact: myvar=123
- debug:
msg: "{{myvar}}"
- name: ------ Show accoutn info ------
debug:
msg: "name: {{account.name}}, age: {{account.age}}"
tags: [show_var]
- name: ----- show user_list ------
debug:
msg: "{{user_list[1]}}"
tags: [show_user_list]
- name: ----- show built-in variables -----
debug:
msg: "{{myvar}}"
tags: show_builtinvars
- debug:
msg: "{{http_port}}"
tags: show_http_port
- name: ----- command module -----
command: 'ps aux'
args:
chdir: /home
creates: /home/dir3
removes: /home/dir1
register: command_out
tags: command_module
- name: ----- show command_out -----
debug:
msg: "{{command_out.stdout}}"
tags: command_module
- name: ----- install expect module -----
expect:
command: passwd devops
responses:
password: "123"
#no_log: true
tags: expect_mod
- name: ------ script module ------
script: myscript.sh
args:
removes: dude
tags: script_module
- name: ----- shell module -----
shell: echo "Shell module" > /home/shell_out
tags: shell_mod
- name: ----- shell module copy files -----
shell: tar czvf test.tar.gz /home/test
tags: shell_mod_cp
- name: ----- example 10 ------
shell: ps aux | grep ansible
register: command_out
tags: exam_10
- name: ----- show result example 10 -----
debug:
msg: "{{command_out.stdout}}"
tags: exam_10
- name: ----- copy module ------
copy:
src: myfiles
dest: /home/dir1/
owner: root
group: root
mode: 0644
force: yes
backup: yes
tags: copy_mod
- name: ----- fetch module -----
fetch:
src: /home/dir1/myfiles
dest: /home/
flat: true
validate_checksum: no
tags: fetch_mod
- name: ----- group module -----
group:
name: ansible
state: present
tags: group_mod
- name: ----- user module -----
user:
name: amin
group: ansible
state: present
shell: /bin/bash
password: 123
tags: user_mod
#- name: ------ exam 12 create group ------
# group:
# name: anisa
# state: present
# tags: exam_12
#- name: ----- exam 12 add group to user -----
# user: devops
# group: anisa
# state: present
# home: /tmp/devops
# tags: exam_12
#- name: ----- exam 12 change the password with expect -----
# expect:
# command: passwd devops
# responses:
# password: "123"
# tags: exam_12
- name: ------ yum repository module ------
yum_repository:
name: epel
description: extra packages
baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
gpgcheck: no
enabled: yes
state: present
tags: [yum_repo_mod]
- name: ----- yum install nginx -----
yum:
name: nginx
state: present
tags: [yum_mod]
- name: ---- yum list -----
yum:
list: ansible
register: yum_out
tags: yum_list
- name: ----- show yum list -----
debug:
msg: "{{yum_out}}"
tags: yum_list
- name: ----- yum list apache -----
yum:
list: httpd
register: yum_httpd_out
tags: yum_httpd
- name: ------ yum list apache show ------
debug:
msg: "{{yum_httpd_out}}"
tags: yum_httpd
- name: ----- yum install apache -----
yum:
name: httpd
state: latest
tags: yum_httpd
#- name: ----- install nginx on debian -----
# apt:
# name: nginx
# state: present
# become: yes
# tags: apt_mod
- name: ----- Template module -----
become: yes
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
backup: yes
tags: template_nginx_mod
- name: ------ lineinefile module -----
lineinfile:
path: /etc/nginx/nginx.conf
insertafter: ' listen 8080 default_server;'
line: ' listen 8081 default_server;'
state: absent
tags: lineinfile_mod
- name: ------ lineinefile module nginx -----
lineinfile:
path: /etc/nginx/nginx.conf
insertafter: '^listen'
line: ' listen 8083 default_server;'
# state: absent
tags: lineinfile_mod_nginx
- name: ----- replace module ------
replace:
path: /etc/nginx/nginx.conf
regexp: '8084'
replace: '8085'
backup: yes
tags: replace_mod
- name: ----- service module ------
service:
name: nginx
state: started
enabled: yes
tags: service_mod
- name: ----- archive module ------
archive:
path: /home/dir1/*
dest: /opt/test.tar.gz
remote_src: yes
exclude_path:
- f2
tags: archive_mod
- name: ----- unarchive module -------
unarchive:
src: test2.tar.g
# read from roles/myproject/files/...
dest: /tmp
exclude:
- f2
tags: unarchive_mod
- name: ----- wait_for module -----
wait_for:
timeout: 10
tags: wait_for_mod
- name: ----- after wait_for -----
debug:
msg: "After wait for"
tags: wait_for_mod
- name: ----- wait for apache to start -----
wait_for:
port: 80
delay: 10
tags: wf_apache
- name: ----- check if apache is up ------
debug:
msg: "apache is up now"
tags: wf_apache
- name: ----- rebote remote host ------
shell: "sleep 1 && reboot"
async: 1
poll: 5
ignore_errors: yes
tags: reboot_hosts
- name: ---- wait_for_connection module ------
wait_for_connection:
timeout: 300
delay: 5
tags: reboot_hosts
- name: ----- after reboot -----
debug:
msg: "Server is rebooted"
tags: reboot_hosts
- name: ----- reboot remote hosts -----
reboot:
reboot_timeout: 500
tags: reboot_mod
- name: ----- install git -----
yum:
name: git
state: present
tags: install_git
- name: ----- git module -----
git:
repo: 'https://github.com/githubtraining/hellogitworld.git'
dest: /opt/hello
tags: git_mod
- name: ----- get_url module (download) -----
get_url:
url: ftp://download.anisa.co.ir/Course/LPIC-2/named.conf
dest: /opt
mode: 0440
timeout: 30
tags: get_url_mod
- name: ----- timezone module ------
timezone:
name: Asia/Tehran
tags: timezone_mod
- name: ----- synchronize module -----
synchronize:
src: /home/dir1
dest: /home/dir2
tags: sync_mod
- name: ----- delegate_to ------
shell: cat /home/test
register: catty
delegate_to: 192.168.0.181
tags: delegate_to_mod
- name:
debug:
msg: "{{catty.stdout_lines}}"
tags: delegate_to_mod
- name: ----- iptables module -----
iptables:
chain: INPUT
source: 192.168.0.181
protocol: tcp
destination_port: 22
jump: DROP
action: insert
state: absent
tags: iptables_mod
- name: ------ install mariadb ------
yum:
name: mariadb-server
state: installed
tags: mariadb
- name: ------ start mariadb ------
service:
name: mariadb
enabled: yes
state: started
tags: mariadb
- name: ----- install mariadb-python ------
yum:
name: MySQL-python
state: installed
tags: mariadb
- name: ----- create a DB ------
mysql_db:
name: mydb
state: present
tags: create_db
- name: ------ dump a db -----
mysql_db:
name: mydb
state: dump
target: /home/mydb_dump.sql
tags: dumpdb
- name: ------ find .txt and .conf file in /home which are older than 2 weeks and greater than 1K ----
find:
path: /home
# age: 2w
# size: 1k
# recurse: yes
file_type: file
pattern: '*.txt'
tags: find_mod
- name: ------ find .txt on local Ansible server -----
local_action:
module: find
path: /home/amin/Downloads
age: -1d
size: -1m
recurse: yes
file_type: file
pattern: '*.txt'
tags: local_action_mod
- set_fact:
package_name: "httpd"
when: ansible_os_family=="RedHat"
tags: cond_1
- set_fact:
package_name: "apache2"
when: ansible_os_family=="Suse"
tags: cond_1
- debug:
msg: "{{package_name}}"
tags: cond1
- name: ---- check if nginx-filesystem is installed ----
shell: rpm -qa | grep nginx
register: query_out
tags: cond2
- name: ----- nginx-filesystem is installed -----
debug:
msg: "nginx-filesystem is installed"
when: "'nginx-filesystem' in query_out.stdout"
tags: cond2
- name: ------ gather rpm packages ------
package_facts:
manager: auto
tags: cond3
- name: ----- check if nginx is installed -----
debug:
var: ansible_facts.packages['nginx']
# var: ansible_facts.packages
tags: cond3
- name: ------ check if nginx is installed ----
debug:
msg: "nginx is already installed"
when: "'nginx' in ansible_facts.packages"
tags: cond3
- name: ----- check if a variable is defined ------
pause:
prompt: "The backup path is undifined! Please enter the path"
register: backup_path2
when: backup_path is not defined
tags: pause_mod
- name: ----- cat /home/test.txt -----
shell: cat /home/test.txt
register: catty
tags: cond4
- name: ----- if /home/test.txt contains Ansible ----
debug:
msg: "test.txt contains the word Ansible"
when: catty.stdout.find("Ansible") != -1
tags: cond4
- name: ----- if var1 or var2 is true -----
shell: echo "var1"
when: var1 or var2 | bool
tags: cond6
- name: ----- if var1 is false -----
debug:
msg: "{{var1 is false}}"
# when: not var1
when: var1
tags: cond7
- name: ----- create a1 and a2 in /home -----
file:
path: /home/{{item}}
state: touch
owner: root
group: root
mode: 0644
with_items:
- a1
- a2
tags: loop_with_items
- name: ----- install nginx and gcc ------
yum:
name: "{{item}}"
state: present
with_items:
- nginx
- gcc
tags: loop2
- name: ----- install nginx and gcc -----
yum:
name: ['nginx', 'gcc']
state: present
tags: loop3
- name: ----- delete a1 and a2 file form /home -----
file:
path: /home/{{item}}
state: absent
loop:
- a1
- a2
tags: loop4
- name: ----- user module -----
user:
name: "{{item}}"
group: ansible
state: absent
shell: /bin/bash
password: 123
loop:
- user1
- user2
- user3
tags: user_loop
- name: ----- update nginx and remove gcc -----
yum:
name: '{{item.name}}'
state: '{{item.state}}'
with_items:
- {name: 'apache2', state:'latest'}
- {name: 'gcc', state:'absent'}
tags: loop5
- name: ----- create multiple db ------
mysql_db:
name: '{{item}}'
state: present
with_items:
- 'clientdb'
- 'employeedb'
- 'providerdb'
tags: loop7
- name: ---- create multiple users -----
mysql_user:
name: '{{item}}'
password: 123456
state: present
with_items:
- user1
- user2
tags: loop8
- name: ----- give user access to multiple DBs -----
mysql_user:
name: '{{item[0]}}'
priv: '{{item[1]}}.*:ALL'
append_privs: yes
password: 123456
login_user: root
with_nested:
- ['user1', 'user2']
- ['clientdb', 'employeedb', 'providerdb']
tags: loop9
- name: ---- loop over a dictionary ----
debug:
msg: "{{item.key}}----{{item.value}}"
loop: "{{my_dict | dict2items}}"
tags: loop10
- name: ----- loop over inventory hosts in servers group -----
debug:
msg: "{{item}}"
loop: "{{groups['myservers']}}"
tags: loop11
- name: ----- loop over inventory hosts in all group ->
debug:
msg: "{{item}}"
loop: "{{groups['all']}}"
tags: loop12
- name: ----- loop over inventory hosts in all group ->
debug:
msg: "{{item}}"
loop: "{{ansible_play_batch}}"
tags: loop13
- name: ----- loop over inventory hosts in servers group ->
debug:
msg: "{{item}}"
loop: "{{ansible_play_batch}}"
loop_control:
pause: 10
tags: loop14
- name: ----- tracking loop index -----
debug:
msg: "The {{item}} index is {{my_index}}"
loop:
- first
- second
- third
- fourth
loop_control:
index_var: my_index
tags: loop15
- name: ----- Retry a task until a certain condition is met -----
shell: cat /home/test.txt
register: result
until: result.stdout.find('Ansible') != -1
retries: 3
delay: 5
tags: loop16
#- import_tasks: task2.yml
- name: ----- test handlers -----
become: yes
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
backup: yes
notify:
- restart mynginx
tags: myhandlers
- debug:
msg: "My username is: {{username}}"
tags: condition10
- set_fact:
mypass: !vault |
$ANSIBLE_VAULT;1.1;AES256
64386233643138353531373566373433623663393639633065306665366164313761353431653363
6633626462303466366337643163633161643830636336390a333865366536663632303838346663
32656632633631356334336563306563326161623335333632643133313335633566306364613564
3663396639636633380a373866396230373238613334313564363030336631343435663531313234
6363
tags: vault1
- debug:
msg: "My password is: {{mypass}}"
tags: vault1
- set_fact:
userpass: !vault |
$ANSIBLE_VAULT;1.1;AES256
38386565373062336630336332313331633039323962376631393231363939666362663434313738
6239353862613030323437623632653438636664346564630a616332343239636364393032353636
66663132666535646131336266626266666131613365343665366233616536633162323932663931
3034613731373533370a313966663237363430633561353234363538313035363735653734363466
6333
- name: ----- user add with hash -----
user:
name: dude
group: root
state: present
shell: /bin/bash
password: userpass
tags: user_hash
# Install 2 nginx server and load balance with haproxy
# both nginx server should know about haproxy address
- name: ----- Add haproxy to host -----
lineinfile:
path: /etc/hosts
insertafter: EOF
line: '192.168.56.66 haproxy'
tags: haproxy
- name: ----- Install epel-release and enginx -----
yum:
name: ['epel-release', 'nginx']
state: present
tags: install_nginx
- name: ------ Edit index.html ------
copy:
content: '<h1>hello from {{ansible_hostname}}</h1>'
dest: /usr/share/nginx/html/index.html
tags: edit_index
- name: ------ enable and start nginx ------
service:
name: nginx
enabled: true
state: started
tags: start_nginx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment