Useful Ansible commands
- Get link
- X
- Other Apps
Help: [#> ansible --help]
Version: [#> ansible --version]
#> ansible --version
ansible 2.0.0.2
config file = /etc/ansible/ansble.cfg
configured module search path = Default w/o overrides
Check: [#> ansible foo.yml --check]
Inventory: [#> ansible -i 192.x.x.x, x.x.x.x]
ListHosts: [#> ansible -list-hosts]
OneLine: [#> ansible –-one-line]
SyntaxCheck: [#> ansible –-syntax-check]
TimeOut: [#> ansible –timeout=X]
Verbose: [#> ansible –verbose]
Example: Run an ad hoc command against an Ansible inventory group and limit the execution of a playbook to a max of 5 simultaneous servers:
$ ansible europe -a "whoami" -f 5
Example: Execute a playbook against the Europe group as a privileged account (different from the SSH account
$ ansible europe -a "/usr/bin/foo" -u username --become [--ask-become-pass]
Example: Transfer a local (on the ansible control server) file to a set of remote hosts simultaneously
$ ansible europe -m copy -a "src=/opt/myfile dest=/opt/myfile"
Example: Running an ansible playbook against a single hostname or IP address
#> ansible-playbook -i "192.168.10.10," -c local hellodevopsworld.yml
Example: Running an ansible playbook against an inventory group
#> ansible-playbook myplaybook.yml
1. Configuring Ansible
/etc/ansible/ansible.cfg
# some basic default values...
#inventory = /etc/ansible/hosts
#library = /usr/share/my_modules/
#module_utils = /usr/share/my_module_utils/
#remote_tmp = ~/.ansible/tmp
#local_tmp = ~/.ansible/tmp
#forks = 5
#poll_interval = 15
#sudo_user = root
#ask_sudo_pass = True
#ask_pass = True
#transport = smart
#remote_port = 22
#module_lang = C
#module_set_locale = False
2. Defined inventory groups
/etc/ansible/hosts
[databaseservers]
mydbserver105.example.org
mydbserver205.example.org
[webservers]
mywbserver105.example.org
mywbserver205.example.org
3. Executing playbook's and targeting specific inventory files and groups
#> ansible playbookfoo.yml -l 'groupname'
Let's take a peek at how Jinja fits in with YAML:
# Example Jinja Playbook
- hosts: all
vars:
foo: "{{ lookup('env', 'FOO' }}"
tasks:
- name: Copy file
copy: src="{{ foo }}" dest=/some/path mode=0775 owner=user group=user
---
- hosts: all
vars :
http_port : 80
tasks:
- name: Install nginx web server
apt: pkg=nginx state=installed update_cache=true
notify:
- start nginx
tasks:
- name: Can we use Ansible to Install Apache2 web server
apt: pkg=apache2 state=installed update_cache=true
tasks:
- name: Use Ansible to Install nginx web server
apt: pkg=nginx state=installed update_cache=true
tasks:
- name: Use Ansible to Install MySQL web server
apt: pkg=mysql-server state=installed update_cache=true
4. Variables and Variable Files
---
- hosts: all
vars:
myvar: helloworld
vars_files:
- /vars/my_vars.yml
Example: Simple Key/Value Variables in Ansible.
---
- hosts: all
vars:
# Single Variable(s) Example
myvar: helloworld
tasks:
- name: $myvar
ping:
# Task specific variables
tasks:
- name: copy files
copy: src={{ item }} dest=/opt/{{ item }}
with_items:
- foo
- bar
# my_vars_file.yml --- east_coast_host_local: virginia
west_coast_host_local: oregon
definitions:
- servers: web
instance: apache
- servers: db
instance: cassandra
ping_server: 192.168.10.10
# Example: Simple Variables File in Ansible.
---
- hosts: all
vars_files:
- my_vars_file.yml
tasks:
- name: ping target server
ping: $ping_server
# Example: Simple Variables File in Ansible.
---
- hosts: all
tasks:
- name: include default step variables
include_vars: my_vars_file.yml
ping: $ping_server
a.
# Example: Simple Variables File in Ansible.
---
- hosts: all
vars_files:
- my_vars_file.yml
- "/opt/varsfiles/{{ env_vars }}.yml"
tasks:
- name: ping target server
ping: $ping_server
$> ansible-playbook site.yml -e "env_vars=dev" -c local
Hosts and Inventory
Ansible offers a default inventory file, which is typically located in the /etc/ansible/hosts file location.
# This example uses the default hosts file provided by Ansible
# and executes playbook.yml
$> ansible-playbook -i hosts playbook.yml
# This example specifies an alternative hosts file
# and executes playbook.yml
$> ansible-playbook -i /opt/mynewinventoryfile playbook.yml
# This example specifies a set of hosts directly on the
# command line and executes playbook.yml
$> ansible-playbook -i fqdn.example.com,playbook.yml
Ansible hosts file example (/etc/ansible/hosts):
# Example Ansible hosts file with two defined groups
[WEB]
192.168.10.10
[DATABASE]
192.168.10.11
Targeting the WEB hosts group via an Ansible playbook (playbook.yml):
# Example Ansible Playbook, which targets the 'WEB' group
---
- hosts: WEB
vars :
http_port : 80
tasks:
- name: Install nginx web server
apt: pkg=nginx state=installed update_cache=true
notify:
- start nginx
# Run Ansible and instruct it to execute the contents of
# playbook.yml against the inventory file of hosts
# (the default Ansible inventory)
$> ansible-playbook playbook.yml -i hosts
# Example hosts line values:
hosts: all -- Applies the current playbook to all hosts in the specified inventory file
hosts: hostname -- Applies the playbook ONLY to the specified host 'hostname'
hosts: groupname -- Applies the playbook to all hosts in specified groupname
hosts: groupA,groupB -- Applies the playbook to hosts in groupB and groupB
hosts: group1,host1 -- A combination of single hosts and groups
hosts: *.google.com -- Applies the playbook to wildcard matches
https://docs.ansible.com/ansible/latest/collections/index_module.html
Comments
Post a Comment