1. #hosts文件
  2. [webserver]
  3. 192.168.203.[129:130]
  4. [dbserver]
  5. 192.168.203.131
  6. [appsrvs]
  7. 192.168.203.[129:131]
  8. [all_servers]
  9. dbserver
  10. webserver
  11. #配置文件
  12. vim /etc/ansible/ansible.cfg
  13. # uncomment this to disable SSH key host checking 是否输入yes
  14. host_key_checking = False
  15. #修改默认模块-配置文件
  16. module_name = shell
  17. #生成秘钥
  18. ssh-keygen -f /root/.ssh/id_rsa -P' '
  19. ip=192.168.203
  20. for i in {129..131};do
  21. ssh-copy-id $ip.$i
  22. done
  23. [root@dba project]# ansible all -i hosts --list-hosts
  24. hosts (3):
  25. 192.168.203.129
  26. 192.168.203.130
  27. 192.168.203.131
  28. #ansible 资产 -i hosts 模块 参数
  29. ansible all -m ping
  30. [root@dba ~]# ansible webserver --list-hosts
  31. hosts (2):
  32. 192.168.203.129
  33. 192.168.203.130
  34. #差集
  35. [root@dba ~]# ansible 'web:&app' --list-hosts
  36. hosts (2):
  37. 192.168.203.129
  38. 192.168.203.130
  39. #并集
  40. [root@dba ~]# ansible web:db --list-hosts
  41. hosts (3):
  42. 192.168.203.129
  43. 192.168.203.130
  44. 192.168.203.131
  45. 查看模块帮助
  46. [root@dba ~]# ansible-doc -s command
  47. - name: Execute commands on targets
  48. command:
  49. argv: # Passes the command as a list rather than a string. Use `argv' to avoid quoting values that would otherwise be interpreted incorrectly (for example "user
  50. name"). Only the string or the list form can be provided, not both. One or the other must be provided.
  51. chdir: # Change into this directory before running the command.
  52. cmd: # The command to run.
  53. creates: # A filename or (since 2.0) glob pattern. If it already exists, this step *won't* be run.
  54. free_form: # The command module takes a free form command to run. There is no actual parameter named 'free form'.
  55. removes: # A filename or (since 2.0) glob pattern. If it already exists, this step *will* be run.
  56. stdin: # Set the stdin of the command directly to the specified value.
  57. stdin_add_newline: # If set to `yes', append a newline to stdin data.
  58. strip_empty_ends: # Strip empty lines from the end of stdout/stderr in result.
  59. warn: # Enable or disable task warnings.
  60. ---
  61. [root@dba ~]# ansible web -a 'cat /etc/centos-release'
  62. 192.168.203.130 | CHANGED | rc=0 >>
  63. CentOS Linux release 7.9.2009 (Core)
  64. 192.168.203.129 | CHANGED | rc=0 >>
  65. CentOS Linux release 7.9.2009 (Core)

===============command与shell的区别:shell支持管道符和内置命令
copy
[root@dba ~]# ansible web -m copy -a “src=./nginx.repo dest=/etc/yum.repos.d/nginx.repo”
ansible web -a ‘ls /etc/yum.repos.d’

模块

script

  1. [root@dba project]# ansible db -i hosts -m script -a '/root/project/test.sh'

