Ansible Linux Playbook Example - One Intro Example


---
# this is an annotated example of some features available in playbooks
# it shows how to make sure packages are updated, how to make sure
# services are running, and how to template files.  It also demos
# change handlers that can restart things (or trigger other actions)
# when resources change.  For more advanced examples, see example2.yml

# on all hosts, run as the user root...

- name: example play
  hosts: all
  remote_user: root

# could have also have done:
#  remote_user: mdehaan
#  become: yes
#  become_method: sudo

  # make these variables available inside of templates
  # for when we use the 'template' action/module later on...

  vars:
    http_port: 80
    max_clients: 200
  # define the tasks that are part of this play...

  tasks:
  # task #1 is to run an arbitrary command
  # we'll simulate a long running task, wait for up to 45 seconds, poll every 5
  # obviously this does nothing useful but you get the idea

  - name: longrunner
    command: /bin/sleep 15
    async: 45
    poll: 5

  # let's demo file operations.
  #
  # We can 'copy' files or 'template' them instead, using jinja2
  # as the templating engine.  This is done using the variables
  # from the vars section above mixed in with variables bubbled up
  # automatically from tools like facter and ohai.  'copy'
  # works just like 'template' but does not do variable subsitution.
  #
  # If and only if the file changes, restart apache at the very
  # end of the playbook run
  - name: write some_random_foo configuration
    template: src=templates/foo.j2 dest=/etc/some_random_foo.conf
    notify:
    - restart apache

  # make sure httpd is installed at the latest version
  - name: install httpd
    yum: pkg=httpd state=latest

  # make sure httpd is running
  - name: httpd start
    service: name=httpd state=running

  # handlers are only run when things change, at the very end of each
  # play.  Let's define some.  The names are significant and must
  # match the 'notify' sections above

  handlers:
    # this particular handler is run when some_random_foo.conf
    # is changed, and only then
    - name: restart apache
      service: name=httpd state=restarted



Ansible Linux Playbook Example - Prompt

Content of var_prompt.tf


- hosts: all
  gather_facts: False
  vars_prompt:
    - name: "Version"
      prompt: "Which version Do you want to install?"

  tasks:
    - name: Ansible prompt example.
      debug:
        msg: "{{ Version }}"

Ansible Linux Playbook Example - Prompt

Content of prompts.tf


---

# it is possible to ask for variables from the user at the start
# of a playbook run, for example, as part of a release script.

- hosts: all
  remote_user: root

# regular variables are a dictionary of keys and values

  vars:
     this_is_a_regular_var: 'moo'
     so_is_this: 'quack'

# alternatively, they can ALSO be passed in from the outside:
#    ansible-playbook foo.yml --extra-vars="foo=100 bar=101"
# or through external inventory scripts (see online API docs)

# here's basic mode prompting.  Specify a hash of variable names and a prompt for
# each.
#
# vars_prompt:
#   release_version: "product release version"

# prompts can also be specified like this, allowing for hiding the prompt as
# entered.  In the future, this may also be used to support crypted variables

  vars_prompt:
    - name: "some_password"
      prompt: "Enter password"
      private: yes

    - name: "release_version"
      prompt: "Product release version"
      default: "my_default_version"
      private: no

    - name: "my_password2"
      prompt: "Enter password2"
      private: yes
      encrypt: "md5_crypt"
      confirm: yes
      salt_size: 7
      salt: "foo"

# this is just a simple example to show that vars_prompt works, but
# you might ask for a tag to use with the git module or perhaps
# a package version to use with the yum module.

  tasks:

  - name: imagine this did something interesting with {{release_version}}
    shell: echo foo >> /tmp/{{release_version}}-alpha

  - name: look we crypted a password
    shell: echo my password is {{my_password2}}





Ansible Linux Playbook Example - Execute Script


---
- name: This sets up an httpd webserver
  hosts: localhost
  tasks:
  - name: Install the httpd apps
    yum: name=httpd
  - name: start the httpd service
    service: name=httpd state=started
  - name: example copying file with owner and permissions
    copy:
      src: /opt/test.sh
      dest: /tmp/test.sh
      mode: 0644
  - script: /tmp/test.sh

Ansible Linux Playbook Example - Register vars

When using register, the data is registered only for the current host (localhost in your case). This way, you can have different value on each host.

If you want to access the variables of another host (of the group node-servers in your case), you have to use hostvars:

{{ hostvars.localhost.server }}

More info https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#accessing-information-about-other-hosts-with-magic-variables


# Example 1
- hosts: all
  tasks:
  - name: Ansible register variable basic example
    shell: "find *.txt"
    args:
      chdir: /test
    register: find_output

  - debug:
      var: find_output
# Make sure that /test should not be empty


