My Wiki!

DevOps - Ansible

Ansible

Ansible kvm

Ansible OpenStack

Ansible tips

Role and library

How to do that?:

Clone a module e.g., nmcli, and copy meta/main.yml to make it a role :-):

tree library_ext/ansible-nmcli-b3936faaedd4c0ab7f091806460253eff508e816
library_ext/ansible-nmcli-b3936faaedd4c0ab7f091806460253eff508e816
├── library
│   └── nmcli_dev.py
├── meta
│   └── main.yml
├── playbook-add.yml
├── playbook-del.yml
├── README.md
└── testing_scripts
    ├── add-connection.py
    ├── list-connections.py
    ├── list-con-simple.py
    ├── list-devices.py
    ├── test-dbus.py
    └── timetest.py

Now it can be installed by ansible-galaxy in to library_ext folder:

# cat requirements.yml 
---
- src:  https://github.com/thuydang/ansible_nmcli_role.git
  path: ./library_ext/

# vim ansible.cfg so moudles in subdir are searched for by ansible
# the trick is putting library in the path. This should be supported by default but is currently a bug:
library        = /usr/share/ansible:library_ext:library #https://github.com/ansible/ansible/issues/16561#issuecomment-230146841
# vim ansible.cfg to add library_ext to list of roles folders
roles_path    = library_ext:roles_ext

# install the role (library) with ansible-galaxy
[dang@dai142 ansible_quicklabs]$ ansible-galaxy install -r requirements.yml 
- extracting ansible_nmcli_role to /mnt/nfv/ansible_quicklabs_ws/ansible_quicklabs/library_ext/ansible_nmcli_role
- ansible_nmcli_role was installed successfully

copy meta to make it detected as a role

Nested list in yaml variables

Ansible Debug

Step: Enable debug

[dang@dai142 ansible]$ cat vfoss_dev_postgres_setup_playbook.yml 
---
- name: Install and configure postgresql replica sets
  strategy: debug       <---------------- this is it
  hosts: vfoss_dev
  become: true
  remote_user: root
  vars_files:
    - 'vars/vars_vfoss_dev.yml'
  roles:
    - { role: ansible-role-postgresql }

Step: Make it failed then the debug will kickin

[dang@dai142 ansible]$ cat roles/ansible-role-postgresql/tasks/main.yml 
---
- name: Installing postgresql
  include: 'install.yml'

- debug: msg={{ postgresql_data_dir }}

- debug: var={{ postgresql_master  }}
- name: DEBUG postgresql_master    <------------------- this is it
  fail: msg="debug"
  when: (postgresql_master is undefined) or (not postgresql_master|bool) #or true
  #when: not postgresql_master|bool

- name: Configuring the master
  include: 'configure-master.yml'
  when: postgresql_master

Step: Playbook Debuger console

p task/vars/host/result : Print values used to execute a module:

task.args[key] = value : Update module’s argument.

  task.args['name'] = 'bash'
  

vars[key] = value : Update vars.

  vars['pkg_name'] = 'bash'
  (debug) vars['postgresql_streaming_slaves'] = []
  (debug) r
  

r(edo)

Run the task again. c(ontinue)

Just continue. q(uit)

Ansibletools

yaml-online-parser.appspot.com


Navigation