gpt4 book ai didi

php - 如何删除ansible用户和主机?

转载 作者:行者123 更新时间:2023-11-29 12:04:02 24 4
gpt4 key购买 nike

我目前有一个安装 mySql 和其他一些东西的 Ansible playbook。在 mySQL 角色期间,我正确设置了所有 root 用户及其主机。然而,在剧本成功完成后,其中一个用户 (root@keenan-virtualbox) 在我尝试将其从表中删除后仍然存在。我不想明确删除该帐户并希望它可以在许多不同的计算机上运行。因此, $ansible_hostname 应该是我要删除的主机。下面是来自 mysql 任务的 main.yml 以及完成后的 mysql.user 表。

   ---
- name: MySQL | Set debconf vars
action: raw sudo echo mysql-server mysql-server/root_password password
$mysql_root_password | sudo /usr/bin/debconf-set-selections
tags: mysql

- name: MySQL | Set debconf vars
action: raw sudo echo mysql-server mysql-server/root_password_again password $mysql_root_password | sudo /usr/bin/debconf-set-selections
tags: mysql

- name: Add the OS specific variables
include_vars: "{{ ansible_os_family }}.yml"

- name: Install the mysql packages in Redhat derivatives
yum: name={{ item }} state=installed
with_items: mysql_pkgs
when: ansible_os_family == 'RedHat'

- name: Install the mysql packages in Debian derivatives
apt: name={{ item }} state=installed update_cache=yes
with_items: mysql_pkgs
environment: env
when: ansible_os_family == 'Debian'

- name: Copy the my.cnf file
template: src=my.cnf.{{ ansible_os_family }}.j2 dest={{ mysql_conf_dir }}/my.cnf
notify:
- restart mysql

- name: Create the directory /etc/mysql/conf.d
file: path=/etc/mysql/conf.d state=directory
notify:
- restart mysql

- name: Start the mysql services Redhat
service: name={{ mysql_service }} state=started enabled=yes

- name: update mysql root password for all root accounts
ignore_errors: yes
mysql_user: name=root host={{ item }} password={{ mysql_root_db_pass }}
with_items:
- $ansible_hostname
- 127.0.0.1
- ::1
- localhost
- name: copy .my.cnf file with root password credentials
template: src=.my.cnf.j2 dest=~/.my.cnf mode=0600

- name: ensure anonymous users are not in the database
mysql_user: name='' host={{ item }} state=absent
with_items:
- localhost
- "{{ ansible_hostname }}"

- name: remove the test database
mysql_db: name=test state=absent

- name: Create the database's
mysql_db: name={{ item.name }} state=present
with_items: mysql_db
when: mysql_db|lower() != 'none'

- name: Create the database users
mysql_user: name={{ item.name }} password={{ item.pass|default("") }}
priv={{ item.priv|default("*.*:ALL") }} state=present host={{ item.host | default("localhost") }}
with_items: mysql_users
when: mysql_users|lower() != 'none'

- name: Delete ansible host root user
mysql_user: name=root host={{ ansible_hostname }} state=absent


- name: Restart mysql
action: service name=mysql state=restarted enabled=true



--------------------------------------------------------------------------


+------------------+-------------------+-------------------------------------------+
| user | host | password |
+------------------+-------------------+-------------------------------------------+
| root | localhost | *3F9DF5A32114E05C12C50A83EAE02991016C917B |
| root | keenan-VirtualBox | |
| root | 127.0.0.1 | *3F9DF5A32114E05C12C50A83EAE02991016C917B |
| root | ::1 | *3F9DF5A32114E05C12C50A83EAE02991016C917B |
| debian-sys-maint | localhost | *726DD22D744226797C5E47F531F32DE43F595B5B |
| root | $ansible_hostname | *3F9DF5A32114E05C12C50A83EAE02991016C917B |
+------------------+-------------------+-------------------------------------------+
8 rows in set (0.02 sec)

最佳答案

看起来像 mysql_user module 中的错误- 问题是您的主机名中有一个连字符,并且模块没有引用用户和主机,因此模块执行的 DROP USER 可能会默默失败(即不匹配)。只是出于好奇,您是否在非连字符的主机上尝试过此操作?

关于php - 如何删除ansible用户和主机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31863630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com