Ansible杂记

yum install epel-release
yum install ansible

实用命令

1 ansible在 a b c三台服务器上运行各自服务器上的deploy.sh

vim /etc/ansible/hosts

# myservers是主机组的名称,a.example.com、b.example.com和c.example.com是你的服务器的域名或IP地址
[myservers]
a.example.com
b.example.com
c.example.com

vim deploy.yml (这个文件就是playbook)

---
- name: Run deploy.sh on all servers
  hosts: myservers
  tasks:
    - name: Execute deploy.sh
      shell: /path/to/deploy.sh

ansible-playbook deploy.yml

都部署a服务器上面的deploy.sh,说白了就是先复制过去
---
- name: Run deploy.sh on all servers
  hosts: myservers
  tasks:
    - name: Copy deploy.sh
      copy:
        src: /local/path/to/deploy.sh
        dest: /remote/path/to/deploy.sh
        mode: 0755

    - name: Execute deploy.sh
      shell: /remote/path/to/deploy.sh

ansible 将服务器上的 /etc/docker/daemon.json 备份,将ansible主机上的/etc/docker/daemon.json copy到这些服务器上
- name: Copy daemon.json file to servers and backup existing one
  hosts: all
  become: true
  tasks:
    - name: Backup existing daemon.json file
      ansible.builtin.shell: mv /etc/docker/daemon.json /etc/docker/daemon.json.bak
      register: backup_result
      ignore_errors: true

    - name: Copy daemon.json file from local to remote server
      ansible.builtin.copy:
        src: /etc/docker/daemon.json
        dest: /etc/docker/daemon.json
        owner: root
        group: root
        mode: '0644'
      register: copy_result

    - name: Print backup and copy results
      ansible.builtin.debug:
        msg: |
          Backup result: {{ backup_result }}
          Copy result: {{ copy_result }}

ansible-playbook playbook.yml

下面是一个简单的Ansible playbook,它会在多台机器上运行几条shell命令:

在执行这个playbook之前,需要先确保目标机器已经安装了Ansible,并且可以通过SSH访问。执行命令ansible all -m ping可以测试SSH连接是否正常。

playbook.yml

---
- name: Run Shell Commands on Multiple Machines
  hosts: web_servers
  tasks:
    - name: Run shell commands
      shell: |
        echo "Hello World"
        ls /etc/
        df -h

说明:
name: playbook的名称;
hosts: 执行此playbook的目标机器组,可以在/etc/ansible/hosts文件中指定;
tasks: playbook要执行的任务列表;
name: 任务的名称;
shell: 要运行的shell命令,可以使用|符号将多条命令组合在一起。

要运行这个playbook,可以使用以下命令:

ansible-playbook playbook.yml

其中,playbook.yml是playbook的文件名。

以下是一个将/etc/ssh/sshd_config文件中的PermitRootLogin参数修改为no的Shell脚本:
#!/bin/bash

# 备份原始配置文件
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

# 将PermitRootLogin参数修改为no
sed -i 's/#PermitRootLogin.*/PermitRootLogin no/g' /etc/ssh/sshd_config

# 重新加载sshd服务
systemctl reload sshd.service

echo "PermitRootLogin参数已修改为no"

该脚本的具体步骤如下:

1、使用cp命令备份原始的/etc/ssh/sshd_config文件,将其复制为/etc/ssh/sshd_config.bak。
2、使用sed命令将PermitRootLogin参数修改为no。其中,#PermitRootLogin.*表示匹配以#PermitRootLogin开头的行,.*表示该行后面的任意字符。将该行替换为PermitRootLogin no即可。
3、使用systemctl命令重新加载sshd服务,以使修改生效。
4、输出提示信息,告知用户PermitRootLogin参数已修改为no。
请注意,在运行此脚本之前,您应该先以root用户身份登录到系统中。此外,建议您在运行脚本之前先手动备份/etc/ssh/sshd_config文件,以避免意外的数据丢失。

shell脚本 如何实现docker login 输入用户名和密码,比如用户名是admin, 密码是123456
#!/bin/bash

# Enter Docker Hub username and password
username="admin"
password="123456"

# Login to Docker Hub
echo "$password" | docker login --username "$username" --password-stdin

该脚本使用变量 $username 和 $password 存储 Docker Hub 账号的用户名和密码。然后,使用 echo 命令将密码传递给 docker login 命令,并使用 --password-stdin 选项告诉 docker login 命令从标准输入中读取密码。最后,docker login 命令将使用输入的用户名和密码进行身份验证,并在成功登录后输出一条消息。

