Ansible(六) 常用模块

Posted by fishcried on September 5, 2016

Ansible(六) 常用模块

远程执行

Ad-Hoc/ script

    `ansible all -m command -a 'ls'`
    `ansible all -m script -a 'xxxx args'`

连接测试

Ping

`ansible all -m ping`

软件包/服务相关

Services

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

crond

ansible all -m cron -a 'name="you autoupdate" weekday="2" minute=0 hour=12 user="root" job="/usr/sbin/yum-autoupdate" cron_file=ansible_yum-autoupdate'

apt

- name: Update apt sources
  apt:
    update_cache: yes
    cache_valid_time: 600

- name: Install galera pre packages
  apt:
    pkg: ""
    state: latest
  register: install_packages
  until: install_packages|success
  retries: 5
  delay: 2
  with_items: galera_pre_apt_packages

apt_key

- name: Add galera apt-keys
  apt_key:
    id: ""
    keyserver: ""
    data: ""
    url: ""
    state: "present"

apt_repository

- name: Add galera repo(s)
  apt_repository:
    repo: ""
    state: ""
  register: add_repos
  until: add_repos|success
  retries: 5
  delay: 2
  with_items:
    - ""
    - ""

debconf

- name: Preseed galera password(s)
  debconf:
    name: ""
    question: ""
    value: ""
    vtype: ""
  with_items: galera_debconf_items

用户管理

ansible all -m user -a "name=foo password=xxxx"
ansible all -m user -a "name=foo state=absent"

文件配置管理

- name: Update hosts file remove stale IP entries
  lineinfile:
    dest: /etc/hosts
    regexp: "^ (?!$)"
    state: absent
  with_items:
    - ""
    - ""
  tags:
    - openstack-host-hostfile

- name: Update hosts file remove stale Host entries
  lineinfile:
    dest: /etc/hosts
    regexp: "(?<!^) $"
    state: absent
  with_items:
    - ""
    - ""
  tags:
    - openstack-host-hostfile

- name: Update hosts file from ansible inventory
  lineinfile:
    dest: /etc/hosts
    line: " "
    state: present
  with_items:
    - ""
    - ""
  tags:
    - openstack-host-hostfile

文件传输与修改

ansible all  -m copy -a 'src=/etc/my.cnf dest=/etc/my.cnf owner=mysql group=mysql' 
# 修改文件属性
ansible all  -m file -a 'dest=/etc/rsync.d/secrets  mode=600 owner=root group=root'
# 创建目录
ansible all -m file -a 'dest=/data/install mode=755 owner=root group=root state=directory' 
# 删除文件
ansible all -m file -a 'dest=/data/tmp state=absent'