copy

  1. ansible web -m copy -a "src=./nginx.repo dest=/etc/yum.repos.d/nginx.repo"
  2. ansible web -a 'ls /etc/yum.repos.d'
  3. ansible web -m copy -a "src=./nginx.repo dest=/etc/yum.repos.d/nginx.repo backup=yes"
  4. ansible web -m shell -a 'ls /etc/yum.repos.d|grep nginx'
  5. #备份-根据hash来判断文件是否需要备份 如果没有改变则不会备份
  6. [root@dba ~]# ansible web -m copy -a "src=./nginx.repo dest=/etc/yum.repos.d/nginx.repo backup=yes"
  7. 192.168.203.129 | SUCCESS => {
  8. "ansible_facts": {
  9. "discovered_interpreter_python": "/usr/bin/python"
  10. },
  11. "changed": false,
  12. "checksum": "c62d148a221da3d7de0451794fe32d5b7df8df9e",
  13. "dest": "/etc/yum.repos.d/nginx.repo",
  14. "gid": 0,
  15. "group": "root",
  16. "mode": "0644",
  17. "owner": "root",
  18. "path": "/etc/yum.repos.d/nginx.repo",
  19. "size": 398,
  20. "state": "file",
  21. "uid": 0
  22. }
  23. #对文件的权限和属主进行设置
  24. [root@dba ~]# ansible web -m copy -a "src=./nginx.repo dest=/etc/yum.repos.d/nginx.repo backup=yes owner=nobody group=nobody mode=755"
  25. 192.168.203.131 | CHANGED => {
  26. "ansible_facts": {
  27. "discovered_interpreter_python": "/usr/bin/python"
  28. },
  29. "changed": true,
  30. "checksum": "c62d148a221da3d7de0451794fe32d5b7df8df9e",
  31. "dest": "/etc/yum.repos.d/nginx.repo",
  32. "gid": 0,
  33. "group": "root",
  34. "md5sum": "094165ee4178bb13167ff8980091fa12",
  35. "mode": "0755",
  36. "owner": "nobody",
  37. #1.拷贝文件文件至被控节点
  38. [root@m01 ~]# ansible oldboy -m copy -a "src=/etc/hosts dest=/tmp/test.txt"
  39. #2.对远端已有文件进行备份,按照时间信息备份
  40. [root@m01 ~]# ansible oldboy -m copy -a "src=/etc/hosts dest=/tmp/test.txt backup=yes"
  41. #3.向被控端主机写入数据,并且会覆盖远端文件内原有数据信息
  42. [root@m01 ~]# ansible oldboy -m copy -a "content='bgx' dest=/tmp/oldboy"
  43. src #推送数据的源文件信息
  44. dest #推送数据的目标路径
  45. backup #对推送传输过去的文件,进行备份
  46. content #直接批量在被管理端文件中添加内容
  47. group #将本地文件推送到远端,指定文件属组信息
  48. owner #将本地文件推送到远端,指定文件属主信息
  49. mode #将本地文件推送到远端,指定文件权限信息

yum_repository

  • 一般直接拷贝

gpgcheck yes/no

enable

  1. - name: Add multiple repositories into the same file (1/2)
  2. yum_repository:
  3. name: epel
  4. description: EPEL YUM repo
  5. file: external_repos
  6. baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
  7. gpgcheck: no
  8. enabled: yes
  1. [root@dba project]# ansible web -i hosts -m yum_repository -a "name=epel baseurl='http://mirrors.aliyun.com/repo/epel-7.repo' description='epel'"

yum

  • present 确认安装但是不升级
  • installed确认安装
  • latest安装并升级
  • absent、removed移除
    1. #安装
    2. [root@dba project]# ansible db -i hosts -m yum -a "name=htop state=present"
    3. #删除
    4. [root@dba project]# ansible db -i hosts -m yum -a "name=htop state=absent"

systemd

  • daemon_reload
  • enabled
  • name
  • state started stopped restarted reloaded
  1. #服务重启
  2. [root@dba project]# ansible db -i hosts -m systemd -a "name=sshd state=restarted"
  3. ansible管理服务的启动与停止,使用servicesystemd
  4. #1.启动crond服务,并加入开机自启
  5. [root@m01 ~]# ansible webservers -m service -a "name=crond state=started enabled=yes"
  6. #2.停止crond服务,并删除开机自启
  7. [root@m01 ~]# ansible webservers -m service -a "name=crond state=stopped enabled=no"
  8. #3.重启crond服务
  9. [root@m01 ~]# ansible webservers -m service -a "name=crond state=restarted"
  10. #4.重载crond服务
  11. [root@m01 ~]# ansible webservers -m service -a "name=crond state=reloaded"
  12. name # 定义要启动服务的名称
  13. state # 指定服务状态
  14. started #启动服务
  15. stopped #停止服务
  16. restarted #重启服务
  17. reloaded #重载服务
  18. enabled #开机自启

group 模块

ansible db -i hosts -m group -a "name=db_admin"

user

创建用户添加到组

[root@dba project]# ansible db -i hosts -m user -a "user=foo group=db_admin append=yes"

删除

[root@dba project]# ansible db -i hosts -m user -a "user=foo group=db_admin state=absent"

file

  • touch
  • sbaent
  • directory
  • file

创建文件

  1. [root@dba project]# ansible db -i hosts -m file -a "path=/tmp/file.sh state=touch"
  2. [root@dba project]# ansible db -i hosts -m file -a "path=/tmp/file.sh owner=nobody group=nobodymode=644"
  3. #连接的目录必须存在
  4. [root@dba project]# ansible db -i hosts -m file -a "src=/tmp/file.v1.sh dest=/tmp/file.sh state=link"
  5. 192.168.203.131 | FAILED! => {
  6. "ansible_facts": {
  7. "discovered_interpreter_python": "/usr/bin/python"
  8. },
  9. "changed": false,
  10. "gid": 0,
  11. "group": "root",
  12. "mode": "0644",
  13. "msg": "src file does not exist, use \"force=yes\" if you really want to create the link: /tmp/file.v1.sh",
  14. #
  15. - name: Create two hard links
  16. file:
  17. src: '/tmp/{{ item.src }}'
  18. dest: '{{ item.dest }}'
  19. state: hard
  20. loop:
  21. - { src: x, dest: y }
  22. - { src: z, dest: k }
  23. 1.直接修改被控端的权限
  24. [root@m01 ~]# ansible web01 -m file -a "path=/opt mode=0400" -i ./hosts
  25. 2.在被控端创建目录
  26. [root@m01 ~]# ansible oldboy -m file -a "path=/tmp/oldboy state=directory"
  27. 3.在被控端创建文件
  28. [root@m01 ~]# ansible oldboy -m file -a "path=/tmp/tt state=touch mode=555 owner=root group=root"
  29. 4.递归授权目录权限
  30. [root@m01 ~]# ansible oldboy -m file -a "path=/data owner=bgx group=bgx recurse=yes"
  31. path #指定远程主机目录或文件
  32. recurse #递归授权
  33. state #状态
  34. directory #在远端创建目录
  35. touch #在远端创建文件
  36. link #创建链接文件
  37. absent #表示删除文件或目录
  38. mode #设置文件或目录权限
  39. owner #设置文件或目录属主
  40. group #设置文件或目录属组

cron

//新建一个任务

  1. [root@dba project]# ansible db -i hosts -m cron -a "name='new job' minute='0' job='ls -alh>/dev/null'"
  2. [root@ansible3 tmp]# crontab -l
  3. #Ansible: new job
  4. 0 * * * * ls -alh>/dev/null
  5. [root@dba project]# ansible db -i hosts -m cron -a "name='new job' state=absent"
  6. [root@dba project]# ansible db -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup mysql" job=/root/mysql_bak.sh'
  7. [root@ansible3 tmp]# crontab -l
  8. #Ansible: backup mysql
  9. 30 2 * * 1-5 /root/mysql_bak.sh
  10. #禁用执行计划
  11. ansible db -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup mysql" job=/root/mysql_bak.sh disabled=yes'

debug

  1. [root@dba project]# ansible db -i hosts -m debug -a "msg='haha'"
  2. 192.168.203.131 | SUCCESS => {
  3. "msg": "haha"
  4. }

template

  1. [root@dba project]# cat hello_world.j2
  2. hello {{var}} !
  3. [root@dba project]# ansible db -i hosts -m template -a "src=hello_world.j2 dest=/tmp/j2.txt" -e "var=2021"

unarchive

  1. ansible db -m unarchive -a "src=/root/project/pro.tar.gz dest=/tmp "

lineinfile

  1. <br />

mount

[root@m01 ~]# ansible web01 -m yum -a ‘name=nfs-utils state=present’ -i ./hosts
[root@m01 ~]# ansible web01 -m file -a ‘path=/data state=directory’ -i ./hosts
[root@m01 ~]# ansible web01 -m copy -a ‘content=”/data 172.16.1.0/24(rw,sync,no_all_squash)” dest=/etc/exports’ -i ./hosts
[root@m01 ~]# ansible web01 -m systemd -a “name=nfs state=started enabled=yes” -i ./hosts
[root@m01 ~]# ansible web02 -m mount -a “src=172.16.1.7:/data path=/data fstype=nfs opts=defaults state=present”
[root@m01 ~]# ansible web02 -m mount -a “src=172.16.1.7:/data path=/data fstype=nfs opts=defaults state=mounted”
[root@m01 ~]# ansible web02 -m mount -a “src=172.16.1.7:/data path=/data fstype=nfs opts=defaults state=unmounted”
[root@m01 ~]# ansible web02 -m mount -a “src=172.16.1.7:/data path=/data fstype=nfs opts=defaults state=absent”
present # 开机挂载,仅将挂载配置写入/etc/fstab
mounted # 挂载设备,并将配置写入/etc/fstab
unmounted # 卸载设备,不会清除/etc/fstab写入的配置
absent # 卸载设备,会清理/etc/fstab写入的配置

playbook

  • hosts
  • tasks 任务集
  • variables
  • Templates
  • Hadnders和notify
  • tags
  1. [root@dba project]# cat nginx.yaml
  2. - hosts: web
  3. remote_user: root
  4. gather_facts: no
  5. tasks:
  6. - name: nginx install
  7. yum: name=nginx state=present
  8. - name: html
  9. copy: src=index.html dest=/usr/share/nginx/html/index.html
  10. - name: start nginx
  11. service: name=nginx state=started enabled=yes

安装httpd

  1. - hosts: web
  2. tasks:
  3. - name: Installed Httpd Server
  4. yum: name=httpd state=present
  5. - name: Start Httpd Server
  6. systemd: name=httpd state=started enabled=yes
  7. - hosts: web01
  8. tasks:
  9. - name: Configure web01 Website
  10. copy: content='This is Web01' dest=/var/www/html/index.html
  11. - hosts: web02
  12. tasks:
  13. - name: Cofnigure webi-2 weisite
  14. copy: content='This is Web02' dest=/var/www/html/index.html

安装nfs

  1. #模板文件
  2. vim exports.j2
  3. /data 192.168.203.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
  4. #服务端
  5. #1.安装nfs
  6. #2.配置挂载目录
  7. #创建目录 创建用户
  8. #3.启动加入自启
  9. #客户端
  10. #准备目录
  11. #启动rpcbind-安装nfs-utils
  12. #挂载共享目录
  13. - hosts: db
  14. remote_user: root
  15. gather_facts: no
  16. tasks:
  17. - name: install nfs
  18. yum: name=nfs-utils state=present
  19. - name: config nfs
  20. copy: src=./exports.j2 dest=/etc/exports backup=yes
  21. - name: nfs group
  22. group: name=www gid=666
  23. - name: nfs user
  24. user: name=www uid=666 group=666 shell=/sbin/nologin create_home=no
  25. - name: nfs data
  26. file: path=/data state=directory owner=www group=www recurse=yes
  27. - name: service start
  28. service: name=nfs state=started enabled=yes
  29. - hosts: web
  30. gather_facts: no
  31. tasks:
  32. - name: client data
  33. file: path=/nfs_tt state=directory
  34. - name: client mount
  35. mount:
  36. src: 192.168.203.131:/data
  37. path: /nfs_tt
  38. fstype: nfs
  39. opts: defaults
  40. state: mounted

变量的定义和使用

组变量

  1. #vars
  2. [root@dba project]# cat dp.yaml
  3. - hosts: web
  4. vars:
  5. - webpkgs: httpd
  6. - ftppkgs: vsftpd
  7. tasks:
  8. - name: install {{ webpkgs }} {{ ftppkgs }} rpm
  9. yum:
  10. name:
  11. - "{{ webpkgs }}"
  12. - "{{ ftppkgs }}"
  13. state: present
  14. # 变量文件
  15. [root@dba project]# cat vars_pub.yaml
  16. webpkgs: httpd
  17. ftppkgs: vsftpd
  18. #cat vars2.yaml
  19. - hosts: web
  20. vars_files: ./vars_pub.yaml
  21. tasks:
  22. - name: install rpm
  23. yum:
  24. name:
  25. - "{{ webpkgs }}"
  26. - "{{ ftppkgs }}"
  27. state: present新建
  28. #group_vars目录与主机清单的组名保持一致
  29. [root@dba project]# tree group_vars/
  30. group_vars/
  31. └── web.yaml
  32. #只对web组生效,与主机的组一致
  33. [root@dba project]# cat group_vars/web.yaml
  34. webpkgs: wget
  35. ftppkgs: tree
  36. #对所有组都生效
  37. [root@dba project]# cat group_vars/allml
  38. webpkgs: wget
  39. ftppkgs: tree
  40. #系统提供了一个特殊组all 只需要在vars下建立all的文件变好变量,所以组都能使用,不需要指定变量文件
  41. #还可以通过命令-e指定变量
  42. [root@dba project]# ansible-playbook vars3.yaml
  43. - hosts: web
  44. tasks:
  45. - name: install {{ webpkgs }} {{ ftppkgs }} rpm
  46. yum:
  47. name:
  48. - "{{ webpkgs }}"
  49. - "{{ ftppkgs }}"
  50. state: present

主机变量

  • 有主机的找主机没有的找组变量 ```yaml [root@dba host_vars]# cat 192.168.203.9 webpkgs: zmap

  • hosts: 192.168.203.129 tasks:

    • name: install {{ webpkgs }} {{ ftppkgs }} rpm yum: name:
      • “{{ webpkgs }}” state: present
  • hosts: 192.168.203.130 tasks:

    • name: install {{ webpkgs }} {{ ftppkgs }} rpm yum: name:
      • “{{ webpkgs }}” state: present

-e指定变量优先级最高

[root@dba project]# ansible-playbook var4.yaml -e ‘webpkgs=lrzsz’

  1. > 总结:
  2. > gorup_vars 针对主机清单的组
  3. > host_vars 针对主机
  4. > gorup_vars/all 对所有的组都有效
  5. 变量优先级--外置-e---->vars_files-------vars---主机清单-host_var-group_vars----group_vars/all
  6. <a name="Gyumx"></a>
  7. ### 注册变量
  8. - debug会输出所有变量的结果
  9. - 然后在根据输出的结果取指定的值
  10. ```yaml
  11. - hosts: web
  12. tasks:
  13. - name: check httpd
  14. shell: ps aux|grep httpd
  15. register: check_httpd
  16. - name: output vars
  17. debug:
  18. msg: "{{ check_httpd.stdout_lines }}"

facts变量

  • 可以采集被监控端cpu,内存,网络,磁盘,系统版本等信息 ```yaml
    • hosts: web tasks:
      • name: facts debug: msg: “{{ ansible_fqdn }} ip is {{ ansible_default_ipv4.address }}”

TASK [facts] * ok: [192.168.203.129] => { “msg”: “ansible1 ip is 192.168.203.129” } ok: [192.168.203.130] => { “msg”: “ansible2 ip is 192.168.203.130” }

  1. <a name="8063a08a"></a>
  2. #### template配合facts使用
  3. ```shell
  4. [root@dba project]# ansible web -m setup -a 'filter=ansible_memtotal_mb'
  5. 192.168.203.129 | SUCCESS => {
  6. "ansible_facts": {
  7. "ansible_memtotal_mb": 3770,
  8. "discovered_interpreter_python": "/usr/bin/python"
  9. },
  10. "changed": false
  11. }
  12. 192.168.203.130 | SUCCESS => {
  13. "ansible_facts": {
  14. "ansible_memtotal_mb": 3770,
  15. "discovered_interpreter_python": "/usr/bin/python"
  16. },
  17. "changed": false
  18. }
  19. #[root@dba project]# cat memcached.j2
  20. PORT="11211"
  21. USER="memcached"
  22. MAXCONN="1024"
  23. CACHESIZE="{{ ansible_memtotal_mb//2 }}"
  24. OPTIONS=""
  25. #mem.yaml
  26. - hosts: web
  27. tasks:
  28. - name: install
  29. yum: name=memcached state=present
  30. - name: start
  31. service: name=memcached state=started
  32. - name: check memcached
  33. shell: ps aux|grep memcached
  34. register: check_memcached
  35. - name:
  36. template: src=./memcached.j2 dest=/etc/sysconfig/memcached
  37. - name: output vars
  38. debug:
  39. msg: "{{ check_memcached.stdout_lines }}"
  40. #[root@dba project]# cat hostname.yaml
  41. - hosts: db
  42. tasks:
  43. - name: get name
  44. shell: echo $RANDOM|md5sum|head -c 8
  45. register: get_random
  46. - name: debug
  47. debug:
  48. msg: "{{ get_random }}"
  49. - name: hostname
  50. hostname: name={{ get_random.stdout }}

循环控制

when

  1. [root@dba project]# cat when.yaml
  2. - hosts: web
  3. tasks:
  4. - name: install httpd
  5. yum: name=httpd state=present
  6. - name: install httpd2
  7. yum: name=httpd2 state=present
  8. when: ( ansible_distribution == "Ubuntu" )
  9. [root@ans-mgr prod]# ansible web -m setup -a 'filter=ansible_hostname' -i hosts
  10. 192.168.203.131 | SUCCESS => {
  11. "ansible_facts": {
  12. "ansible_hostname": "web01",
  13. "discovered_interpreter_python": "/usr/bin/python"
  14. },
  15. "changed": false
  16. }
  17. 192.168.203.132 | SUCCESS => {
  18. "ansible_facts": {
  19. "ansible_hostname": "web02",
  20. "discovered_interpreter_python": "/usr/bin/python"
  21. },
  22. "changed": false
  23. }
  24. #epel
  25. - hosts: all
  26. tasks:
  27. - name:
  28. name: yum repo
  29. yum_repository: ansible epel
  30. description: EPEL YUM repo
  31. baseurl: http://mirrors.aliyun.com/epel/7/SRPMS
  32. gpgcheck: no
  33. enabled: yes
  34. when: (ansible_hostname is match ("web*"))
  35. # 根据check_httpd执行的结果来重启 如果结果是0则重启 如果不是则跳过
  36. - hosts: web
  37. tasks:
  38. - name: check_httpd
  39. command: systemctl is-active httpd
  40. ignore_errors: yes
  41. register: check_httpd
  42. - name: debug
  43. debug:
  44. msg: "{{ check_httpd }}"
  45. - name:
  46. service: name=httpd state=restarted
  47. when: check_httpd.rc == 0
  48. #非零
  49. when: check_httpd.rc != 0

items

  • 循环变量 ```yaml
  • hosts: web tasks:

    • name: restart service: name={{ item }} state=restarted with_items:
      • httpd
      • vsftp
    • hosts: db tasks:
      • name: add user user: name={{ item.name }} groups={{ item.groups }} state=present with_items:
        • { name: ‘test1’, groups: ‘bin’}
        • { name: ‘test2’, groups: ‘root’} ```

          headlers

          ```yaml
  • hosts: web force_handlers: yes #强制调用,可选 tasks:
    • name: install yum: name=httpd state=present
    • name: template: src=./httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf notify: server restart httpd

      notify: server nginx httpd -可以通知多个,配置发生变动通知

      触发

      handlers:
    • name: server restart httpd service: name=httpd state=restarted ```

tags标签

[root@dba project]# ansible-playbook tags.yaml -t "status"

[root@dba project]# ansible-playbook tags.yaml --skip-tags "status"

  1. #tags
  2. - hosts: web
  3. tasks:
  4. - name: install
  5. yum: name=httpd state=present
  6. - name: start
  7. service: name=memcached state=started
  8. - name:
  9. template: src=./httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
  10. - name:
  11. shell: 'systemctl status httpd'
  12. tags: 'status'
  13. - hosts: web
  14. gather_facts: no
  15. tasks:
  16. - name: install nfs
  17. yum: name=nfs-utils state=present
  18. tags: "install"
  19. - name: client data
  20. file: path=/nfs_tt state=directory
  21. - name: client rpc
  22. yum: name=nfs-utils state=present
  23. - name: client mount
  24. tags: 'mount'
  25. mount:
  26. src: 192.168.203.131:/data
  27. path: /nfs_tt
  28. fstype: nfs
  29. opts: defaults
  30. state: mounted

include

  1. cat svc_restart.yaml
  2. - name: Restart httpd server
  3. service: name=httpd state=restarted
  4. - hosts: web
  5. tasks:
  6. - name: cmd
  7. command: echo "a"
  8. - name: restart httpd
  9. include: svc_restart.yaml

change_when

  1. - hosts: web
  2. tasks:
  3. - name: install
  4. yum: name=httpd state=present
  5. - name: start
  6. service: name=httpd state=started
  7. - name:
  8. template: src=./httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
  9. notify: server restart httpd
  10. - name: check status
  11. command: /usr/sbin/httpd -t
  12. register: check_httpd
  13. changed_when:
  14. - ( check_httpd.stdout.find('ok'))
  15. - false
  16. #notify: server nginx httpd -可以通知多个,配置发生变动通知
  17. #触发
  18. handlers:
  19. - name: server restart httpd
  20. service: name=httpd state=retarted

jinra

  1. #结合facts变量
  2. # cat memcached.j2
  3. PORT="11211"
  4. USER="memcached"
  5. MAXCONN="1024"
  6. CACHESIZE="{{ ansible_memtotal_mb//2 }}"
  7. OPTIONS=""
  8. #jinra
  9. #if
  10. {% if ansible_fqdn == "web01" %}
  11. echo "123"
  12. {% if ansible_fqdn == "web02" %}
  13. echo "456"
  14. {% endif %}
  15. #for
  16. {% for in range{1,10} %}
  17. echo $i
  18. {% endfor %}
  19. #1)主机变量
  20. host_vars/192.168.203.129
  21. state: MASTER
  22. pri: 150
  23. host_vars/192.168.203.130
  24. state: BACKUP
  25. pri: 100
  26. #2) conf.j2
  27. router_id ansible_fqdn
  28. 3)分发
  29. - hosts: lb
  30. tasks:
  31. - name: conf
  32. template: src=./keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf
  33. when: ( ansible_fqdn == "web01" )
  34. notify: restart keepalived
  35. - name: restart keepalived
  36. template: src=./keepalived-slave.conf.j2 dest=/etc/keepalived/keepalived.conf
  37. when: ( ansible_fqdn == "web02" )
  38. notify: restart keepalived
  39. handlers:
  40. - name: restart keepalived
  41. service: name=keepalived state=restarted
  42. jinra实现keepalived渲染配置:
  43. router_id ansible_fqdn
  44. {% if ansible_fqdn == "web01" %}
  45. state MASTER
  46. priority 150
  47. {% endif ansible_fqdn == "web02" %}
  48. state BACKUP
  49. priority 100
  50. {% endif %}

roles

[root@dba project2]# tree .
.
├── hosts
├── memcached
│ ├── files
│ ├── handlers
│ │ └── main.yaml
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ │ └── memcached.j2
│ └── vars
├── site.yaml

  1. #tasks/main.yml
  2. - name: install memcached
  3. yum: name=memcached state=present
  4. - name: config
  5. template: src=memcached.j2 dest=/etc/sysconfig/memcached
  6. notify: restart memcache
  7. #handlers/main.yaml
  8. - name: start
  9. service: name=memcached state=started enabled=yes
  10. cat handlers/main.yaml
  11. - name: restart memcache
  12. service: name=memcached state=restarted
  13. #memcached.j2
  14. [root@dba memcached]# cat templates/memcached.j2
  15. PORT="11211"
  16. USER="memcached"
  17. MAXCONN="1024"
  18. CACHESIZE="{{ ansible_memtotal_mb//2 }}"
  19. OPTIONS=""
  20. #site.yaml
  21. - hosts: db
  22. roles:
  23. - memcached

galaxy

  1. ansible-galaxy init test
  2. [root@dba test]# tree .
  3. .
  4. ├── defaults
  5. └── main.yml
  6. ├── files
  7. ├── handlers
  8. └── main.yml
  9. ├── meta
  10. └── main.yml
  11. ├── README.md
  12. ├── tasks
  13. └── main.yml
  14. ├── templates
  15. ├── tests
  16. ├── inventory
  17. └── test.yml
  18. └── vars
  19. └── main.yml

综合项目

lb01 192.168.203.130 nginx+keepalived
lb02 192.168.203.151 nginx+keepalived
web01 192.168.203.131 nginx+php
web02 192.168.203.132 nginx+php
backup 192.168.203.133 rsync-server
nfs 192.168.203.134
db 192.168.203.135 mysql+ redis

基础环境

  1. #配置ssh免密
  2. #配置host文件
  3. cat hosts
  4. [web]
  5. 192.168.203.131
  6. 192.168.203.132
  7. [db]
  8. 192.168.203.135
  9. [lb]
  10. 192.168.203.130
  11. 192.168.203.151
  12. [nfs]
  13. 192.168.203.134
  14. [backup]
  15. 192.168.203.133
  16. #前置环境
  17. mkdir base/{tasks,handlers,templates} -pv
  18. cat base/tasks/main.yaml
  19. #disable
  20. - name: disable firewall
  21. service: name=firewalld state=stopped enabled=no
  22. - name: disble selinux
  23. selinux: state=disabled
  24. #add user
  25. - name: add {{ web_user }} {{ web_user_id }} group
  26. group: name={{ web_user }} gid={{ web_user_id }}
  27. - name: add user
  28. user: name={{ web_user }} uid={{ web_user_id }} group={{ web_user }}
  29. #add repo
  30. - name: add base yum repo
  31. yum_repository:
  32. name: base
  33. description: Base aliyun repo
  34. baseurl: https://mirrors.aliyun.com/centos/$releasever/os/$basearch/
  35. gpgcheck: no
  36. enabled: yes
  37. - name: add epel yum repo
  38. yum_repository:
  39. name: epel
  40. description: EPEL aliyun repo
  41. baseurl: https://mirrors.aliyun.com/epel/7/$basearch/
  42. gpgcheck: no
  43. enabled: yes
  44. - name: add nginx yum repo
  45. yum_repository:
  46. name: nginx
  47. description: nginx repo
  48. baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
  49. gpgcheck: no
  50. enabled: yes
  51. when: ( ansible_hostname is match ("web*")) or
  52. ( ansible_hostname is match ("lb*"))
  53. - name: add php yum repo
  54. yum_repository:
  55. name: php
  56. description: php repo
  57. baseurl: https://us-east.repo.webtatic.com/yum/el7/x86_64/
  58. gpgcheck: no
  59. enabled: yes
  60. when: ( ansible_hostname is match ("web*"))
  61. - name: install pks
  62. yum: name={{ packages }} state=present
  63. vars:
  64. packages:
  65. - rsync
  66. - nfs-utils
  67. - net-tools
  68. - wget
  69. - tree
  70. - lrzsz
  71. - vim
  72. - unzip
  73. - httpd-tools
  74. - bash-completion
  75. - iotop
  76. #变量
  77. [root@dba project3]# cat group_vars/all
  78. web_user: www
  79. web_user_id: 666

nginx

[root@ans-mgr roles]# mkdir nginx/{tasks,handlers,templates} -pv

  1. ##基础模块nginx/
  2. #templates/nginx.conf.j2
  3. #修改nginx配置的如下部分其他地方不变
  4. user {{ web_user }};
  5. worker_processes {{ ansible_processor_cores }};
  6. events {
  7. worker_connections {{ ansible_processor_cores * 2048 }};
  8. }
  9. #tasks/main.yaml
  10. - name: install nginx
  11. yum: name=nginx state=present
  12. - name: config nginx
  13. template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
  14. notify: restart nginx service
  15. - name: check status
  16. command: /usr/sbin/nginx -t
  17. register: check_nginx
  18. changed_when:
  19. - ( check_nginx.stdout.find('successfully'))
  20. - name: start nginx
  21. service: name=nginx state=started
  22. #handlers/main.yaml
  23. - name: restart nginx service
  24. service: name=nginx state=restarted
  25. #变量
  26. [root@dba project3]# cat group_vars/all
  27. web_user: www
  28. web_user_id: 666
  29. #site
  30. [root@dba project3]# cat site.yaml
  31. - hosts: all
  32. roles:
  33. - base
  34. - hosts: web
  35. roles:
  36. - role: nginx
  37. tags: nginx
  38. #运行
  39. ansible-playbook site.yaml -i hosts -t nginx

php

  1. - name: install php
  2. yum: name={{ item }} state=present
  3. with_items:
  4. - php71w
  5. - php71w-cli
  6. - php71w-common
  7. - php71w-devel
  8. - php71w-embedded
  9. - php71w-gd
  10. - php71w-mcrypt
  11. - php71w-mbstring
  12. - php71w-pdo
  13. - php71w-xml
  14. - php71w-fpm
  15. - php71w-mysqlnd
  16. - php71w-opcache
  17. - php71w-pecl-memcached
  18. - php71w-pecl-redis
  19. - php71w-pecl-mongodb
  20. - name: config php
  21. template: src={{ item.src }} dest={{ item.dest }} mode={{ item.mode }}
  22. with_items:
  23. - { src: 'php.ini.j2', dest: '/etc/php.ini', mode: '0644'}
  24. - { src: 'www.conf.j2', dest: '/etc/php-fpm.d/www.conf', mode: '0644'}
  25. notify: restart nginx service
  26. - name: check status
  27. command: /usr/sbin/nginx -t
  28. register: check_nginx
  29. changed_when:
  30. - ( check_nginx.stdout.find('successfully'))
  31. - name: start php
  32. service: name=php-fpm state=started
  33. #handlers/main.yaml
  34. - name: restart php service
  35. service: name=php-fpm state=restarted
  36. #j2
  37. [root@ans-mgr php]# cat templates/php.ini.j2 | grep server_
  38. session.save_path = "tcp://{{ redis_server_ip }}:{{ redis_server_port }}"
  39. #group_vars/all
  40. redis_server_ip: 192.168.203.135
  41. redis_server_port: 6379
  42. [root@ans-mgr roles]# cat site.yaml
  43. - hosts: all
  44. roles:
  45. - base
  46. - hosts: web
  47. roles:
  48. - role: nginx
  49. - role: php
  50. tags: web

nfs

  1. #nfs
  2. #客户端
  3. #准备目录
  4. #挂载共享目录
  5. #task/main.yaml
  6. - name: install nfs
  7. yum: name=nfs-utils state=present
  8. - name: config nfs
  9. template: src=exports.j2 dest=/etc/exports
  10. notify: restart nfs
  11. - name: nfs group
  12. group: name={{ nfs_user }} gid={{ nfs_user_id }}
  13. - name: nfs user
  14. user: name={{ nfs_user }} uid={{ nfs_user_id }} group={{ nfs_user }} shell=/sbin/nologin create_home=no
  15. - name: nfs share directory
  16. file: path={{ nfs_dir }} state=directory owner={{ nfs_user }} group={{ nfs_user }} recurse=yes
  17. - name: service start
  18. service: name=nfs state=started enabled=yes
  19. ###挂载
  20. - hosts: web
  21. gather_facts: no
  22. tasks:
  23. - name: client data
  24. file: path=/nfs_tt state=directory
  25. - name: client mount
  26. mount:
  27. src: 192.168.203.131:/data
  28. path: /nfs_tt
  29. fstype: nfs
  30. opts: defaults
  31. state: mounted
  32. #handlers/main.yaml
  33. - name: restart nfs
  34. service: name=nfs state=restarted
  35. #all
  36. web_user: www
  37. web_user_id: 666
  38. nfs_user: nfs
  39. nfs_user_id: 888
  40. nfs_dir: /data
  41. nfs_share_ip: 192.168.203.0/24
  42. #j2 exports
  43. {{ nfs_dir }} {{ nfs_share_ip }}(rw,sync,all_squash,anonuid={{ nfs_user_id }},anongid={{ nfs_user_id }})
  44. #site.yaml
  45. - hosts: all
  46. roles:
  47. - base
  48. - hosts: web
  49. roles:
  50. - role: nginx
  51. tags: nginx
  52. - hosts: nfs
  53. roles:
  54. - role: nfs
  55. tags: nfs



redis

  1. #redis
  2. - name: install redis
  3. yum: name=reids-server state=present
  4. - name: config redis
  5. template: src=redis.conf.j2 dest=/etc/redis.conf
  6. notify: restart redis
  7. - name: service start
  8. service: name=redis state=started enabled=yes
  9. #handlers/main.yaml
  10. - name: restart redis
  11. service: name=redis state=restarted
  12. #j2
  13. bindip {{ ansible_default_ipv4.address }}
  14. #
  15. - hosts: db
  16. roles:
  17. - role: redis
  18. tags: redis

mysql

  1. #mysql
  2. Ansible 角色:安装mysql(简易版)
  3. 基于ansible一键部署mysql的角色,自动安装mysql-python 方便后期使用mysql相关模块便于操作,并且设置默认root账号登录密码
  4. 一、版本要求
  5. 被控节点:Centos7
  6. 控制节点:Ansible2.9
  7. (控制节点其他版本的Ansible没有测试过)
  8. mysql 版本:mysql57-community-release-el7-9
  9. #前提做好yum源和ssh免密
  10. 二、如何使用
  11. 1、进入ansible默认角色目录
  12. cd /etc/ansible/roles
  13. 1
  14. 如果在ansible.cfg中更改了默认的角色目录,根据你自己角色目录进行更改
  15. 2、创建一个角色
  16. ansible-galaxy init mysql_install
  17. 5、主机清单
  18. [root@dba project4]# cat hosts
  19. [db]
  20. 192.168.203.135
  21. #结构
  22. [root@dba mysql_install]# tree .
  23. defaults
  24. └─main.yml  
  25. files
  26. ├─change_root_passwd.sh
  27. └─mysql57-community-release-el7-9.noarch.rpm
  28. handlers
  29. meta
  30. tasks
  31. ├─change_root_password.yml
  32. ├─install.yml
  33. ├─main.yml
  34. └─mysql-py_install.yml
  35. templates
  36. tests
  37. └─test.yml
  38. vars
  39. 三、实例剧本
  40. ---
  41. - hosts: db
  42. roles:
  43. - mysql_install
  44. vars:
  45. #一键部署,设置登录root密码
  46. mysql_passwd: "123456"
  47. 8
  48. 四、角色详解
  49. defaults
  50. └─main.yml  
  51. files
  52. ├─change_root_passwd.sh
  53. handlers
  54. meta
  55. tasks
  56. ├─change_root_password.yml
  57. ├─install.yml
  58. ├─main.yml
  59. └─mysql-py_install.yml
  60. templates
  61. tests
  62. └─test.yml
  63. vars
  64. 2、任务
  65. 主任务
  66. main.yml
  67. ---
  68. # tasks file for mysql_install
  69. #剧本执行顺序
  70. - include: install.yml
  71. - include: mysql-py_install.yml
  72. - include: change_root_password.yml
  73. 安装任务
  74. install.yml
  75. ---
  76. #创建临时文件夹任务
  77. - name: create directory
  78. file:
  79. path: "{{ mysql_temp_path }}"
  80. state: directory
  81. #复制rpm安装包任务
  82. - name: copy rpm package
  83. copy:
  84. src: "{{ rpm_package_name }}"
  85. dest: "{{ mysql_temp_path }}"
  86. #安装本地rpm安装包
  87. - name: install rpm
  88. yum:
  89. name: "{{ rpm_package_location }}"
  90. state: present
  91. #安装mysql-server
  92. - name: install mysql server
  93. yum:
  94. name: mysql-server
  95. state: present
  96. #启动mysql
  97. - name: start mysql
  98. service:
  99. name: mysqld
  100. state: started
  101. 远程主机安装mysql-python模块任务
  102. mysql-py_install.yml
  103. ---
  104. #安装epel扩展源任务
  105. - name: install Extended source
  106. yum:
  107. name: epel-release
  108. state: present
  109. #安装依赖包
  110. - name: install mysql-python dependency packages
  111. yum:
  112. name: "{{ dependency_packages }}"
  113. state: present
  114. #安装mysql模块
  115. - name: install mysql-python module
  116. pip:
  117. name: mysql-python
  118. 更改root密码任务
  119. change_root_password.yml
  120. ---
  121. #找到安装完成之后的临时密码任务
  122. - name: find temp passwd
  123. shell: "{{ find_temp_passwd_code }}"
  124. register: results
  125. tags: passwd
  126. #复制脚本文件任务
  127. - name: copy script file
  128. copy:
  129. src: change_root_passwd.sh
  130. dest: "{{ change_passwd_sh }}"
  131. tags: passwd
  132. #为什么不直接使用script模块:
  133. # 脚本中有变量,如果使用script模块,参数不可以在ansible服务器上传入脚本
  134. #所以使用copy模块复制脚本到远程主机之后,使用lineinfile模块替换掉脚本中的
  135. #变量。
  136. #用临时密码的变量替换脚本中的临时密码变量任务
  137. - name: send temp root password to shell file
  138. lineinfile:
  139. path: "{{ change_passwd_sh }}"
  140. regexp: '^PASSWORD='
  141. line: "PASSWORD={{ temp_password }}"
  142. tags: passwd
  143. #用主任务定义的新密码的变量替换脚本中的新密码变量任务
  144. - name: send new root password to shell file
  145. lineinfile:
  146. path: "{{ change_passwd_sh }}"
  147. regexp: '^New_Pass='
  148. line: "New_Pass={{ new_pass }}"
  149. tags: passwd
  150. #跑脚本任务
  151. - name: change root password
  152. shell: "sh {{ change_passwd_sh }}"
  153. tags: passwd
  154. 3、变量
  155. ---
  156. # defaults file for mysql_install
  157. #剧本变量
  158. #mysql临时文件夹
  159. mysql_temp_path: "/etc/tmp/mysql"
  160. #rpm包名称
  161. rpm_package_name: "mysql57-community-release-el7-9.noarch.rpm"
  162. #rpm包位置
  163. rpm_package_location: "{{ mysql_temp_path }}/{{ rpm_package_name }}"
  164. #找到临时文件的命令
  165. find_temp_passwd_code: "grep 'temporary password' /var/log/mysqld.log"
  166. #更换root密码脚本
  167. change_passwd_sh: "{{ mysql_temp_path }}/change_root_passwd.sh"
  168. #临时密码,取值为找到临时文件命令输出切片
  169. temp_password: "{{ results.stdout[-14:] }}"
  170. #新密码,在主任务do.yml中定义
  171. new_pass: "{{ mysql_passwd }}"
  172. #依赖包
  173. dependency_packages:
  174. - mysql
  175. - mysql-devel
  176. - python-devel
  177. - python-pip
  178. 4、脚本
  179. 更改密码脚本
  180. change_root_passwd.sh
  181. #!/bin/bash
  182. Host=127.0.0.1
  183. User=root
  184. PASSWORD=
  185. PORT=3306
  186. New_Pass=
  187. mysql -u$User -p'$PASSWORD' --connect-expired-password <<EOF
  188. set global validate_password_policy=LOW;
  189. set global validate_password_length=6;
  190. set password =password("$New_Pass");
  191. EOF
  192. ————————————————
  193. 版权声明:本文为CSDN博主「2huxy」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
  194. 原文链接:https://blog.csdn.net/qq_42267013/article/details/115368911

mysql 这里一直报错误密码不对 但是日志显示已经对了,没解决

网上看到的https://www.5axxw.com/questions/content/6027sc

  1. --
  2. - name: root | stat to check whether /root/.my.cnf exists
  3. stat:
  4. path: /root/.my.cnf
  5. register: cnf_file
  6. - block:
  7. - name: root | place temporary cnf file
  8. template:
  9. src: temp_cnf.j2
  10. dest: /etc/my.cnf
  11. mode: '0644'
  12. - name: root | start mysql to add the debian-sys-maint user
  13. systemd:
  14. name: mysql
  15. state: started
  16. enabled: true
  17. - name: root | get temp root password
  18. shell: >-
  19. grep 'temporary password' /var/log/mysqld.log |
  20. awk '{print $NF}' | tail -n 1
  21. register: temp_root_pw
  22. no_log: true
  23. - name: root | set root password
  24. shell: >-
  25. mysqladmin -u root
  26. --password="{{ temp_root_pw.stdout }}"
  27. password "{{ mysql_root_password }}"
  28. no_log: true
  29. - name: root | set debian-sys-maint user and password
  30. mysql_user:
  31. name: debian-sys-maint
  32. password: "{{ mysql_system_password }}"
  33. priv: '*.*:ALL,GRANT'
  34. update_password: always
  35. state: present
  36. login_unix_socket: /var/run/mysqld/mysqld.sock
  37. login_user: root
  38. login_password: "{{ mysql_root_password }}"
  39. no_log: true
  40. - name: root | copy root.cnf
  41. template:
  42. src: root.cnf.j2
  43. dest: /etc/mysql/root.cnf
  44. mode: '0600'
  45. owner: root
  46. group: root
  47. - name: root | make symlink of file for root db access
  48. file:
  49. state: link
  50. src: /etc/mysql/root.cnf
  51. path: /root/.my.cnf
  52. - name: root | delete anonymous connections
  53. mysql_user:
  54. name: ""
  55. host_all: true
  56. state: absent
  57. no_log: true
  58. - name: root | secure root user
  59. mysql_user:
  60. name: root
  61. host: "{{ item }}"
  62. no_log: true
  63. loop:
  64. - ::1
  65. - 127.0.0.1
  66. - localhost
  67. - name: root | ensure test database is removed
  68. mysql_db:
  69. name: test
  70. login_user: root
  71. state: absent
  72. - name: root | stop mysql again
  73. systemd:
  74. name: mysql
  75. state: stopped
  76. enabled: true
  77. - name: root | remove mysqld log file
  78. file:
  79. path: /var/log/mysqld.log
  80. state: absent
  81. when: not cnf_file.stat.exists
  82. The temp_cnf.j2:
  83. [client]
  84. socket=/var/run/mysqld/mysqld.sock
  85. [mysqld]
  86. server-id=1
  87. datadir=/var/lib/mysql
  88. socket=/var/run/mysqld/mysqld.sock
  89. log-error=/var/log/mysqld.log
  90. pid-file=/var/run/mysqld/mysqld.pid
  91. 以及root.cnf.j2
  92. # {{ ansible_managed }}
  93. # This file is symlinked to /root/.my.cnf to use passwordless login for the root user
  94. [client]
  95. socket = {{ mysqld.socket }}
  96. user = debian-sys-maint
  97. password = {{ percona_system_password }}
  98. [mysql_upgrade]
  99. socket = {{ mysqld.socket }}
  100. user = debian-sys-maint
  101. password = {{ percona_system_password }}
  102. Some vars:
  103. mysql_root_password: my_password
  104. mysql_system_password: my_password
  105. mysqld:
  106. socket: /var/run/mysqld/mysqld.sock
  107. 应该适用于CentOS 8Rocky LinuxOracle Linux

结合项目: shell脚本调用的方式还是没有解决,直接不调了

  1. cat mysql_install/tasks/main.yml
  2. # tasks file for mysql_install
  3. #创建临时文件夹任务
  4. - name: create directory
  5. file:
  6. path: "{{ mysql_temp_path }}"
  7. state: directory
  8. #复制rpm安装包任务
  9. # - name: copy rpm package
  10. # copy:
  11. # src: "{{rpm_package_name}}"
  12. # dest: "{{mysql_temp_path}}"
  13. #安装本地rpm安装包
  14. # - name: install rpm
  15. # yum:
  16. # name: "{{rpm_package_location}}"
  17. # state: present
  18. #安装mysql-server
  19. - name: install mysql server
  20. yum:
  21. name: mysql-server
  22. state: present
  23. #启动mysql
  24. - name: start mysql
  25. service:
  26. name: mysqld
  27. state: started
  28. # - name: install Extended source
  29. # yum:
  30. # name: epel-release
  31. # state: present
  32. #安装依赖包
  33. - name: install mysql-python dependency packages
  34. yum:
  35. name: "{{ dependency_packages }}"
  36. state: present
  37. #安装mysql模块
  38. - name: install mysql-python module
  39. pip:
  40. name: mysql-python
  41. # - name: install Extended source
  42. # yum:
  43. # name: epel-release
  44. # state: present
  45. #安装依赖包
  46. - name: install mysql-python dependency packages
  47. yum:
  48. name: "{{ dependency_packages }}"
  49. state: present
  50. #安装mysql模块
  51. - name: install mysql-python module
  52. pip:
  53. name: mysql-python
  54. - name: root | get temp root password
  55. shell: grep 'temporary password' /var/log/mysqld.log |awk '{print $NF}' | tail -n 1
  56. register: temp_root_pw
  57. no_log: true
  58. - name: root | set root password
  59. shell: mysqladmin -u root --password="{{ temp_root_pw.stdout }}" password "{{ mysql_passwd }}"
  60. no_log: true
  61. - name: root | secure root user
  62. mysql_user:
  63. name: root
  64. host: "{{ item }}"
  65. no_log: true
  66. loop:
  67. - '%'
  68. - 127.0.0.1
  69. - localhost
  1. #mysql模块
  2. #base
  3. # Enable to use MySQL 5.7
  4. - name: add mysql yum repo
  5. yum_repository:
  6. name: mysql
  7. description: mysql repo
  8. baseurl: http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
  9. gpgcheck: no
  10. enabled: yes
  11. when: ( ansible_hostname is match ("db*"))
  12. #mysql
  13. - name: install mysql
  14. yum:
  15. name: ['mysql-server', 'MySQL-python']
  16. state: present
  17. - name: config mysql
  18. template: src=my.cnf.j2 dest=/etc/my.cnf backup=yes
  19. notify: restart mysql
  20. - name: service start
  21. service: name=mysqld state=started enabled=yes
  22. #handlers/main.yaml
  23. - name: restart mysql
  24. service: name=mysqld state=restarted
  25. - name: create database
  26. mysql_db:
  27. name: wordpress
  28. state: present
  29. login_user: root
  30. login_password: "{{ mysql_pass }}"
  31. - name: create mysql user/pass grant
  32. mysql_user:
  33. name: "{{ db_user }}"
  34. password: "{{ db_pass }}"
  35. priv: '*.*:ALL'
  36. host: '%'
  37. state: present
  38. login_user: root
  39. login_password: "{{ mysql_passwd }}"
  40. #group/all 变量
  41. #db
  42. db_user: lc
  43. db_pass: 123456
  44. mysql_pass: 123456