请注意,在脚本中直接使用密码可能存在安全隐患,因为密码可以被轻松地泄露。因此,建议使用其他安全的方法来存储和传递密码,例如使用加密的配置文件或环境变量。

使用以下步骤将三台服务器的主机名设置为 master1、node1 和 node2:

1、确保您已经在 Ansible 控制机上设置好了 SSH 认证,并可以使用 SSH 连接到这三台服务器。
2、在 Ansible 控制机上创建一个名为 hosts 的文件,并将这三台服务器的 IP 地址添加到文件中:

[servers]
192.168.1.1
192.168.1.2
192.168.1.3

3、创建一个名为 set_hostname.yml 的 Ansible Playbook 文件,并编写以下内容:

- name: Set hostname for servers
  hosts: servers
  become: yes
  tasks:
    - name: Set hostname to master1 for server 192.168.1.1
      hostname:
        name: master1
      when: inventory_hostname == '192.168.1.1'
    - name: Set hostname to node1 for server 192.168.1.2
      hostname:
        name: node1
      when: inventory_hostname == '192.168.1.2'
    - name: Set hostname to node2 for server 192.168.1.3
      hostname:
        name: node2
      when: inventory_hostname == '192.168.1.3'

在此 Playbook 中,我们使用 hostname 模块将服务器的主机名设置为 master1、node1 和 node2。我们使用 when 关键字来指定每个任务应该在哪个服务器上执行,这是通过 inventory_hostname 变量来实现的。
4、在命令行中执行以下命令来运行 Playbook:

ansible-playbook -i hosts set_hostname.yml

Ansible 将自动在 hosts 文件中列出的所有服务器上运行 Playbook,并将主机名设置为 master1、node1 和 node2。完成后,您可以使用 ssh 命令验证每台服务器的主机名是否已更改。

用ansible的yaml 将 某文件传到所有server的指定目录,如果没有这个目录,则创建
- hosts: all
  become: true
  tasks:
    - name: Copy file to remote server
      copy:
        src: /path/to/local/file
        dest: /path/to/remote/directory/file
        mode: '0644'
        backup: 'yes'
        force: 'yes'
        directory_mode: '0755'
        follow: 'yes'

说明:

hosts: all:将任务应用于 Ansible inventory 中的所有主机。
become: true:使用超级用户或管理员权限执行任务,如果需要输入密码,Ansible 会提示您输入密码。
copy::指示 Ansible 将文件从本地复制到远程服务器。
src: /path/to/local/file:指定要复制的本地文件路径。
dest: /path/to/remote/directory/file:指定要将文件复制到的远程服务器目标目录路径。如果目录不存在,则 Ansible 会自动创建它。
mode: ‘0644’:指定要应用于远程文件的权限模式。
backup: ‘yes’:启用备份功能,如果目标文件已存在,则创建备份文件。
force: ‘yes’:如果目标文件已存在,则强制覆盖它。
directory_mode: ‘0755’:指定要应用于新创建的目录的权限模式。
follow: ‘yes’:如果要复制的文件是符号链接,则遵循它指向的文件。

将上述 YAML 代码保存为 playbook.yml 文件,然后使用以下命令运行它:

ansible-playbook playbook.yml

在 Ansible 的 copy 模块中,mode 参数用于指定要应用于目标文件的权限。例如,mode: ‘0644’ 表示将目标文件设置为所有者具有读和写权限,其他用户具有只读权限。类似地,mode: ‘0755’ 表示将目标目录设置为所有用户都具有读、写和执行权限。

ansible yaml脚本里面reboot,然后机器重启后继续执行yaml下面的语句

在 Ansible 的 YAML 脚本中,您可以使用 wait_for_connection 模块在重启后等待 SSH 连接恢复,然后再继续执行下面的任务。这可以确保重启后的服务器已经准备好接收 Ansible 的命令。

以下是在 Ansible 的 YAML 脚本中使用 wait_for_connection 模块等待 SSH 连接恢复的示例:

- hosts: all
  become: true
  tasks:
    - name: Reboot server
      reboot:
        reboot_timeout: 300
        pre_reboot_delay: 0
        post_reboot_delay: 0
      async: 1
      poll: 0
      ignore_errors: true

    - name: Wait for server to reboot
      wait_for_connection:
        delay: 10
        timeout: 300

    - name: Continue with tasks after reboot
      # Add your tasks here

