Mysql
 sql >> Database >  >> RDS >> Mysql

Come utilizzare i valori di ritorno di un'attività in un'altra attività per un host diverso in ansible

Sono stato in grado di risolvere il mio problema definendo le variabili in un nuovo host fittizio e quindi utilizzarlo nel playbook con hostvars .

Una soluzione simile è stata già menzionata in una delle risposte in Come faccio a impostare il registro di una variabile in modo che persista tra le riproduzioni in ansible? Tuttavia non me ne sono accorto finché non ho pubblicato questa domanda.

Ecco cosa ho fatto nelle attività ansible:

  • Ho creato un host fittizio master_value_holder e definito le variabili richieste. (Qui avevo bisogno di master_log_file e master_log_Postion )
  • Accesso alle variabili usando hostvars['master_value_holder']['master_log_file']

Compiti del maestro

- name: Mysql - Check master replication status.
  mysql_replication: mode=getmaster
  register: master

- name: "Add master return values to a dummy host"
  add_host:
    name:   "master_value_holder"
    master_log_file: "{{ master.File }}"
    master_log_pos: "{{ master.Position }}"

Compiti per lo schiavo

- name: Mysql - Displaying master replication status
  debug: msg="Master Bin Log File  is {{ hostvars['master_value_holder']['master_log_file'] }} and Master Bin Log Position is {{ hostvars['master_value_holder']['master_log_pos'] }}"

- name: Mysql - Configure replication on the slave.
  mysql_replication:
    mode: changemaster
    master_host: "{{ replication_master }}"
    master_user: "{{ replication_user }}"
    master_password: "{{ replication_pass }}"
    master_log_file: "{{ hostvars['master_value_holder']['master_log_file'] }}"
    master_log_pos: "{{ hostvars['master_value_holder']['master_log_pos'] }}"
  when: ansible_eth0.ipv4.address != replication_master and not slave.Slave_SQL_Running

Risultato

TASK [Mysql_Base : Mysql - Check master replication status.] ****************
skipping: [stagmysql02]
ok: [stagmysql01]

TASK [AZ-Mysql_Base : Add master return values to a dummy host] ****************
changed: [stagmysql01]

TASK [AZ-Mysql_Base : Mysql - Displaying master replication status] ************
ok: [stagmysql01] => {
    "msg": "Master Bin Log File  is mysql-bin.000001 and Master Bin Log Position is 154"
}
ok: [stagmysql02] => {
    "msg": "Master Bin Log File  is mysql-bin.000001 and Master Bin Log Position is 154"
}

TASK [AZ-Mysql_Base : Mysql - Configure replication on the slave.] *************
skipping: [stagmysql01]
skipping: [stagmysql02]

Come puoi vedere dall'output sopra, lo stato della replica master è ora disponibile per entrambi gli host.