ansible--免密

目录

ssh免密登录

authorized_key模块实现


ssh免密登录

第一步:

在ansible服务端创建密匙:ssh-keygen 

注意:不断回车即可生成密钥

[root@httpd 10.10.10.3]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
01:f0:5a:ef:8f:72:f4:77:b5:f3:37:08:6c:a6:f2:32 root@httpd.test.com
The key's randomart image is:
+--[ RSA 2048]----+
|    ...          |
|     . .         |
|      o .        |
|     o . .       |
|    .   S .      |
|       ..  =    .|
|       ...+ . ...|
|      .E.+. ...+.|
|       o*... .  *|
+-----------------+
[root@httpd 10.10.10.3]# ll /root/.ssh/
-rw-------. 1 root root 1675 Jun 25 13:45 id_rsa
-rw-r--r--. 1 root root  401 Jun 25 13:45 id_rsa.pub

第二步:

复制密钥到客户端上:ssh-copy-id 用户名@主机

[root@httpd 10.10.10.3]# ssh-copy-id root@10.10.10.2
The authenticity of host '10.10.10.2 (10.10.10.2)' can't be established.
ECDSA key fingerprint is 37:f4:17:5a:12:df:eb:4e:46:b8:d9:3e:0d:93:99:4e.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.10.10.2's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@10.10.10.2'"
and check to make sure that only the key(s) you wanted were added.

[root@httpd 10.10.10.3]# ssh root@10.10.10.2
Last login: Fri Jun 25 13:21:10 2021 from 10.10.10.3
[root@ansible ~]# ls .ssh/
authorized_keys

authorized_key模块实现

第一步

关闭公钥认证(如果有个主机没有在“known_hosts”中被初始化将会导致在交互使用Ansible或定时执行Ansible时对key信息的确认提示,如下图)

sed -i "s/^#host_key_checking=/host_key_checking = False/" /etc/ansible/ansible.cfg

/etc/ansible/ansible.cfg
# uncomment this to disable SSH key host checking
host_key_checking = False

第二步

在ansible服务端创建密匙:ssh-keygen 

注意:不断回车即可生成密钥

第三步

复制密钥到客户端上,并重命名

 ansible all -m authorized_key -a "user=root key='{{ lookup('file', '/root/.ssh/id_rsa.pub')}}'" -k

[root@ansible ~]# ansible all -m authorized_key -a "user=root key='{{ lookup('file', '/root/.ssh/id_rsa.pub')}}'" -k 
SSH password: 
10.10.10.4 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": null, 
    "exclusive": false, 
    "follow": false, 
    "key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDpME9Xik+jjAdN8zz8BUG5uEKAp04DWIonUcTmWtxhhmrsj+Ve/RmoQljBac+UGg82T59qMnzPz9AtlG0A74vFbXjiyTHhfqYJVxHemhDSFH3ennlXiymacyWanNbJYZbDvNyLl1RIktbF+10/l0cGFK0loJF49RUuOmwgvzJ1KRUDJAaAffYiOxCJY/QJzwy6sJ805XfALT5nIro7C/2q3kWaBw2J4KNzdCluHw/AhILV+WsdRNNg1zV1/akR7+ZWCrL0vejvAi2IaIK3ix+SOU5I61XwvCDaZkE3uVNYf6ERoFcV1L6qOzR1nGefOWrgqUaLOjcBp0bxr048cO6v root@ansible.test.com", 
    "key_options": null, 
    "keyfile": "/root/.ssh/authorized_keys", 
    "manage_dir": true, 
    "path": null, 
    "state": "present", 
    "user": "root", 
    "validate_certs": true
}
10.10.10.3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": null, 
    "exclusive": false, 
    "follow": false, 
    "key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDpME9Xik+jjAdN8zz8BUG5uEKAp04DWIonUcTmWtxhhmrsj+Ve/RmoQljBac+UGg82T59qMnzPz9AtlG0A74vFbXjiyTHhfqYJVxHemhDSFH3ennlXiymacyWanNbJYZbDvNyLl1RIktbF+10/l0cGFK0loJF49RUuOmwgvzJ1KRUDJAaAffYiOxCJY/QJzwy6sJ805XfALT5nIro7C/2q3kWaBw2J4KNzdCluHw/AhILV+WsdRNNg1zV1/akR7+ZWCrL0vejvAi2IaIK3ix+SOU5I61XwvCDaZkE3uVNYf6ERoFcV1L6qOzR1nGefOWrgqUaLOjcBp0bxr048cO6v root@ansible.test.com", 
    "key_options": null, 
    "keyfile": "/root/.ssh/authorized_keys", 
    "manage_dir": true, 
    "path": null, 
    "state": "present", 
    "user": "root", 
    "validate_certs": true
}
[root@ansible ~]# ssh root@10.10.10.3
Last login: Fri Jun 25 14:44:15 2021 from 10.10.10.2
[root@httpd ~]# exit
logout
Connection to 10.10.10.3 closed.
[root@ansible ~]# ssh root@10.10.10.4
Last login: Fri Jun 25 14:44:18 2021 from 10.10.10.2
[root@nginx ~]# 

authoried_keys模块参数

user:远程主机用户

