mysql 与Ansible处理器一起使用时

mm5n2pyu  于 2023-01-01  发布在  Mysql
关注(0)|答案(2)|浏览(130)

有一个可以在所有主机上运行的ansible剧本,包括基于Debian和RedHat的服务器,这将在基于RHEL的服务器上安装mariadb,只做一些配置更改,一旦服务启动的处理程序被用于更改根密码,就启动服务,但问题是处理程序似乎应用在所有主机上,而不是只应用在基于RHEL的服务器上,当语句不与处理程序一起工作时,检查以下代码:

---

  - name: "Install & Configure MariaDB server on RHEL8"
    hosts: all
    vars:
      required_distribution: "RedHat"
      required_version: "8"

      my_packages:
        - mariadb-server
        - python3-PyMySQL
    tasks:
      - name: "check & install MariaDB on RHEL 8 only"
        yum:
          name: "{{ item }}"
          state: present
        loop: "{{ my_packages }}"
        when: "ansible_distribution == required_distribution and ansible_distribution_major_version == required_version"

      - name: "start & enable MariaDb service"
        service:
          name: mariadb
          state: started
          enabled: true
        when: "ansible_distribution == required_distribution and ansible_distribution_major_version == required_version"
        notify:
          - root_password

    handlers:
      - name: root_password
        mysql_user:
          name: root
          password: password
        when: "ansible_distribution == required_distribution and ansible_distribution_major_version == required_version"

收到此警告:

RUNNING HANDLER [root_password] ****************************************************************************************************************************************************
task path: /home/student/labs/lab6/lab6.yaml:29
[WARNING]: Module did not set no_log for update_********
changed: [server-b] => {"changed": true, "msg": "Password updated (new style)", "user": "root"}
changed: [server-a] => {"changed": true, "msg": "Password updated (new style)", "user": "root"}
changed: [server-d] => {"changed": true, "msg": "Password updated (new style)", "user": "root"}
changed: [server-c] => {"changed": true, "msg": "Password updated (new style)", "user": "root"}
META: ran handlers
META: ran handlers

我在这里考虑的是处理程序也应用在ubuntu-a服务器上&给我***"[警告]:模块未设置更新的no_log_*****"*
如果这个警告不是来自ubuntu-a服务器,那么如何删除它,我的剧本中有什么问题,这只适用于基于RHEL的服务器上的处理程序吗?
我还在寻找编写行动手册的某种有效方法,以便每次不必编写when block来筛选出托管主机时,都必须检查一次(可能在行动级别),并将其应用于所有任务

vxbzzdmp

vxbzzdmp1#

根据存货清单

shell> cat hosts
host_A
host_B

[test_ubuntu]
host_A ansible_host=10.1.0.184

[test_ubuntu:vars]
ansible_connection=ssh
ansible_user=admin
ansible_become=yes
ansible_become_user=root
ansible_become_method=sudo
ansible_python_interpreter=/usr/bin/python3.9

[test_centos]
host_B ansible_host=10.1.0.74

[test_centos:vars]
ansible_connection=ssh
ansible_user=admin
ansible_become=yes
ansible_become_user=root
ansible_become_method=sudo
ansible_python_interpreter=/bin/python3.6

问:***"编写行动手册以筛选出托管主机的有效方法。"***
答:使用库存插件 * 构造 *。请参见

shell> ansible-doc -t inventory ansible.builtin.constructed

1.缓存事实。配置缓存,例如

