2

I am trying to write a playbook to setup mysql master-slave replication with multiple slave servers. For each slave server, I need access to a variable called next_id that should be incremented before use for each host. For example, for the first slave server, next_id should be 2 and for the second slave server it should be 3 and so on. What is the way to achieve this in Ansible?

This is the yaml file I use to run my role.

- name: Setup the environment
  hosts: master , slave_hosts
  serial: 1
  roles:
     - setup-master-slave
  vars:
     master_ip_address : "188.66.192.11"
     slave_ip_list :
       - "188.66.192.17"
       - "188.66.192.22"

This is the yaml file where I use the variable.

- name: Replace conf file with template
  template:
     src: masterslave.j2
     dest: /etc/mysql/mariadb.conf.d/50-server.cnf
  when: inventory_hostname != 'master'
  vars:
     - ip_address : "{{ master_ip_address }}"
     - server_id : "{{ next_id  }}"
Unsel
  • 343
  • 2
  • 8

2 Answers2

0

I can suggest a way which will work according to you requirement for any number of slave servers but it is not based on any any module but self conscience. Here my master_ip_address is 10.x.x.x and input is any value of next_id you want to increment for every slave server

- hosts: master,slave1,slave2,slave3,slave4
  serial: 1
  gather_facts: no
  tasks:
  - shell: echo "{{ input }}" > yes.txt
    delegate_to: localhost
    when: inventory_hostname == '10.x.x.x'

  - shell: cat yes.txt
    register: var
    delegate_to: localhost
    when: inventory_hostname != '10.x.x.x'
  - shell: echo "$(({{var.stdout}}+1))"
    register: next_id
    delegate_to: localhost
    when: inventory_hostname != '10.x.x.x'
  - shell: echo "{{ next_id.stdout }}" > yes.txt
    delegate_to: localhost
    when: inventory_hostname != '10.x.x.x'
  - name: Replace conf file with template
    template:
       src: masterslave.j2
       dest: 50-server.cnf
    when: inventory_hostname != '10.x.x.x'
    vars:
       - ip_address : "{{ master_ip_address }}"
       - server_id : "{{ next_id.stdout  }}"

[ansibleserver@172 test_increment]$ cat masterslave.j2 
       - {{ ip_address }}
       - {{ server_id }} 

Now, if you run

ansible-playbook increment.yml -e 'master_ip_address=10.x.x.x input=1'

slave1 server

[root@slave1 ~]# cat 50-server.cnf 
       - 10.x.x.x
       - 2 

slave2 server

[root@slave2 ~]# cat 50-server.cnf 
       - 10.x.x.x
       - 3 

slave3 server

[root@slave3 ~]# cat 50-server.cnf 
       - 10.x.x.x
       - 4 

and so on

0

"serial" is available in a playbooks only and not working in roles

therefore, for fully automatic generation of server_id for MySQL in Ansible roles, you can use the following:

roles/my_role/defaults/main.yml

---
cluster_alias: test_cluster
mysql_server_id_config: "settings/mysql/{{ cluster_alias }}/server_id.ini"

roles/my_role/tasks/server-id.yml

---
- name: Ensures '{{ mysql_server_id_config | dirname }}' dir exists
  file:
    path: '{{ mysql_server_id_config | dirname }}'
    state: directory
    owner: root
    group: root
    mode: 0755
  delegate_to: localhost
    
- name: Ensure mysql server id config file exists
  copy:
    content: "0"
    dest: "{{ mysql_server_id_config }}"
    force: no
    owner: root
    mode: '0755'
  delegate_to: localhost
    
- name: server-id
  include_tasks: server-id-tasks.yml
  when: inventory_hostname == item
  with_items: "{{ ansible_play_hosts }}"
  tags:
    - server-id

roles/my_role/tasks/server-id-tasks.yml

# tasks file
---
- name: get last server id
  shell: >
    cat "{{ mysql_server_id_config }}"
  register: _last_mysql_server_id
  check_mode: no
  delegate_to: localhost
  tags:
    - server-id

- name: get new server id
  shell: >
    echo "$(({{_last_mysql_server_id.stdout}}+1))"
  register: _new_mysql_server_id
  check_mode: no
  delegate_to: localhost
  tags:
    - server-id

- name: save new server id
  shell: >
    echo -n "{{ _new_mysql_server_id.stdout }}" > "{{ mysql_server_id_config }}"
  check_mode: no
  delegate_to: localhost
  tags:
    - server-id
    
- debug:
    var: _new_mysql_server_id.stdout
  tags:
    - server-id
    
- name: Replace conf file with template
  template:
    src: server-id.j2
    dest: server-id.cnf