在上述 YAML 中,reboot 模块用于重启远程服务器。 async: 1 和 poll: 0 选项将使 Ansible 异步执行此任务并立即返回控制权,而不是等待重启完成。 ignore_errors: true 选项将忽略重启期间可能出现的任何错误。

wait_for_connection 模块用于等待 SSH 连接恢复。delay: 10 表示每 10 秒检查一次连接是否可用,timeout: 300 表示最长等待时间为 300 秒。

在 SSH 连接恢复后,Ansible 将继续执行下面的任务,即 Continue with tasks after reboot。您可以在此任务中添加您想要执行的其他任务。

ansible把文件夹包括里面的文件和子文件夹copy到所有server

把所有要下载的东西,提前下载到起源机上,包括驱动runfile

- name: Copy directory to remote hosts
  hosts: all
  tasks:
    - name: Copy directory
      ansible.builtin.copy:
        src: /path/to/source/directory
        dest: /path/to/destination/directory
        recursive: yes

在这个例子中,src参数指定要复制的源目录,dest参数指定要复制到的目标目录。recursive参数用于递归地复制目录中的所有文件和子目录

任务失败后你需要怎么停止ansible

max_fail_percentage参数用于控制在出现多少任务失败的情况下,Ansible将停止执行Playbook。默认情况下,max_fail_percentage参数设置为0,这意味着只要有任何任务失败,Ansible将停止执行。

如果您希望在出现一些失败任务的情况下继续执行Playbook,则可以将max_fail_percentage参数设置为一个大于0的值。例如,如果将max_fail_percentage设置为20,则在出现20%或更多的失败任务时,Ansible将停止执行。

以下是一个使用max_fail_percentage参数的示例Playbook:

- name: Example Playbook
  hosts: all
  max_fail_percentage: 50
  tasks:
    - name: Task 1
      ansible.builtin.command: some_command
    - name: Task 2
      ansible.builtin.command: another_command
    - name: Task 3
      ansible.builtin.command: yet_another_command

在这个例子中,max_fail_percentage参数设置为50,这意味着在出现50%或更多的失败任务时,Ansible将停止执行。

需要注意的是,max_fail_percentage参数仅适用于任务级别的失败,而不是主机级别的失败。也就是说,如果一个任务在某些主机上失败,但在其他主机上成功,那么这个任务仍然会被视为成功。如果要控制主机级别的失败率,可以使用max_failures参数。

ansible根据不同的主机名执行不同的shell语句
- name: Execute shell commands based on hostname
  hosts: all
  tasks:
    - name: Task 1
      ansible.builtin.shell: echo "This is host 1"
      when: ansible_hostname == 'host1'

    - name: Task 2
      ansible.builtin.shell: echo "This is host 2"
      when: ansible_hostname == 'host2'

在这个例子中,如果当前主机的名称为host1,则将执行第一个任务,打印"This is host 1";如果当前主机的名称为host2,则将执行第二个任务,打印"This is host 2"。如果主机名不是host1或host2,则不执行任何任务。

需要注意的是,在使用when关键字时,需要使用正确的语法和引号。如果条件包含字符串,必须将其用单引号或双引号括起来,例如ansible_hostname == ‘host1’ 或 ansible_hostname== “host1”。如果条件包含布尔值或数字,不需要引号,例如some_var == true或some_var > 5。

Regenerate response

以前的烂脚本
yum -y install epel-release
yum -y install ansible

-become 提权
# copy 分发文件  
ansible nodes  -m copy -a "src=hosts dest=/root/hosts"
# shell 命令  
ansible nodes -i hosts -m shell -a "ls /root"
# 执行命令集 
ansible-playbook test.yml
# 执行sh脚本
ansible asb -m shell  -a "sh /home/wfq/playbook/pb_shell.sh chdir=/home/wfq/playbook"

问题

1、Failed to create temporary directory.In some cases, you may have been able to authenticate and did not have permissions on the target directory. Consider changing the remote tmp path in ansible.cfg to a path rooted in “/tmp”

将配置文件中remote_tmp = ~/.ansible/tmp修改 remote_tmp = /tmp/.ansible/tmp,这里主要是权限问题

2、 {
“msg”: “Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host’s fingerprint to your known_hosts file to manage this host.”
}
vi /etc/ansible/ansible.cfg
host_key_checking = False

3、REMOTE HOST IDENTIFICATION HAS CHANGED!
ssh-keygen -R 接收方ip

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值