Simple Loops :
[root@control]mkdir playbook_test && cd playbook
[root@control]vim /playbook_test/cusers_playbook.yml
[root@control]$ vi create_users.yaml- hosts: centos_webserver
become: yes
tasks:
- name: Create new users
user:
name: '{{ item.name }}'
uid: '{{ item.uid }}'
state: present
loop:
- name: tiya
uid: 1020
- name: riya
uid: 1030
- name: jack
uid: 1040
[root@control]$ansible-playbook create_user.yml
Example of start service
[root@control]$vim service_start.yml
- name: Postfix and Dovecot are runningservice:
name: "{{ item }}"
state: started
with_items:
- postfix
- dovecot
[root@control]$ansible-playbook service_start.yml
Nested Loops
[root@control]$vim user_for_dbs.yml
tasks:- name: All DB users have privileges on all databases
mysql_user:
name: "{{ item[0] }}"
priv: "{{ item[1] }}.*:ALL"
append_privs: yes
password: redhat
with_nested:
[ 'joe', 'jane' ]
[ 'clientdb', 'employeedb', 'providerdb' ]
Example of install webserver :
without loop :
[root@control]$vim webserver_install.yml
- hosts: all
vars:
run_my_task: true
tasks:
- name: httpd package is installed
yum:
name: httpd
when: run_my_task
Example With Loop
[root@control]$vim webserver_install.yml
- hosts: all
vars:
my_service: httpd
tasks:
- name: "{{ my_service }} package is installed"
yum:
name: "{{ my_service }}"
when: my_service is defined
No comments:
Post a Comment