kknd22
4/27/2018 - 5:57 PM

Example Ansible Delegation

Example Ansible Delegation

# prepare and install for ssh cert
#
# ssh-keygen -t rsa -C"web"
# ssh-keygen -t rsa -C"db"
#
# ssh-copy-id -i ~/.ssh/id_rsa.pub vagrant@192.168.33.22
# ssh-copy-id -i ~/.ssh/id_rsa.pub vagrant@192.168.33.21
#
# wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# sudo rpm -ivh epel-release-6-8.noarch.rpm
# yum --disablerepo=epel -y update  ca-certificates
# yum repolist

ansible-playbook ./playbook.yml -i inventory.ini \
-u vagrant -s --private-key=~/.ssh/id_rsa
- hosts: web
  tasks:
    - name: Touch a file on the web server
      shell: "echo 'webserver1' > /opt/whoami"

- hosts: db
  tasks:
    - name: Touch a file on the db server
      shell: "echo 'dbserver1' > /opt/whoami"

- hosts: web
  tasks:
    - name: Collect database server name
      register: dbserver_whoami
      shell: cat /opt/whoami
      delegate_to: "{{ groups['db'][0] }}"
      ignore_errors: True

    - name: Store database server name
      shell: "echo '{{ dbserver_whoami.stdout }}' > /opt/dbserver"

- hosts: db
  tasks:
    - name: Collect webserver server name
      register: webserver_whoami
      shell: cat /opt/whoami
      delegate_to: "{{ groups['web'][0] }}"
      ignore_errors: True

    - name: Store webserver server name
      shell: "echo '{{ webserver_whoami.stdout }}' > /opt/webserver"
[web]
192.168.33.21

[db]
192.168.33.22
# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = '2'

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box     = 'centos6'
  config.vm.box_url = 'http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130731.box'

  config.vm.boot_timeout = 60

  config.vm.define 'web' do |config|
    config.vm.network :private_network, ip: '192.168.33.21'
  end

  config.vm.define 'db' do |config|
    config.vm.network :private_network, ip: '192.168.33.22'
  end

end