Ansibleの一般的な使い方

2594 ワード

ここ1ヶ月のAnsibleスクリプトの配置作業をして、いくつかの整理をして、私がよく使ういくつかの命令をリストして、モジュールの使い方をしました.Ansibleは良いものですが、関連資料が少ないので、これらの基本モジュールの使い方をリストすると、ほとんどの作業をカバーすることができます.

インストール


推奨パッケージマネージャ(yum)
yum install -y ansible

#          ,(    )
yum install -y libselinux-python

インストラクション


Ad-Hoc

#       Do shell
ansible -i hosts all -m shell -a "systemctl restart docker"
#     +    
ansible nodes -m setup -a "filter=ansible_local"
#       +    
ansible nodes -l 192.168.1.211 -m setup -a "filter=ansible_local" --extra-vars "ansible_sshpass=666"

共通モジュール


ファイルアクション

# copy
- name: Write docker daemon.conf file
  copy: src=daemon.json dest=/etc/docker/daemon.json mode=0440

# file
- name: Create config file directory
  file: path={{ flannel_config_dir }} state=directory

- name: Verify docker config files exists
  file: path={{ docker_config_dir }}/{{ item }} state=touch

- name: Clean up the flannel config file
  file: path=/tmp/flannel-config.json state=absent
  #          
  run_once: true
  delegate_to: "{{ groups['etcd'][0] }}"

# template
- name: replace repo
  template:
    src: offline.repo.j2
    dest: /etc/yum.repos.d/offline.repo

# lineinfile         
- name: Install http_proxy into docker(-network)
  lineinfile: dest={{ docker_config_net }} regexp="^{{ docker_env_export }}http_proxy=" line="{{docker_env_export}}http_proxy={{ http_proxy }}"
  when: http_proxy is defined
  notify:
    - restart docker
  tags: configure


コマンド#コマンド#

# shell
- name: Create CA cert
  shell: "cfssl gencert -initca ca-csr.json | cfssljson -bare ca -"
  args:
    chdir: "{{ etcd_cert_tempdir }}/"
    #            shell
    creates: "{{ etcd_ca_file }}"
  when: inventory_hostname == groups['masters'][0]

# command
- name: reload systemd
  command: systemctl --system daemon-reload

# script
- name: Detect docker version
  script: detect-docker-version.sh
  register: docker_version

System

# debug
- name: Detected version
  debug:
    msg: "{{ docker_version.stdout | regex_replace('(\\r\
)','') }}" # service - name: stop docker service: name=docker state=stopped # yum_repository - name: Setup base repo yum_repository: name: offline description: offline baseurl: http://{{ offline_yum_repo }}/base enabled: yes gpgcheck: no