zp7412 发表于 2018-7-29 14:05:18

ansible 安装部署详解(待更新)


[*]  安装ansible
  epel,ansible
  yum -y install epel-release
  ansible安装:
  yum -y install ansible
[*]  主程序:
  ansible
  ansible-playbook
  ansible-doc
  ansible的简单使用格式:
  ansible HOST-PATTERN -m MOD_NAME -a MOD_ARGS
[*]  ansible的常用模块:
  获取模块列表:
  ansible-doc -l
  ansible-doc获取帮助:
  ansible-doc -h
  -s:显示模块的参数信息
  例如:
]# ansible-doc -s ping  
- name: Try to connect to host, verify a usable python and return `pong' on success.
  
action: ping
  command模块(默认):在远程主机运行命令
]# ansible-doc -s command  
- name: Executes a command on a remote node
  
action: command
  
      chdir          # cd into this directory before running the command
  
      creates         # a filename or (since 2.0) glob pattern, when it already exists, this step will
  
                      *not* be run.
  
      executable       # change the shell used to execute the command. Should be an absolute path to the
  
                               executable.
  
      free_form=       # the command module takes a free form command to run.There is no parameter
  
                               actually named 'free form'. See the examples!
  
      removes         # a filename or (since 2.0) glob pattern, when it does not exist, this step will
  
                     *not* be run.
  
      warn         # if command warnings are on in ansible.cfg, do not warn about this particular
  
                     line if set to no/false.
  注意:command模块不支持管道
  shell模块:远程主机在shell进程下运行命令,支持shell特性,如管道等
  例如:
]# ansible all -m shell -a 'echo xin |passwd --stdin user1'  
192.168.57.130 | SUCCESS | rc=0 >>
  
更改用户 user1 的密码 。
  
passwd: 所有的身份验证令牌已经成功更新。
  
192.168.57.128 | SUCCESS | rc=0 >>
  
更改用户 user1 的密码 。
  
passwd:所有的身份验证令牌已经成功更新。
  copy模块:把本地文件拷贝到远程主机
  例如:
]# ansible all -m copy -a "src=/etc/fstab dest=/tmp/fstab"  
192.168.57.128 | SUCCESS => {
  
    "changed": true,
  
    "checksum": "96da61260bfa0930d07ba59d324a973eed9fd51f",
  
    "dest": "/tmp/fstab",
  
    "gid": 0,
  
    "group": "root",
  
    "md5sum": "0f081fe397e86456d1a14eb516c2db68",
  
    "mode": "0644",
  
    "owner": "root",
  
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
  
    "size": 501,
  
    "src": "/root/.ansible/tmp/ansible-tmp-1484386476.59-59078606587651/source",
  
    "state": "file",
  
    "uid": 0
  
}
  
192.168.57.130 | SUCCESS => {
  
    "changed": true,
  
    "checksum": "96da61260bfa0930d07ba59d324a973eed9fd51f",
  
    "dest": "/tmp/fstab",
  
    "gid": 0,
  
    "group": "root",
  
    "md5sum": "0f081fe397e86456d1a14eb516c2db68",
  
    "mode": "0644",
  
    "owner": "root",
  
    "size": 501,
  
    "src": "/root/.ansible/tmp/ansible-tmp-1484386477.82-55437091606538/source",
  
    "state": "file",
  
    "uid": 0
  
}
  ansible all -m copy -a "src=/etc/fstab dest=/tmp/fstab"
  copy模块:
  用法:1,src= dest=
  2,content= dest=
  owner,group,mode
  cron模块:管理计划任务
  minute=
  day=
  month=
  weekday=
  hour=
  job=
  *name=
  state=
  present:创建
  sbsent:删除
  fetch模块:从远程主机拉取文件到本地
  file模块:设置文件属性
  用法:1.创建链接文件:*path= src= state=link
  2.修改属性:path= owner= mode= group=
  3.创建目录:path= state=directory
  hostname模块:设置主机名
  name=
  yum模块:基本yum机制管理程序包
  *name=:程序包名称,可以带版本号
  state=
  present,latest
  absent
  例如:ansible all -m yum -a "name=mysql state=latest"
  service模块:管理服务
  *name=
  state=
  started,stopped,restarted
  enabled=:开机自动启动
  runlevel=
  user模块:管理用户
  *name=
  system=
  uid=
  comment=
  home=
  password=
  remove=
  setup模块:获取远程主机的facts 变量
  # ansible 192.168.57.130 -m setup
  YAML: 语法格式
  Playbook的核心元素:
  Hosts
  Tasks:任务
  Variables
  Templates:包含了模板语法的文本文件
  Handlers:由特定条件触发的任务
  Roles
  playbook的基础组件:
  Hosts:运行指定任务的目标主机
  remoute_user:在远程主机上执行任务的用户
  sudo_user:
  tasks:任务列表
  调用模块,指明模块参数
  格式:
  (1)action:module arguments
  (2)module:arguments
  注意:shell和command模块后面直接跟命令,而非参数列表key=value
  (1)某任务的状态在运行后为changed时,可通过notify通知给相应的handlers
  (2)任务可以通过tags打标签,而后可在ansible-playbook命令上使用-t指定进行调用,可以用逗号隔开,使用多个标签
  ansible-playbook
  --check
  ]# ansible-playbook --check first.yaml
  --list-hosts
  运行playbook的方式:
  (1)测试
  ansible-playbook --check
  只检测可能发生的改变,但不真正执行操作
  ansible-playbook --list-hosts
  列出运行任务的主机
  (2)运行
  handlers:
  任务,在特定条件下解发
  接受到其他任务的通知时被触发
  variables:
  (1)facts:由setup模块提供,可直接调用
  (2)ansible-playbook命令的命令行中的自定义变量:
  -e VARS, --extra-vars=VARS
  (3)通过roles传递变量
  (4)Host Inverntory
  (a)向不同的主机传递不同的变量
  IP/HOSTNAME varaiable=value var2=value2
  (b)向组中的主机传递相同的变量
  
  varaiable=value
  注意:Inverntory参数:
  用于定义ansibles远程连接目标主机时的参数,而非传给playbook的变量
  ansible_ssh_host
  ansible_ssh_port
  ansible_ssh_user
  ansible_ssh_pass
  ansible_sudo_pass
  ...
  ansible:agentless,ssh
  ansible
  ansible-playbook
  因最近无法上网,故此更新会延后,请谅解
页: [1]
查看完整版本: ansible 安装部署详解(待更新)