Saya dapat memecahkan masalah saya dengan mendefinisikan variabel ke host tiruan . baru dan kemudian menggunakannya di seluruh buku pedoman dengan hostvars .
Solusi serupa telah disebutkan di salah satu jawaban di Bagaimana cara mengatur variabel register agar tetap ada di antara pemutaran di ansible? Namun saya tidak menyadarinya sampai saya memposting pertanyaan ini.
Inilah yang saya lakukan dalam tugas yang memungkinkan:
- Saya telah membuat host tiruan master_value_holder dan mendefinisikan variabel yang diperlukan. (Di sini saya membutuhkan master_log_file danmaster_log_Postion )
- Mengakses variabel menggunakan hostvars['master_value_holder']['master_log_file']
Tugas di Master
- 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 }}"
Tugas untuk Budak
- 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
Keluaran
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]
Seperti yang Anda lihat dari output di atas bahwa status replikasi master tersedia untuk kedua host sekarang.