# Ansible Playbooks [toc] #### 1. ping.yaml ````go=1 vi ping.yaml Put the following content in the file: --- ## Using playbook to run ping on all nodes - hosts: all remote_user: ansible become: yes tasks: - name: Ping all nodes action: ping Run the playbook via the following command: ansible-playbook ping.yaml ```` #### 2. elinks.yml ````yaml=1 --- # Basic play to install a package - hosts: nodes become: yes tasks: - name: install elinks yum: name: elinks state: latest ```` #### 3. vars.yaml ````yaml=1 --- - hosts: '{{ myhosts }}' become: yes tasks: - name: Install below mentioned software on above mentioned nodes apt: name: '{{ pkg }}' state: latest ```` :mag: Run above playbook using the following command: ```console! $ ansible-playbook vars.yaml --extra-vars "myhosts=nodes pkg=telnet" or $ ansible-playbook vars.yaml -e "myhosts=nodes pkg=telnet" ``` #### 4. user.yaml ````yaml=1 --- - name: create user sk12k hosts: all tasks: - name: Create users user: name: sk12k home: /home/sk12k shell: /bin/bash ```` #### 5. loop.yaml ````yaml=1 --- ## Using loop to create - name: create user hosts: nodes become: yes tasks: - name: create multiple users listed user: name: "{{item}}" state: present loop: - sk12k - saif - hari - juarez - dani ## Play to install multiple packages - name: install packages hosts: nodes become: yes tasks: - name: install packages mentioned below apt: name: "{{item}}" state: latest loop: - tree - git - elinks - telnet ```` #### 6. handlers.yaml ````yaml=1 --- ## Playbook to show how handlers work in Ansible - name: install and configure apache hosts: nodes become: yes tasks: - name: install apache2 ## Apache installation apt: name: apache2 state: latest update_cache: yes - name: Upload index file ## Index.html page copied from controller to all webservers copy: src: index.html dest: /var/www/html/index.html mode: 0755 notify: restart apache2 handlers: - name: restart apache2 ## Make sure the service is running service: name: apache2 state: restarted ```` #### 7. get_url.yaml ````yaml=1 --- ## use get_url module to download tomcat package from Apache portal - name: Download Tomcat from tomcat.apache.org hosts: nodes become: yes tasks: - name: Create a directory for Tomcat file: path: /tmp/tomcat state: directory - name: Download tomcat get_url: url: https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.83/bin/apache-tomcat-8.5.83.tar.gz dest: /tmp/tomcat mode: '0755' group: sk12k owner: sk12k ```` #### facts.yaml ````yaml=1 --- ## Playbook to take action based on ansible facts - name: using facts hosts: nodes become: yes tasks: - name: worker1 folder ansible.builtin.command: mkdir /tmp/worker1 when: ansible_facts['hostname'] == "ip-172-31-57-187" - name: folder on worker2 ansible.builtin.command: mkdir /tmp/worker2 when: ansible_facts['hostname'] == "ip-172-31-49-9" ```` #### apache_conditional.yaml ````yaml=1 --- ## Playbook to install apache on both Debian and Redhat systems - name: Install apache hosts: nodes become: yes tasks: - name: Apache on Debian when: ansible_facts['os_family'] == "Debian" ansible.builtin.package: name: apache2 state: present - name: Apache on Redhat when: ansible_facts['os_family'] == "RedHat" ansible.builtin.package: name: httpd state: present ```` #### NodeJS installation using Ansible Playbook ````yaml= --- ## Install NodeJS using Ansible Playbook - name: install nodejs hosts: nodes gather_facts: True become: true tasks: - name: add apt key for nodesource apt_key: url=https://deb.nodesource.com/gpgkey/nodesource.gpg.key - name: add repo for nodesource apt_repository: repo: 'deb https://deb.nodesource.com/node_0.10 {{ ansible_distribution_release }} main' update_cache: no - name: install nodejs apt: name=nodejs ````