playbook-loop.yml примеры циклов loop, with_items
---
- name: Loops Hello
hosts: all
become: yes
tasks:
- name: Say Hello to ALL
debug: msg="Hello {{ item }}"
loop:
- "Vasya"
- "Masha"
- "Olga"
- "Petr"
- name: loop until example
shell: echo -n Z >> myfile.txt && cat myfile.txt
register: output
delay: 2
retries: 10
until: output.stdout.find("ZZZZ") == false
- name: Print Final Output
debug:
var: output.stdout
# - name: Install many packeg
# apt: name={{ item }} state=present
# with_items:
# - apache2
# - htop
# - tree
...
playbook.yml install-and-copy-folder
---
- name: Install Apache and Upload my Web Page
hosts: all
become: yes
vars:
source_folder: ./website
destin_folder: /var/www/html
tasks:
- name: Cheack and Print Linux Version
debug: var=ansible_os_family
- block: # === BLOCK REDHAT ====
- name: Install Apache Web Server for RedHat
yum: name=httpd state=present
- name: Start Apache and Enable it on every boot
service: name=httpd state=started enabled=yes
when: ansible_os_family == "RedHat"
- block: # === BLOCK DEBIAN ====
- name: Install Apache Web Server for Debian
apt: name=apache2 state=present
- name: Start Apache and Enable it on every boot
service: name=apache2 state=started enabled=yes
when: ansible_os_family == "Debian"
- name: Copy folder to web Servers
copy: src={{ source_folder }}/{{ item }} dest={{ destin_folder }} mode=0555
loop:
- "file0.txt"
- "file1.txt"
- "file2.txt"
- "file3.txt"
- "file4.txt"
- "file5.txt"
notify:
- Restart Apache RedHat
- Restart Apache Debian
handlers:
- name: Restart Apache RedHat
service: name=httpd state=restarted
when: ansible_os_family == "RedHat"
- name: Restart Apache Debian
service: name=apache2 state=restarted
when: ansible_os_family == "Debian"
...
playbook.yml install-and-copy-folder по маске
---
- name: Install Apache and Upload my Web Page
hosts: all
become: yes
vars:
source_folder: ./website
destin_folder: /var/www/html
tasks:
- name: Cheack and Print Linux Version
debug: var=ansible_os_family
- block: # === BLOCK REDHAT ====
- name: Install Apache Web Server for RedHat
yum: name=httpd state=present
- name: Start Apache and Enable it on every boot
service: name=httpd state=started enabled=yes
when: ansible_os_family == "RedHat"
- block: # === BLOCK DEBIAN ====
- name: Install Apache Web Server for Debian
apt: name=apache2 state=present
- name: Start Apache and Enable it on every boot
service: name=apache2 state=started enabled=yes
when: ansible_os_family == "Debian"
- name: Copy folder to web Servers
copy: src={{ item }} dest={{ destin_folder }} mode=0555
with_fileglob: "{{ source_folder }}/*.*"
notify:
- Restart Apache RedHat
- Restart Apache Debian
handlers:
- name: Restart Apache RedHat
service: name=httpd state=restarted
when: ansible_os_family == "RedHat"
- name: Restart Apache Debian
service: name=apache2 state=restarted
when: ansible_os_family == "Debian"
...