How to use [ansible] / 怎样使用ansible

用户bPcOcF9

image.png

Tested on CentoS7.

Install [ansible]

First of all, you need to add [epel] yum repo,

yum install epel-release

Or you can use the 阿里云 epel repo, edit /etc/yum.repos.d/aliyun-epel.repo

[aliyun-epel-7]
name = aliyun epel 7
baseurl = http://mirrors.aliyun.com/epel/7/x86_64/
enabled = 1
gpgcheck = 0

Then you can install [ansible],

yum install ansible

A [ansible] cluster contains one master and many slaves, you only need to install [ansible] on the master node.

image.png

Initial setup / 开始配置

First you need to let the master node know how many slaves it has. / 首先你要让主节点知道它有多少从节点。
On the master node, edit /etc/ansible/hosts,


[group1]
192.168.122.2 ansible_ssh_user=root ansible_ssh_pass=123456
192.168.122.3 ansible_ssh_user=root ansible_ssh_pass=123456

While the "group1" is the group name of those slaves, "192.168.122.2","192.168.122.3" is the slaves' IP,"root" and "123456" is the username and password to login slaves.
"group1" 是这一组从机的组名,"192.168.122.2","192.168.122.3" 是从机的IP,"root"和"123456"是从机的用户名,密码。

Check how many slaves you have in the "group1" group:

ansible group1 --list-hosts

the first argument "group1" is the group name, it can also be a slave's IP.
第一个参数"group1"是组名,也可骒一个从机的IP。

Then ssh to each of your slave nodes to make sure they are in your ~/.ssh/known_hosts.

ssh root@192.168.122.2
ssh root@192.168.122.3

Because, every first time you ssh to a box, you will be asked whether you accept:
因为每次你第一次登录一个机器,你会被问是否接受这个机器:

The authenticity of host 'localhost (::1)' can't be established.
ECDSA key fingerprint is SHA256:DWi3dgrfeF3GBbIlVt9It/VXivilIDKxO30ba+SMj5Q.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

and [ansible] cant handle this situation. / 但是ansible不能处理这个。

How to execute a remote command? / 怎样远程执行命令?

How to execute "ls /" on all slave nodes of group "group1"?
如何在"group1"组的所有从机上执行"ls /"?

ansible group1 -m shell -a "ls /"

How to execute "ls /" on "192.168.122.2"?

ansible 192.168.122.2 -m shell -a "ls /"

"-m shell" means call the "shell" module,
"-m shell"表示召唤"shell"模块,
[ansible] has many modules, you can use "ansible-doc -l" to show how many modules you have:
ansible有很多模块,可以用"ansible-doc -l"检查有多少模块:

ansible-doc -l

How do you know how to use a module, for example "shell" module?
怎么知道如何使用一个模块,比如"shell"模块?

ansible-doc shell

How do you know how to write the playbook file of a module, for example the "shell" module?
怎么知道如何写一个模块的playbook文件,比如"shell"模块?

ansible-doc -s shell

Some often used modules include "shell", "service", "copy".
常用的模块有"shell","service","copy".

How to make sure "httpd" service keep started on all slave nodes?
This requirement needs the "service" module. / 这个需求需要使用"service"模块。

ansible group1 -m service -a "name=httpd state=starte"

How to copy a file "/root/abc.txt" to all the slave node's "/home/"?


ansible group1 -m copy -a "src=/root/abc.txt dst=/home"

How to write playbook file?

To achieve the same goal of this command:

ansible group1 -m service -a "name=httpd state=started"

you can write "test.yml":

- name: Let's execute a command
  hosts: group1
  tasks:
    - name: How are you?
      service: 
        name: httpd
        state: started

and execute:

ansible-playbook test.yml

To achieve the same goal of this command:

ansible group1 -m shell -a "ls /"

you can write "test.yml":

- name: Let's execute a command
  hosts: group1
  tasks:
    - name: How are you?
      shell: "ls /"
      

and execute:

ansible-playbook test.yml

To achieve to same goal of this command:

ansible group1 -m copy -a "src=/root/abc.txt dst=/home"

you can write "test.yml":

- name: Let's execute a command
  hosts: group1
  tasks:
    - name: How are you?
      copy:
          src: /root/abc.txt
          dst: /home/
          

and execute:

ansible-playbook test.yml

阅读 113

高线概的Linux操作笔记
高线概的Linux笔记。
3 声望
1 粉丝
0 条评论
你知道吗?

3 声望
1 粉丝
宣传栏