Table of Contents
DevOps - Ansible
Ansible
- develop module: http://docs.ansible.com/ansible/developing_modules.html
Ansible kvm
- Networking:
Ansible OpenStack
Ansible tips
- Develope module: http://docs.ansible.com/ansible/developing_modules.html
- ansible-galaxy: requirements.yml http://stackoverflow.com/questions/25230376/how-to-automatically-install-ansible-galaxy-roles
Role and library
- Ansible module is a .py file implementing the module. It must be placed in playbookdir/library, or playbookdir/roles/role_name/library, or in a directory specified in “library=” in ansible.cfg
- There is currently no way to pull module from git and place in library folders. This is useful when i.e., forking available module for development. However, ansible-galaxy enables installation of roles from git, etcs. One way that make sense is to fork the module and turn it into role, which can be installed by ansible-galaxy in requirements.yml file.
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