Ansible playbook examples and techniques
Overview:
Some examples of Ansible playbook syntax, key value and techniques.
- # Example of execute other playbook:
- – import_playbook: host_discovery.yml
- – import_playbook: example.yml
- # Example execute task with Ansible role:
- – import_tasks: host_discovery.yml
- – import_tasks: example.yml
- # Example of initiate Ansible Role:
- – name: Initiate Ansible Role: prac_role
- tasks:
- – include_role: name=prac_role
- or
- roles:
- – prac_role
- tasks:
- – name: Initiate Ansible Role: prac_role
- # Example of Ansible playbook play and tasks:
- – – –
- – name: First Ansible play to run other tasks
- – become: true # when like to run the play with escalate privilege
- – hosts: all
- name: Ansible Playbook Examples
- vars:
- acct:
- – testuser1
- – testuser2
- acct:
- vars_prompt:
- – name: example_tool_download # call it later with “when: example_tool_download | bool”
- prompt: “Download example-tool.zip from Apache server (yes/no)”
- private: false
- prompt: “Download example-tool.zip from Apache server (yes/no)”
- private: false
- – name: filePath # call the variable later with “{{ filePath }}”
- prompt: “Enter file path of the ISO”
- privete: false
- – name: example_tool_download # call it later with “when: example_tool_download | bool”
- tasks:
- – name: Simple PING test example
- ping:
- – name: Account Creation example
- user:
- name: “{{ item }}”
- groups: wheel
- password: “<generated_password_hash>”
- loop: “{{ acct }}”
- user:
- – name: Account verification
- getent:
- database: shadow
- key: “{{ item }}”
- loop: “{{ acct }}”
- getent:
- – name: Single Package Installation Example
- yum: name=vsftpd state=present update_cache=true
- – name: Multiple Packages Installation Example
- become: true
- yum:
- name:
- – httpd
- – nginx
- – vsftpd
- state: present
- update_cache: true
- name:
- – name: Latest Package Updates Example
- become: true
- yum:
- name: “*”
- state: latest
- – name: Register practice with command module
- command: “id testuser”
- register: acctInfo
- – name: Find and copy with command example
- command: “find /tmp -iname ‘example-tool.zip’ -exec cp -t $HOME {} +”
- ignore_errors: true
- – name: Outputs with line break example
- vars:
- msg: |
- Distribution: {{ ansible_distribution }}
- Release: {{ ansible_distribution_release }}
- Distribution Version: {{ ansible_distribution_version }}
- Kernel: {{ ansible_kernel }}
- Architecture: {{ ansible_architecture }}
- debug:
- msg: “{{ msg.split(‘\n’) }}”
- msg: |
- vars:
- – name: Create local repo file
- file:
- path: /etc/yum.repos.d/local.repo
- mode: “0644”
- state: touch
- file:
- – name: Modify local repo file created
- copy:
- dest: /etc/yum.repos.d/local.repo
- content: |
- [localbase]
- name=Local Base
- baseurl=http://x.x.x.10/rhel90/BaseOS
- enabled=1
- gpgcheck=1
- gpgfile=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
- [localapp]
- name=Local AppStream
- baseurl=http://x.x.x.10/rhel90/AppStream
- enabled=1
- gpgcheck=1
- gpgfile=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
- copy:
- – name: Modify or replace line inf file
- lineinfile:
- create: true
- state: present
- mode: 0644
- path: /etc/conf/examplefile
- insertafter: ‘allow perm=any dir=/opt/targetdir/ : all’
- line: ‘allow perm=any dir=/opt/example_dir : all’
- lineinfile:
- – name: Unpack compressed file
- unarchive:
- src: “{{ansible_env.HOME}}/{{example_tool_zip_file}}”
- dest: /opt
- unarchive:
- – name: Package up files
- archive:
- path: /opt/tartget_path/
- dest: /dest/path/example.tar.gz
- archive:
- – name: File transfer
- fetch:
- src: local_system_file.tar.gz
- dest: /remote_system/dest_path/dir
- fetch:
- – name: Simple PING test example