Configure VM using Ansible - hqzhang/cloudtestbed GitHub Wiki

All tasks

- hosts: web
  tasks:
# command, apt/yum, copy
- name: install collectd packages
  sudo: yes
  yum:
    name: "{{ item }}"
    state: present
  with_items:
    - collectd           # collectd itself
    - libsemanage-python # for managing selinux settings here

- name: Installs nginx web server
  apt: pkg=nginx state=installed update_cache=true
  notify:
    - start nginx

- name: Upload default index.php for host
  copy: src=static_files/index.php dest=/usr/share/nginx/www/ mode=0644
  register: php
  ignore_errors: True

- name: Remove index.html for host
  command: rm /usr/share/nginx/www/index.html
  when: php|success

- name: create missing 'conf' symlink
  sudo: yes
  file:
    state: link
    src: /etc/zookeeper/conf
    dest: /opt/mesosphere/zookeeper/conf

- name: generate zookeeper consul services
  sudo: yes
  template:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
  with_items:            #for loop.
    - src: zk-consul.json.j2
      dest: /etc/consul/zk-consul.json

- name: register zookeeper service
  sudo: yes
  service:
    enabled: yes
    name: "{{ zookeeper_service }}"
    state: started
  tags:
    - zookeeper

- name: create zookeeper user
  sudo: yes
  user:
    name: "{{ zookeeper_os_user }}"
    state: present

- name: create zookeeper auth digest
  sudo: yes
  shell: "/usr/local/bin/zookeeper_digest.sh  {{ zk_super_user }}:{{ zk_super_user_secret }}"
  register: zookeeper_auth_digest
  changed_when: no
  when: zk_super_user is defined and zk_super_user_secret is defined-
~                                                                                                                               
~                                       
  1. Get all facts of target
ansible tasks list
add_host: hostname={{ HOSTNAME }}
command:
shell:
apt:
    name: foo
    update_cache: yes
file: path=/etc/systemd/network/static.network state=absent
copy/fetch:src=../../coreos/config/10-dhcp.network dest=/etc/systemd/network/10-dhcp.network force=yes mode=0644
      command: sync
      pause: seconds=10
pip: name=docker-py version=1.1.0
template: src="{{ CONFIG_DIR }}/50-insecure-registry.conf" dest= . //replate variable defined inside file
git:
        accept_hostkey: yes
        repo: https://fusionmgmtbuild-gen/something.git
docker:
get_url:
    url: http://example.com/path/file.conf
    dest: /etc/foo.conf
ansible -m setup all
  1. hosts vs modules
Ansible is similar with ssh and consists of Hosts and modules
if we want execute command in remote target:
ssh user@target command
for ansible equalence is:
hosts==user@target
modules=command
in yamal file, apt is modues
its location is in python library/modules path
ie. /Library/Python/2.7/site-packages/ansible/modules
- hosts: webserver
  tasks:
     -name: install apache
      apt:
        name: apache2
        state: present
      environment:
        RUNLEVEL: 1
  1. install ansible
sudo apt-add-repository -y ppa:ansible/ansible;
sudo apt-get update;
sudo apt-get install -y ansible;
  1. create inventory file
cat <<EOF > /etc/ansible/hosts
[web]
192.168.56.102
EOF
  1. genkey and copy key ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

  2. check hosts working ansible web -m ping

  3. use command line install sth sudo ansible web -s -m shell -a 'apt-get install nginx'

  4. create ansible-playbook

cat <<EOF > nginx.yml
  ---
- hosts: local
  tasks:
   - name: Install Nginx
     apt: pkg=nginx state=installed update_cache=true
EOF
  1. For multiple tier
cat <<EOF > nginx.yml
---
- hosts: weblocal
  sudo: yes
  roles:
    - role: ngnix
EOF
cat <<EOF > roles/ngnix/tasks/main.yml
---
- name: Installs nginx web server
  apt: pkg=nginx state=installed update_cache=true
  notify:
    - start nginx
---
cat <<EOF > roles/ngnix/handlers/main.yml
- name: start nginx
      service: name=nginx state=started
  1. use ansible-playbook to install ansible-playbook -s nginx.yml

  2. ansible configuration file

* ANSIBLE_CONFIG (an environment variable)
* ansible.cfg (in the current directory)
* .ansible.cfg (in the home directory)
* /etc/ansible/ansible.cfg
  1. ansible config file: /ect/ansible/ansible.cfg for log and ssh check
[defaults]
 log_path = /tmp/ansible.log
 #host_key_checking = False
 inventory = /etc/ansible/hosts
⚠️ **GitHub.com Fallback** ⚠️