cmdref.net - Command Reference

This site is command references for system engineers.

User Tools

Site Tools


Top     OS ( Linux )     Middleware     Protocol     Hardware     Programming ( Shell Script )     SiteMap

Sidebar

Top     SiteMap








Reference











.

middleware:iac:ansible:index.html



Middleware

Ansible

Web Sites

Installation

RHEL/CentOS

http://mirrors.isu.net.sa/pub/fedora/fedora-epel/7/x86_64/repoview/epel-release.html

# yum install epel-release
# yum install ansible

or

# curl http://mirrors.isu.net.sa/pub/fedora/fedora-epel/7/x86_64/e/epel-release-7-6.noarch.rpm > epel-release-7-6.noarch.rpm
# rpm -ivh epel-release-7-6.noarch.rpm

# yum install ansible


hosts

The default file is /etc/ansible/hosts.

[web]
192.168.0.101
192.168.0.102

[db]
192.168.0.100

[production:children]
web
db
[webservers]
web-[1:15].example.com 
[examples]
www.example.com    ansible_user=uer01    ansible_ssh_pass=pass1


ansible Command

man ansible

NAME
       ansible - run a task on a target host(s)

SYNOPSIS
       ansible <host-pattern> [-m module_name] [-a args] [options]

ARGUMENTS
       host-pattern
           A name of a group in the inventory, a shell-like glob selecting hosts in inventory or any combination of the two separated by
           commas.

OPTIONS
       -i PATH, --inventory=PATH
           The PATH to the inventory, which defaults to /etc/ansible/hosts. Alternatively you can use a comma
           separated list of hosts or single host with traling comma host,.

       -a 'ARGUMENTS', --args='ARGUMENTS'
           The ARGUMENTS to pass to the module.

       -m NAME, --module-name=NAME
           Execute the module called NAME.

       -s, --sudo
           Run the command as the user given by -u and sudo to root (deprecated, use become).

       K, --ask-become-pass
           Ask for privilege escalation password.


$ ansible testservers -a "/bin/date"
$ ansible webservers -a "/sbin/reboot" -f 10 --sudo

-m ping

$ ansible all -m ping
$ ansible -i /tmp/hosts all -m ping

-m command

$ ansible group1 -i /tmp/hosts all -m command -a "touch /tmp/test1.txt"

-m shell

$ ansible testserver -m shell -a 'echo $TERM'

-m copy

$ ansible testservers -m copy -a "src=/etc/hosts dest=/tmp/hosts"

-m file

$ ansible testservers -m file -a "dest=/tmp/hosts mode=600 owner=root group=root" -s -K

-m yum

$ ansible webservers -m yum -a "name=httpd state=installed"




ansible-playbook command

man ansible-playbook

NAME
       ansible-playbook - run an ansible playbook

SYNOPSIS
       ansible-playbook <filename.yml> ... [options]

ARGUMENTS
       filename.yml
           The names of one or more YAML format files to run as ansible playbooks.

OPTIONS
       -h, --help
           Show help page and exit

       --list-hosts
           Outputs a list of matching hosts; does not execute anything else.

       --list-tasks
           List all tasks that would be executed; does not execute anything else.


       -i PATH, --inventory=PATH
           The PATH to the inventory, which defaults to /etc/ansible/hosts. Alternatively, you can use a comma-separated list of hosts or a
           single host with a trailing comma host,.

       -C, --check
           Do not make any changes on the remote system, but test resources to see what might have changed. Note this can not scan all
           possible resource types and is only a simulation.

       --syntax-check
           Look for syntax errors in the playbook, but don’t run anything

Example

$ ansible-playbook  PLAYBOOK.yml
$ ansible-playbook -i /tmp/hosts PLAYBOOK.yml
$ ansible-playbook --private-key=./KEY ./PLAYBOOK.yml

--list-tasks

$ ansible-playbook --list-tasks -i test-servers site.yml

--check

$ ansible-playbook  PLAYBOOK.yml --check


Playbook

command module

- name: disable selinux
    command: /sbin/setenforce 0

shell module

tasks:
  - shell: /home/shell/test.sh > result.txt
- name: ruby configure
  shell: chdir=/tmp/{{ rubyver }} ./configure --disable-install-doc

- name: make ruby
  shell: chdir=/tmp/{{ rubyver }} make

- name: make install ruby
  shell: chdir=/tmp/{{ rubyver }} make install

user module

tasks:
  - user: name=user2 password=$6$rounds=656000$3Co6RKpxxxx

get_url module

get_url: url=http://toolbelt.treasuredata.com/sh/install-redhat.sh dest=/root/fluentd-install.sh

copy module

tasks:
  - name: copy a file
    copy: src=test.txt dest=/tmp/
  - name: copy directory
    copy: src=/tmp/test1.d dest=/tmp/test1.d/
  - name: copy files in directory
    copy: src=/tmp/test1.d/ dest=/tmp/test1.d/

