Ansible

General

Ansible cnetralizes model:

ansible general diagram

  • Adhoc example:

    $ ansible -m copy -a "src=orignalFile.txt dest=finalFile.txt" --check --diff localhost
    
    

    Where

    • Check = to run in dry mode
    • Diff = Shows the difference before and after apply ansible
    • “copy” is a module, list of all ansible modules in here
    • “m” module name
    • “a” arguments

    More examples ansible examples adhoc

Playbook

  • A playbook uses a Yaml file as main place to read from.

  • A task can be visualise as:

tasks components

  • To run playbook:

    $ ansile-playbook playbookFile.yml
    
  • Ansible-playbook calls the “setup” module, which is mask as “Gathering facts " in the logs, and it can be executed manually as:

    $ ansible -m setup localhost
    

    This helps to understand the current state of an environment.

    NOTE:

    You can disable this feature “gathering facts” in the playbook, see below. It will speed up the run.

    setting false gathering facts

  • More output lines

    You can add more verbose, “-v”, to the output:

    $ ansible-playbook playbook.yml -v
    $ ansible-playbook playbook.yml -vv
    $ ansible-playbook playbook.yml -vvv
    $ ansible-playbook playbook.yml -vvvv
    
  • Output meaning:

    To understand the output logs, you can refer to the documentation of the module you are working on, or you can use ansible-doc to get information about a module from command line

    $ ansible-doc copy
    
  • Another example indentataion ansible

Inventory

  • Ansible ignore the following extensions:

    $ ansible-config --list
    
    ...
    INVENTORY_IGNORE_EXTS:
      default: '{{(BLACKLIST_EXTS + (''.orig'', ''.ini'', ''.cfg'', ''.retry''))}}'
      description: List of extensions to ignore when using a directory as an inventory
        source
    ...
    
    
  • Ansible does not recommend to use “.” as part of the name of a hosts or group of hosts

    Not replacing invalid character(s) "{'.'}" in group name (new_group_jenkins_agents.ol7)
    [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
    Not replacing invalid character(s) "{'.'}" in group name (newgroup_jenkins_agents.ol7)
    Not replacing invalid character(s) "{'.'}" in group name (oldgroup_jenkins_agents.olh6)
    
    
  • Ansible graph presents hosts in different format:

    $ ansible-inventory --graph
    [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
    @all:
      |--@backup_coordinator_group.ol7:
      |  |--hostonw.dcnet
      |--@bitbucketgroup.ol6:
      |  |--host2.dcnet
      |  |--host2b.dcnet
      |--@bitbucketgroup.ol6:
      |  |--host3.cdnet
      |--@health_checker_group.ol7:
      |  |--host4.dcnet
    
    
  • To run on any of this groups:

    $ ansible -m command -a "ls -ltr " bitbucketgroup
    $ ansible -m command -a "ls -ltr " host4.dcnet
    
    
  • Some useful links

  • Steps to consider to use inventories:

    • Config ansible using “ansible.cfg”
      [default]
      # disale host_key_chkcings
      # https://docs.ansible.com/ansible/latest/user_guide/connection_details.html
      host_key_checking = False
            
      inventory=myDirectory
      # export ANSIBLE_INVENTORY = /path/directory/inventory
      # FYI : "vagrant ssh-config " is a great guide for configuring ansible to
      # connect directly to VMs created by vagrant