如何在ansible中为不同的主机使用一个任务在另一个任务中的返回值

mfpqipee  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(430)

我试图用ansible为一个由2台mysql主机组成的主机组设置mysql主从复制。
以下是我的设想:
我在第一台主机上运行一个任务并跳过第二台主机,因此第一个任务(即主复制状态)返回一些值,如position、file等。
然后,我在第二个主机上运行另一个任务(跳过第一个主机),这个任务使用第一个任务的返回值,如master.position、master.file等。
现在,当我运行playbook时,第一个任务的变量在第二个任务中似乎不起作用
库存文件

[mysql]
    stagmysql01 ansible_host=1.1.1.1 ansible_ssh_user=ansible ansible_connection=ssh
    stagmysql02 ansible_host=1.1.1.2 ansible_ssh_user=ansible ansible_connection=ssh

主机上的任务

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

- debug: var=master

从机上的任务

- 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: "{{ master.File }}"
    master_log_pos: "{{ master.Position }}"
  ignore_errors: True

主输出

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

TASK [Mysql_Base : debug]***************************************************
ok: [stagmysql01] => {
    "master": {
        "Binlog_Do_DB": "", 
        "Binlog_Ignore_DB": "mysql,performance_schema", 
        "Executed_Gtid_Set": "", 
        "File": "mysql-bin.000003", 
        "Is_Master": true, 
        "Position": 64687163, 
        "changed": false, 
        "failed": false
    }
}
ok: [stagmysql02] => {
    "master": {
        "changed": false, 
        "skip_reason": "Conditional result was False", 
        "skipped": true
    }
}

从机输出

TASK [Mysql_Base : Mysql - Configure replication on the slave.]*************
skipping: [stagmysql01]
fatal: [stagmysql02]: FAILED! => {"failed": true, "msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'File'\n\nThe error appears to have been in '/root/ansible/roles/Mysql_Base/tasks/replication.yml': line 30, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Mysql - Configure replication on the slave.\n  ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'dict object' has no attribute 'File'"}
...ignoring

如上所述,由于未定义的变量,第二个主机的第二个任务失败。但是,第一个主机的第一个任务中有必需的变量。
如何在另一个任务中使用从第二个主机中的第一个主机返回的变量?
p、 s:我见过使用{{hostvars['inventory\u hostname']['variable']}的方法。不过,我对这种方法很困惑,因为需要直接添加主机名或ip地址。我正在寻找一个共同的模板,可以用于不同的库存文件和剧本。

xxslljrj

xxslljrj1#

我可以通过将变量定义为一个新的虚拟主机,然后在带有hostvars的playbook中使用它来解决问题。
类似的解决方案已经在ansible中的“如何设置寄存器变量以在两次播放之间保持”中的一个答案中提到?然而,我没有注意到它,直到我张贴这个问题。
以下是我在ansible任务中所做的:
我已经创建了一个虚拟主机主\u值\u持有者,并定义了所需的变量(这里我需要主日志文件和主日志位置)
使用hostvars['master\u value\u holder']['master\u log\u file']访问变量
主机上的任务

- 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 }}"

从属任务

- 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

输出

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]

从上面的输出可以看出,主复制状态现在可用于两台主机。

相关问题