fetch module

tasks:
  - fetch: src=/tmp/test2.txt dest=/tmp/

service module

tasks:
  - service: name=httpd state=restarted

file module

- name: create directories
  file: path={{ item.path }} owner={{ item.owner }} group={{ item.group }} mode=0{{ item.mode }} state=directory
  with_items:
    - { "path":"/test/test2", "owner":"root", "group":"root", "mode":"755" }
    - { "path":"/test/test3", "owner":"root", "group":"root", "mode":"777" }

unarchive module

unarchive: src=/tmp/ruby.tar.gz dest=/tmp copy=no

lineinfile module

lineinfile: dest=/etc/sysconfig/selinux regexp="^SELINUX=.*" line="SELINUX=disabled"
lineinfile: >
  dest=/etc/ssh/sshd_config
  regexp="^PasswordAuthentication"
  line="PasswordAuthentication no"
  insertafter="#PasswordAuthentication"


PLAYBOOK Example

- hosts: all
  become: yes
  gather_facts: no
  tasks:
  - command: touch /tmp/test1.txt
  - command: touch /tmp/test2.txt
  - command: touch /tmp/test3.txt

install httpd

- hosts: all
  become: yes
  gather_facts: no
  tasks:
  - name: check install httpd
    yum: name=httpd state=latest

  - name: start httpd and enabled httpd
    service: name=httpd state=started enabled=yes
- hosts: all
  become: yes
  gather_facts: no
  tasks: 
   - name: yum install httpd
     yum: name=httpd state=present 
  
   - name: service httpd start
     service: name=httpd state=started

   - name: chkconfig httpd on
     command: /sbin/chkconfig httpd on 

ruby install from source

---
- hosts: all
  become: yes
  gather_facts: nos
  tasks:
    - name: apt-get update
      apt: update_cache=yes

    - name: install depended packages
      apt: name={{ item }} state=latest
      with_items:
        - libffi-dev
        - libreadline6-dev
        - libssl-dev
        - make
        - zlib1g-dev

    - name: check exist of ruby source
      stat: path=/usr/local/src/ruby-2.3.0
      register: ruby_source

    - name: get source and unarchive
      unarchive: src=https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.0.tar.gz dest=/usr/local/src copy=no
      when: not ruby_source.stat.exists

    - name: build
      shell: ./configure && make && make install
      args:
        chdir: /usr/local/src/ruby-2.3.0

make install

- hosts: all
  become: yes
  gather_facts: no
  tasks: 
    - name: download hoge source file
      get_url: url=https://hostname/hoge_source.tar.gz dest=/usr/local/src/hoge_source.tar.gz

    - name: extract hoge source file
      unarchive: src=/usr/local/src/hoge_source.tar.gz dest=/usr/local/src/ creates=/usr/local/src/hoge_source

    - name: install hoge
      shell: ./configure && make && make install chdir=/usr/local/src/hoge_source creates=/usr/bin/hoge

exec shell

- hosts: all
  become: yes
  gather_facts: no
  tasks:
    - name: copy file
      copy: src=/tmp//sumple.sh dest=/tmp/sumple.sh owner=user01 group=dev mode=0755 

    - name: exec shell
      shell: /tmp/sumple.sh

    - name: delete file
      file: dest=/tmp/sumple.sh state=absent


Playbook Roles and Include Statements

Include

directory structure

ansible/
    webserver.yml
    apserver.yml
    dbserver.yml
    
    hosts

    tasks/
        common-centos6.yml
        apache.yml
        nginx.yml
        mysql.yml

    files/
        xxxx.rpm
        xxxx.sh
        xxxx.conf
webserver.yml
- hosts: all
  sudo: yes
  tasks:
    - include: tasks/common-centos6.yml
    - include: tasks/nginx.yml
execution example
$ ansible-playbook webserver -i hosts webserver.yml
$ ansible-playbook dbserver  -i hosts dbserver.yml


Roles

directory structure

ansible/
    webserver.yml
    apserver.yml
    dbserver.yml

    hosts/
          local
          production
          staging

    roles/
        common-centos6/
            files/
                xxxx.rpm
                xxxx.sh
                xxxx.conf
            tasks/
                main.yml

        nginx/
            files/
                xxxx.rpm
                xxxx.sh
                xxxx.conf
            tasks/
                main.yml

        mysql/
            files/
                xxxx.rpm
                xxxx.sh
                xxxx.conf
            tasks/
                main.yml
webserver.yml
-hosts: all
  become: yes
  roles:
    -common
    -nginx
execution example
$ ansible-playbook webserver.yml -i hosts/local



middleware/iac/ansible/index.html.txt · Last modified: 2017/05/07 by admin