测试Ansible

创建一个inventory文件

$ vi inventory

inventory:

[lanhosts]
10.9.80.3
192.168.1.250

[alihosts]
aliyun-ubuntu

以用户itr的身份,在lanhosts中的两台主机上运行uptime命令和lsb_release命令:

$ ansible -u itr -i inventory -m raw -a 'uptime' lanhosts
$ ansible -u itr -i inventory -m raw -a 'lsb_release -a' lanhosts

密码安全

我们直接把密码放在了inventory文件。 现在修改一下

inventory:

## ansible_become_pass='passwd' ### itr的密码
ansible_become_pass='{{ my_user_password }}'

然后创建一个文件 passwords.yml:

$ ansible-vault create passwords.yml

设置密码后,会打开编辑器。在最后添加

my_user_password: 这里填写密码

保存后,运行

$ ansible-playbook -i inventory --ask-vault-pass --extra-vars '@passwords.yml' test.yml

在linux上设置ssh密钥

在Fedora上使用ssh-keygen命令创建密钥对

$ ssh-keygen -t itrssh -C "Desktop ssh key"

复制并安装公钥到远程Linux/Unix/BSD服务器上

$ ssh-copy-id -i $HOME/.ssh/id_itrssh.pub itr@aliyun-ubuntu
$ ssh-copy-id -i $HOME/.ssh/id_itrssh.pub itr@10.9.80.3
$ ssh-copy-id -i $HOME/.ssh/id_itrssh.pub itr@192.168.1.250

测试是否可用不用密码就能登录到服务器上

$ssh itr@192.168.1.250

使用Ansible playbook添加用户

假设我们要给所有lanhosts组的服务器都添加一个新用户nginx。 新建一个yml文件:

add-user.yml

---
- hosts: lanhosts
  tasks:
          - name: Add a new user to my Linux VMs with password disabled but allow ssh log in
            user:
                    name: nginx
                    comment: "Account to run jobs for our web server"
                    shell: /bin/bash
                    groups: sudo
                    append: yes
                    password: *
          - name: Upload ssh key for user wwwjobs for log in purpose
            authorized_key:
                    user: itr
                    state: present
                    manage_dir: yes
                    key: "{{ lookup('file', '/home/itr/.ssh/id_itrssh.pub') }}"

运行:

$ ansible-playbook -i inventory --ask-vault-pass --extra-vars '@passwords.yml' add-user.yml

编写 Ansible playbook 来管理服务器

修改inventory文件,配置sudo参数

[all:vars]
ansible_user='itr'
ansible_become='yes'
ansible_become_pass='passwd' ### itr的密码
ansible_become_method='sudo'

[lanhosts]
10.9.80.3
192.168.1.250

[alihosts]
aliyun-ubuntu

playbook其实就是在远程机器上执行的脚本/命令。创建一个名为test.yml的playbook,如下所示:

test.yml

---
- hosts: lanhosts
 
  tasks:
          - name: Get hostname for testing purpose
            command: /bin/hostname
            changed_when: False
            register: hostname
 
          - debug: var={{ item }}
            with_items:
                    - hostname.stdout

运行:

$ ansible-playbook -i inventory test.yml

如何使用ansible安装和删除包

我们将使用apt命令为lanhosts组中的所有主机添加和删除软件包。

创建一个ubuntu-pkg文件:

ubuntu-pkg.yml

---
- hosts: linodehosts
  tasks:
          - name: Add a list of software on VMs ...
            apt:
                    name: "{{ packages }}"
                    state: present
            vars:
                    packages:
                            - vim
                            - unzip
                            - htop
                            - atop
                            - iftop
                            - nmon
                            - sysstat
                            - iotop
                            - nicstat
                            - vnstat
          - name: Delete a list of software from VMs ...
            apt:
                    name: "{{ packages }}"
                    state: absent
            vars:
                    packages:
                            - nano

运行

$ ansible-playbook -i inventory --ask-vault-pass --extra-vars '@passwords.yml' ubuntu-software.yml

在Fedora Linux上安装Ansbile

$ sudo dnf info ansible
$ sudo dnf install ansible

安装后查看ansible版本

$ ansible --version

更新Fedora系统

$ sudo dnf update
$ sudo dnf search ansible
如何在Fedora上安装Ansible

Ansible是一个类似于Chef和Puppet的免费开源的配置管理工具。它可以在基于SSH的会话上工作,不需要在远程服务器上使用任何软件或客户机/代理。人们可以使用Ansible来管理Linux、Unix、macOS和BSD系列操作系统。

如何在Fedora安装ansible并配置ansible playbook?

日期:2019-04-29 03:18:06 来源:oir作者:oir