如何删除用户和主机ansible?

时间:2015-08-06 18:49:52

标签: php mysql ansible

我目前有安装mySql的Ansible手册和其他一些东西。在mySQL角色期间,我让所有root用户及其主机都正确设置。然而,在playbook成功完成后,其中一个用户(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)

1 个答案:

答案 0 :(得分:1)

看起来像mysql_user module中的一个错误 - 问题是你的主机名中有一个连字符,而且模块没有引用用户和主机,所以模块执行的DROP USER是可能会默默地失败(即,没有匹配)。出于好奇,你有没有在非连字主持人上试过这个游戏?