key:ansible主机上pub密钥存放路径

path:远程主机authorized_keys的存放路径,默认~/.ssh

state:absent,默认present

exclusive:是否移除authorized_keys文件中其它非指定key,默认no

manage_dir: yes,模块会创建目录,以及设置一个已存在目录的拥有者和权限;no,搭配path使用

非常感谢您的提问,我将为您详细描述 ansible-varnish-nginx-php-fpm-ftp-mysql 的部署流程。 前置条件: - 安装 Ansible 工具 - 所有主机都配置好 SSH 免密登录 - 所有主机都已安装 Python 2.x 或 Python 3.x 步骤: 1. 编写 Ansible 配置文件 创建一个名为 inventory 的文件,并在其中定义要部署的主机列表和相关变量: ``` [web] webserver ansible_host=192.168.1.10 ansible_user=<your_user> ansible_ssh_private_key_file=<your_ssh_key> dbserver ansible_host=192.168.1.11 ansible_user=<your_user> ansible_ssh_private_key_file=<your_ssh_key> [web:vars] nginx_version=1.14.1 php_version=7.2 mysql_version=5.7 ``` 2. 编写 Ansible Playbook 创建一个名为 playbook.yml 的文件,并编写 Ansible Playbook。以下是一个示例 Playbook,包含安装和配置 varnish、nginx、PHP-FPM、FTP 和 MySQL。 ``` - hosts: web become: true vars_files: - vars.yml tasks: # 安装 Varnish - name: Install Varnish yum: name: varnish state: present # 安装 Nginx - name: Install Nginx yum: name: nginx state: present notify: - Restart Nginx # 配置 Nginx - name: Configure Nginx template: src: templates/nginx.conf.j2 dest: /etc/nginx/nginx.conf notify: - Restart Nginx # 安装 PHP-FPM - name: Install PHP-FPM yum: name: php-fpm state: present notify: - Restart PHP-FPM # 配置 PHP-FPM - name: Configure PHP-FPM template: src: templates/php-fpm.conf.j2 dest: /etc/php-fpm.conf notify: - Restart PHP-FPM # 安装 FTP - name: Install FTP yum: name: vsftpd state: present notify: - Restart FTP # 配置 FTP - name: Configure FTP template: src: templates/vsftpd.conf.j2 dest: /etc/vsftpd/vsftpd.conf notify: - Restart FTP # 安装 MySQL - name: Install MySQL yum: name: mysql-community-server state: present notify: - Start MySQL # 配置 MySQL - name: Configure MySQL template: src: templates/my.cnf.j2 dest: /etc/my.cnf notify: - Start MySQL handlers: # 重启 Nginx - name: Restart Nginx systemd: name: nginx state: restarted # 重启 PHP-FPM - name: Restart PHP-FPM systemd: name: php-fpm state: restarted # 重启 FTP - name: Restart FTP systemd: name: vsftpd state: restarted # 启动 MySQL - name: Start MySQL systemd: name: mysqld state: started ``` 3. 准备模板文件 创建模板文件,用于生成配置文件。以下是模板文件示例代码: nginx.conf.j2 ``` user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; events { worker_connections 1024; use epoll; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.php index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } include /etc/nginx/conf.d/*.conf; } ``` php-fpm.conf.j2 ``` [global] pid = /var/run/php-fpm/php-fpm.pid error_log = /var/log/php-fpm/error.log emergency_restart_threshold = 5 emergency_restart_interval = 1m process_control_timeout = 10 daemonize = no [www] user = nginx group = nginx listen = 127.0.0.1:9000 listen.owner = nginx listen.group = nginx listen.mode = 0660 pm = dynamic pm.max_children = 5 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3 pm.max_requests = 500 ``` vsftpd.conf.j2 ``` anonymous_enable=NO local_enable=YES write_enable=YES local_umask=022 dirmessage_enable=YES xferlog_enable=YES xferlog_file=/var/log/xferlog xferlog_std_format=YES chroot_local_user=YES allow_writeable_chroot=YES local_root=/var/www/html/ listen=YES listen_ipv6=NO pam_service_name=vsftpd userlist_enable=YES tcp_wrappers=YES ``` my.cnf.j2 ``` [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql symbolic-links=0 skip-external-locking key_buffer_size = 16M max_allowed_packet = 256M table_open_cache = 16384 sort_buffer_size = 512K net_buffer_length = 16K myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size = 16M query_cache_limit = 1M log-bin=mysql-bin binlog_format=ROW server-id=1 innodb_buffer_pool_size = 256M innodb_log_file_size = 64M default-storage-engine=innodb character-set-server=utf8mb4 collation-server=utf8mb4_unicode_ci [client] socket=/var/lib/mysql/mysql.sock [mysql] socket=/var/lib/mysql/mysql.sock ``` 4. 运行 Ansible Playbook 运行以下命令来运行 Ansible Playbook: ``` $ ansible-playbook -i inventory playbook.yml ``` 此命令将在指定主机上按顺序执行 Playbook 中的每个任务,安装和配置所有必需的软件包和服务,最终实现 ansible-varnish-nginx-php-fpm-ftp-mysql 的部署。 感谢您提供的问题,希望可以帮到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

金牌收租佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值