Skip to main content

Posts

Showing posts with the label Ansible

Ansible: summing the data disk space

The context On each host, I want to calculate the volume occupied by the OS and data disks. The OS is always installed on sda. The data disks are sdb, sdc, ... facter_disks provides info on the disks. I use the attribute size_bytes . The size of OS is easy to get: facter_disks.sda.size_bytes . However, it's less obvious to sum up the other disks since you don't know how many disks you have. Remember Ansible does not allow nested loops. Well, at least explicitely... The solution Extract sdb , sdc ,.. from facter_disks Convert it to a list Loop on this list to extract the path to attribute size_bytes Sum up the values (optional) Add the sum to facts - hosts: all gather_facts: true become: yes become_method: sudo ignore_errors: yes tasks: - set_fact: os_disk_total_size_gb: "{{facter_disks.sda.size_bytes/1073741824}}" data_disks_total_size_gb: "{{ facter_disks | map('regex_search'...

Ansible: how to read a data file

Let's say I have a data file, each line contains several columns. I want to read the file line per line, select the columns. My data file abc 123 def 32 alice ghi 12345 alice bob The idea is to loop on the file line per line with the splitlines function: loop: "{{ lookup('file', '/path/to/file').splitlines() }}" Then, for each line, we split the columns based on a separator, and we put the result in a variable: myvar: "{{ item.split( sep , maxcol ). index }}" Where: sep is the separator maxcol is the maximum number of columns you want to use. If a line contains more columns that maxcol , the last column contains the rest of the line. index is the index of the current column. So the overall playbook will looks like: - name: ... vars: A: "{{ item.split(' ',3).0 }}" B: "{{ item.split(' ',3).1 }}" C: "{{ item.split(' ',3).2 }}" function: ... loop: "...

Ansible: ERROR! failed to combine variables, expected dicts but got a 'dict' and a 'AnsibleSequence'

I was slowly refactoring my standard playbooks to roles when I encountered this error. It took me a while to figure out what was wrong. Start: the All-In-One playbook myplaybook.yml - hosts : rhel vars : ... tasks : - name : ... ... This kind of playbook is hard to maintain. Also, some tasks where common to several playbooks, so I've decided to split it into several: myplaybook.yml - hosts : rhel vars : ... tasks : - name : ... include_tasks : path/to/task-1.yml include_tasks : path/to/task-2.yml ... This was nice, but I encountered issues with variables that were not seen by sub-tasks. Must be mentioned that sub-tasks should not include the tasks keyword; only a list of tasks. task-1.yml - name : ... ... Here comes the roles Roles has been precisely designed to split complex tasks into reusable modules. playbooks roles my_custom_role defaults main.yml files my-cert-ad.pem handlers ...

Some examples and hints on Ansible

I play with Ansible for quite a while now. Here are some real life examples and hints. Yaml good indentation - hosts: ... vars: filename: ... vars_files: - ... tasks: - name:... command: ... Managing several versions - lineinfile: name: /etc/inittab regexp: '^ca::ctrlaltdel' line: 'ca::ctrlaltdel:/bin/logger -p authpriv.warning -t init "Console-invoked Ctrl-Alt-Del was ignored"' state: 'present' when: ansible_distribution_major_version == "5" - name: Disable Ctrl+Alt+Delete on RHEL6 block: - file: path: /etc/init/control-alt-delete.override state: absent - lineinfile: path: /etc/init/control-alt-delete.override create: yes owner: root group: root mode: 0644 line: 'exec /usr/bin/logger -p authpriv.notice -t init "Console-invoked Ctrl-Alt-Del was ignored"...

Ansible: How installing a group of packages?

Installing a set of packages with Ansible The context I need to install a group of packages on multiple Linux servers. Ansible is the right tool for that. The host file: hosts [test_machines]    test1.my_domain.com    test2.my_domain.com [dev]    dev1.my_domain.com    dev2.my_domain.com [uat]    uat1.my_domain.com    uat2.my_domain.com [prod]    prd1.my_domain.com    prd2.my_domain.com The playbook : add_packages.yml - hosts: all   vars_files:     - data/all_my_packages.yml   tasks:   - name: Ensure a set of packages is installed.     yum:       name={{ item.name }}       state={{ item.state }}     with_items: '{{ packages }}' The datafile: data/all_my_packages.yml packages:  - { name: 'glibc.i686', state: 'present' }  - { name: 'glibc.x86_64', state: 'present' ...