shell> cat ansible.cfg
[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_cache
fact_caching_prefix = ansible_facts_
fact_caching_timeout = 86400
...

然后按计划行事

shell> cat pb2.yml
- hosts: all
  tasks:
    - debug:
        msg: Completed.

这将创建缓存

shell> tree /tmp/ansible_cache/
/tmp/ansible_cache/
├── ansible_facts_host_A
└── ansible_facts_host_B

1.创建清单

shell> tree inventory/
inventory/
├── 01-hosts
└── 02-constructed.yml

0 directories, 2 files
shell> cat inventory/01-hosts 
host_A
host_B

[test_ubuntu]
host_A ansible_host=10.1.0.184

[test_ubuntu:vars]
ansible_connection=ssh
ansible_user=admin
ansible_become=yes
ansible_become_user=root
ansible_become_method=sudo
ansible_python_interpreter=/usr/bin/python3.9

[test_centos]
host_B ansible_host=10.1.0.74

[test_centos:vars]
ansible_connection=ssh
ansible_user=admin
ansible_become=yes
ansible_become_user=root
ansible_become_method=sudo
ansible_python_interpreter=/bin/python3.6
shell> cat inventory/02-constructed.yml 
plugin: ansible.builtin.constructed
strict: true
groups:
  centos_8: ansible_distribution == 'CentOS' and ansible_distribution_major_version == '8'
  ubuntu_20: ansible_distribution == 'Ubuntu' and ansible_distribution_major_version == '20'

测试库存

shell> ansible-inventory -i inventory --graph
@all:
  |--@centos_8:
  |  |--host_B
  |--@test_centos:
  |  |--host_B
  |--@test_ubuntu:
  |  |--host_A
  |--@ubuntu_20:
  |  |--host_A
  |--@ungrouped:

你可以看到这个插件 * constructed * 创建了组 * centos_8 * 和 * ubuntu_20 *。
1.在戏剧中使用小组。例如,

shell> cat pb3.yml
- hosts: centos_8

  tasks:

    - setup:
        gather_subset: distribution
    - debug:
        msg: |
          ansible_distribution: {{ ansible_distribution }}
          ansible_distribution_major_version: {{ ansible_distribution_major_version }}

    - debug:
        msg: Trigger handler
      changed_when: true
      notify: root_password

  handlers:

    - name: root_password
      debug:
        msg: Change root password

给予

shell> ansible-playbook -i inventory pb3.yml 

PLAY [centos_8] ******************************************************************************

TASK [setup] *********************************************************************************
ok: [host_B]

TASK [debug] *********************************************************************************
ok: [host_B] => 
  msg: |-
    ansible_distribution: CentOS
    ansible_distribution_major_version: 8

TASK [debug] *********************************************************************************
changed: [host_B] => 
  msg: Trigger handler

RUNNING HANDLER [root_password] **************************************************************
ok: [host_B] => 
  msg: Change root password

PLAY RECAP ***********************************************************************************
host_B: ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

问:***"这个处理程序也适用于Ubuntu。"***
答:在您的代码中,没有这样做的理由。例如,给定数据

required_distribution: "CentOS"
  required_version: "8"

任务

- setup:
        gather_subset: distribution
    - debug:
        msg: |
          ansible_distribution: {{ ansible_distribution }}
          ansible_distribution_major_version: {{ ansible_distribution_major_version }}

Give

ok: [host_A] => 
  msg: |-
    ansible_distribution: Ubuntu
    ansible_distribution_major_version: 20
ok: [host_B] => 
  msg: |-
    ansible_distribution: CentOS
    ansible_distribution_major_version: 8

你的病情如预期般好转

- debug:
        msg: |
          ansible_distribution is {{ required_distribution }} and
          ansible_distribution_major_version is {{ required_version }}
      when: "ansible_distribution == required_distribution and ansible_distribution_major_version == required_version"

给予

skipping: [host_A]
ok: [host_B] => 
  msg: |-
    ansible_distribution is CentOS and
    ansible_distribution_major_version is 8

你不必引用条件

when: ansible_distribution == required_distribution and ansible_distribution_major_version == required_version

您可以设置格式

when: ansible_distribution == required_distribution and
            ansible_distribution_major_version == required_version

,甚至更好

when:
        - ansible_distribution == required_distribution
        - ansible_distribution_major_version == required_version

所有选项都给出相同的结果。
测试处理程序。如果条件位于将触发处理程序的任务中,则没有理由将该条件放入处理程序

handlers:

    - name: root_password
      debug:
        msg: Change root password

以下任务将仅触发 * required_distribution * 和 * required_version * 的处理程序

- debug:
        msg: Trigger handler
      changed_when: true
      notify: root_password
      when:
        - ansible_distribution == required_distribution
        - ansible_distribution_major_version == required_version

给予

TASK [debug] *********************************************************************************
skipping: [host_A]
changed: [host_B] => 
  msg: Trigger handler

RUNNING HANDLER [root_password] **************************************************************
ok: [host_B] => 
  msg: Change root password

PLAY RECAP ***********************************************************************************
  • 完整的测试行动手册示例
- hosts: all
  gather_facts: false

  vars:

    required_distribution: "CentOS"
    required_version: "8"

  tasks:

    - setup:
        gather_subset: distribution
    - debug:
        msg: |
          ansible_distribution: {{ ansible_distribution }}
          ansible_distribution_major_version: {{ ansible_distribution_major_version }}

    - debug:
        msg: |
          ansible_distribution is {{ required_distribution }} and
          ansible_distribution_major_version is {{ required_version }}
      when: "ansible_distribution == required_distribution and ansible_distribution_major_version == required_version"

    - debug:
        msg: |
          ansible_distribution is {{ required_distribution }} and
          ansible_distribution_major_version is {{ required_version }}
      when: ansible_distribution == required_distribution and ansible_distribution_major_version == required_version

    - debug:
        msg: |
          ansible_distribution is {{ required_distribution }} and
          ansible_distribution_major_version is {{ required_version }}
      when: ansible_distribution == required_distribution and
            ansible_distribution_major_version == required_version

    - debug:
        msg: |
          ansible_distribution is {{ required_distribution }} and
          ansible_distribution_major_version is {{ required_version }}
      when:
        - ansible_distribution == required_distribution
        - ansible_distribution_major_version == required_version

    - debug:
        msg: Trigger handler
      changed_when: true
      notify: root_password
      when:
        - ansible_distribution == required_distribution
        - ansible_distribution_major_version == required_version

  handlers:

    - name: root_password
      debug:
        msg: Change root password
  • 此项目的树
shell> tree .
.
├── ansible.cfg
├── hosts
├── inventory
│   ├── 01-hosts
│   └── 02-constructed.yml
├── pb2.yml
├── pb3.yml
└── pb.yml

1 directory, 7 files
laik7k3q

laik7k3q2#

我不认为这条消息表明处理程序正在Ubuntu上运行,但如果没有看到您的清单和其他更多信息,很难说。
不管怎么说,如果你想只适用当“当”一次(好冲动,你有两个选择:
1.把所有的任务放在一个“块”中,并把when应用到块中,如果所有的任务都使用相同的when跳过,那么你甚至不需要在处理程序中使用then when。
1.如果您真正想要的是在非RedHat 8主机上完全放弃这个剧本,那么您可以像这样使用end_host,作为剧本中的第一个任务。

# Example showing how to end the play for specific targets
- name: End the play for hosts are not the desired versions
  ansible.builtin.meta: end_host
  when: ansible_distribution != required_distribution or ansible_distribution_major_version != required_version

相关问题