# # Example 2
- hosts: all
  tasks:
  - name: Ansible register with_items example
    shell: "find *.txt"
    args:
      chdir: /Users/mdtutorials2/Documents/Ansible
    register: with_output

  - shell: "cp {{ item }} {{item}}_bkp"
    with_items:
      - "{{ with_output.stdout_lines }}"

Ansible Linux Playbook Example - nginx


---
- name: Install nginx
  hosts: host.name.ip
  become: true

  tasks:
  - name: Add epel-release repo
    yum:
      name: epel-release
      state: present

  - name: Install nginx
    yum:
      name: nginx
      state: present

  - name: Insert Index Page
    template:
      src: index.html
      dest: /usr/share/nginx/html/index.html

  - name: Start NGiNX
    service:
      name: nginx
      state: started

Ansible Linux Playbook Example - include


# Program 1
- hosts: localhost
  tasks:
    - debug:
        msg: play1

- name: Include a play after another play
  include: otherplays.yaml

# Program 2
- hosts: all
  tasks:
    - debug:
        msg: task1

    - name: Include task list in play
      include: stuff.yaml

    - debug:
        msg: task10

# Program 3
- hosts: all
  tasks:
    - debug:
        msg: task1

    - name: Include task list in play only if the condition is true
      include: "{{ hostvar }}.yaml"
      static: no
      when: hostvar is defined
      
# Program 4
- name: this is a play at the top level of a file
  hosts: all
  remote_user: root
  tasks:
  - name: say hi
    tags: foo
    shell: echo "hi..."
    
- include: intro_example.yml

######FULLL EXAMPLE##################

#my_tasks.yml
- name: Check PID of existing Java process
  shell: "ps -ef | grep [j]ava"
  register: java_status

  - debug: var=java_status.stdout

#check_java_pid.yml
---
- hosts: all
  gather_facts: no

  
  tasks:
    - include my_tasks.yml

#check_java_pid.yml
---
- hosts: instance_1
  gather_facts: no

  tasks:
    - include: my_tasks.yml

Ansible Linux Playbook Example - Handlers


---
- hosts: webservers
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
    yum:
      name: httpd
      state: latest
  - name: write the apache config file
    template:
      src: /srv/httpd.j2
      dest: /etc/httpd.conf
    notify:
    - restart apache
  - name: ensure apache is running
    service:
      name: httpd
      state: started
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted

Ansible Linux Playbook Example - Handlers into file


# more_handlers.yml
---
  tasks:
- name: restart apache
  service: name=apache state=restarted
  
# main_handlers.yml  
- hosts: webs
  sudo: yes

  tasks:
  - name: Ensure that Apache is installed
    yum: name=httpd state=present
    when: ansible_os_family == "RedHat"

  - name: Start Apache Services
    service: name=httpd enabled=yes state=started
	
  handlers:
  - include_tasks: more_handlers.yml
# or
  - import_tasks: more_handlers.yml

Ansible Linux Playbook Example - Get urls


---
- hosts: webservers
  vars:
  - jquery_directory: /var/www/html/javascript
  - person: 'Susie%20Smith'
  tasks:
  - name: Create directory for jQuery
    file: dest={{jquery_directory}} state=directory mode=0755
  - name: Grab a bunch of jQuery stuff
    get_url: url=http://code.jquery.com/{{item}}  dest={{jquery_directory}} mode=0444
    with_items:
    - jquery.min.js
    - mobile/latest/jquery.mobile.min.js
    - ui/jquery-ui-git.css
  #- name: Pass urlencoded name to CGI
  #  get_url: url=http://example.com/name.cgi?name='{{person}}' dest=/tmp/test

Ansible Linux Playbook Example - Execute mysql


##
# Example Ansible playbook that uses the MySQL module.
#
---
- hosts: all
  remote_user: root

  tasks:

    - name: Create database user
      mysql_user: user=bob password=12345 priv=*.*:ALL state=present

    - name: Create database
      mysql_db: db=bobdata state=present

    - name: Ensure no user named 'sally' exists and delete if found.
      mysql_user: user=sally state=absent

Ansible Linux Playbook Example - Execute mysql


- hosts: dbservers
  sudo: yes

  tasks:
  - name: Ensure MySQL is installed
    yum: name=mysql-server state=present
    when: ansible_os_family == "RedHat"

  - name: Start MySQL
    service: name=mysqld state=started

- hosts: webservers:dbservers
  sudo: yes

  tasks:
  - name: Stop IPTABLES NOW!!!
    service: name=iptables state=stopped

Ansible Linux Playbook Example - debug


#Content of more myvar.yaml
#---
#age: 30
---
- name: Reading variables from var files
  hosts: localhost
  connection: local
  vars_files:
    - myvar.yaml
  tasks:
    - name: Display all variables/facts known for a host
      debug: var=hostvars[inventory_hostname]
    - debug: var=age
    - name: Print version
      debug:
        msg: "My Name is : {{ age }}"
    - debug: msg="System {{ myname }} has uuid {{ ansible_product_uuid }}"
    - debug: msg={{ ansible_product_uuid }}
    - debug: var=ansible_product_uuid
    - debug:
        var: ansible_product_uuid

		
# ansible-playbook play.yaml

Ansible Linux Playbook Example - httpd


---
- name: This sets up an httpd webserver
  hosts: localhost
  tasks:
  - name: Install the httpd apps
    yum: name=httpd
  - name: start the httpd service
    service: name=httpd state=started
#  - name: Open port 80
#    firewalld: service=http permanent=true state=enabled
#  - name: start the firewalld service
#    service: name=firewalld state=stopped enabled:no

#$  ansible-playbook -i inventory httpd.yaml -u ec2-user --key-file remote.pem -b    
    
---
- name: This sets up an httpd webserver
  hosts: all
  tasks:
  - name: Install the httpd apps
    yum: name=httpd
  - name: start the httpd service
    service: name=httpd state=started
#  - name: Open port 80
#    firewalld: service=http permanent=true state=enabled
#  - name: start the firewalld service
#    service: name=firewalld state=stopped enabled:no
    
#$  ansible-playbook -i inventory httpd.yaml -u ec2-user --key-file remote.pem -b 

 
---
- name: This sets up an httpd webserver
  hosts: all
  remote_user: ec2-user
  become: yes
  vars:
   ansible_ssh_private_key_file: remote.pem
   pack: httpd
  tasks:
  - name: Install the httpd apps
    yum: name=httpd
  - name: start the httpd service
    service: name=httpd state=started
#  - name: Open port 80
#    firewalld: service=http permanent=true state=enabled
#  - name: start the firewalld service
#    service: name=firewalld state=stopped enabled:no

    
#$  ansible-playbook -i inventory httpd.yaml


---
- name: This sets up an httpd webserver
  hosts: all
  remote_user: ec2-user
  become: yes
  vars:
   ansible_ssh_private_key_file: remote.pem
   companyname: verizon
   pack: httpd
  tasks:
  - name: Install the httpd apps
    yum: name=httpd
  - name: Deploy configuration File
    template: src=templates/index.j2 dest=/var/www/html/index.html
  - name: start the httpd service
    service: name=httpd state=started
#  - name: Open port 80
#    firewalld: service=http permanent=true state=enabled
#  - name: start the firewalld service
#    service: name=firewalld state=stopped enabled:no

    
#$  ansible-playbook -i inventory httpd.yaml


---
- name: This sets up an httpd webserver
  hosts: all
  remote_user: ec2-user
  become: yes
  vars:
   ansible_ssh_private_key_file: remote.pem
   companyname: verizon
   pack: httpd
  tasks:
  - name: Install the httpd apps
    yum: name=httpd
  - name: Deploy configuration File
    template: src=templates/index.j2 dest=/var/www/html/index.html
  - name: start the httpd service
    service: name=httpd state=started
  - name: Install common software requirements
    yum: pkg={{ item }} state=installed
    with_items:
     - git
     - ntp
     - vim
#  - name: Open port 80
#    firewalld: service=http permanent=true state=enabled
#  - name: start the firewalld service
#    service: name=firewalld state=stopped enabled:no

    
#$  ansible-playbook -i inventory httpd.yaml


---
- name: This sets up an httpd webserver
  hosts: all
  remote_user: ec2-user
  become: yes
  vars:
   ansible_ssh_private_key_file: remote.pem
   companyname: verizon
   pack: httpd
  tasks:
  - name: Install the httpd apps
    yum: name=httpd
  - name: Deploy configuration File
    template: src=templates/index.j2 dest=/var/www/html/index.html
  - name: start the httpd service
    service: name=httpd state=started
  - name: Install common software requirements
    yum: pkg={{ item }} state=installed
    with_items:
     - git
     - ntp
     - vim
  - name: Creating a file
    file: path=/opt/verizon.txt state=touch
    when:
      - ansible_distribution == "RedHat"
      - ansible_distribution_major_version == "7.5"
#  - name: Open port 80
#    firewalld: service=http permanent=true state=enabled
#  - name: start the firewalld service
#    service: name=firewalld state=stopped enabled:no

    
#$  ansible-playbook -i inventory httpd.yaml

  tasks:
  - name: Install the httpd apps
    yum: name=httpd
  - name: start the httpd service
    service: name=httpd state=started
#  - name: Open port 80
#    firewalld: service=http permanent=true state=enabled
#  - name: start the firewalld service
#    service: name=firewalld state=stopped enabled:no
    
#$  ansible-playbook -i inventory httpd.yaml -u ec2-user --key-file remote.pem -b