2016-08-21 89 views
-1

我創建了一個ansible-劇本,做一些系統配置,但是當我在我的集​​羣上運行它,它提出了類似的錯誤:無法運行的ansible玩書,

[WARNING]: Host file not found: /etc/ansible/hosts 
    [WARNING]: provided hosts list is empty, only localhost is available 
    ERROR! Syntax Error while loading YAML. 
    The error appears to have been in '/home/ansible/goblin/roles/prepare-sys/tasks/main.yml': line 50, column 3, but maybe elsewhere in the file depending on the exact syntax problem. 
    The offending line appears to be: 
    mode=0644} 
    when: selinux_status !=0 
^here 

因爲我只運行ansible通過簡單的命令行,這是我第一次寫一個結構化的劇本。有誰能告訴我我在這裏犯的錯誤嗎?

我的劇本的結構是:

├── group_vars 
 
├── host_vars 
 
├── prepare-sys 
 
├── prepare-sys.yml 
 
├── roles 
 
│   └── prepare-sys 
 
│    ├── defaults 
 
│    │   └── main.yml 
 
│    ├── files 
 
│    │   ├── hosts 
 
│    │   ├── ntp 
 
│    │   │   └── ntp.conf 
 
│    │   ├── selinux 
 
│    │   └── umask 
 
│    ├── handlers 
 
│    │   └── main.yml 
 
│    ├── logs 
 
│    ├── tasks 
 
│    │   └── main.yml 
 
│    └── templates 
 
│     ├── disk.j2 
 
│     └── ntp.conf.slave.j2 
 
└── site.yml
主劇本site.yml: --- #妖精/ site.yml #主劇本由所有子劇本

- include: prepare-sys.yml 

playbook prepare-sys.yml:

--- 
# file - playbook prepare-sys 
- hosts: prepare-sys 
    roles: 
    - prepare-sys 

清單文件:準備-SYS

[cluster] 
 
10.254.2.160 
 
10.254.2.92 
 
10.254.2.93 
 
10.254.2.94 
 
[group1] 
 
10.254.2.160 
 

 
[group2] 
 
10.254.2.93 
 

 
[ansible_server] 
 
127.0.0.1 
 

 
[all:vars] 
 
ansible_ssh_user= "root" 
 
ansible_ssh_pass= "qwe123"

--- 
 
# goblin/roles/task/prepare.yml 
 
# At the very beginning, we shall create a tmp dir on each remote nodes for sake of info collection 
 
- name: Make Directory For latter Use 
 
    file: path=/tmp/ansible/mounts_log state=directory mode=0777 
 
- name: copy local modified config files to DIR files 
 
# list: 
 
# - /etc/hosts 
 
# - /etc/selinux/config 
 
# - /etc/ntp.conf 
 
# - /etc/bashrc 
 
# - /etc/csh.cshrc 
 
# - /etc/profile 
 
    local_action: copy src={{item.src}} dest={{item.dest}} 
 
    with_items: 
 
     - { src: "/etc/hosts", dest: "$GOBLIN_HOME/roles/prepare-sys/files/hosts/hosts" } 
 
     - { src: "/etc/selinux/config", dest: "$GOBLIN_HOME/roles/prepare-sys/files/selinux/config" } 
 
     - { src: "/etc/ntp.conf", dest: "$GOBLIN_HOME/roles/prepare-sys/files/ntp/ntp.conf" } 
 
     - { src: "/etc/bashrc", dest: "$GOBLIN_HOME/roles/prepare-sys/files/umask/bashrc"} 
 
     - { src: "/etc/csh.cshrc", dest: "$GOBLIN_HOME/roles/prepare-sys/files/umask/csh.cshrc"} 
 
     - { src: "/etc/profile", dest: "$GOBLIN_HOME/roles/prepare-sys/files/umask/profile"} 
 
# OS Distribution and regarding Version need to be verified as present BC products flows better on Redhat/CentOS 6.5 
 
- name: Check OS Distribution 
 
    fail: msg="inappropriate Operation System Distribution {{ansible_distribution}}" 
 
    when: (ansible_distribution != "CentOS") or (ansible_distribution != "Redhat") 
 
- name: Check OS Version 
 
    fail: msg="inappropriate Operation System Version {{ansible_distribution_version}}" 
 
    when: ansible_distribution_version != 6.5 
 

 
# Firewalls (iptables & selinux) must in off mode 
 
- name: Turnoff Iptables 
 
    service: { 
 
    name: iptables, 
 
    state: stopped, 
 
    enabled: no 
 
    } 
 
- name: Check selinux 
 
    shell: "getenforce" 
 
    register: selinux_status 
 
- name: Turnoff selinux 
 
    selinux: state=disable 
 
    when: (selinux_status != 0) 
 
- name: swap selinux file 
 
    copy:{ 
 
     src="$GOBLIN_HOME/roles/prepare-sys/files/selinux/config", 
 
     dest=/etc/selinux/config, 
 
     owner=root, 
 
     group=root, 
 
     mode=0644 
 
    } 
 
    when: selinux_status !=0 
 

 
# Ensuring data storage disks are at correct mount point, defualt format: /data1 -- /dataN or /chunk1 -- /chunkN 
 
- name: Collect mount and fstype info 
 
    template: { 
 
    src="$GOBLIN_HOME/roles/prepare-sys/templates/disk.j2", 
 
    dest=/tmp/ansible/mounts_log/{{ansible_hostname}}.log 
 
    } 
 
    with_items: ansible_mounts 
 
- name: fetch remote facts logs 
 
    fetch: { 
 
    src: "/tmp/ansible/mounts_log/{{ansible_hostname}}.log", 
 
    dest: "$GOBLIN_HOME/roles/prepare-sys/logs/", 
 
    flate: yes 
 
    } 
 
    # once the mount log has been fetched to dir logs/ , comparing this {{ansible_hostname}}.log file 
 
    # with a template file in files/mount_check_templates/ 
 
    # there might be couple of templates prepared due to various situations 
 
#- name: compare current operated remote server"s mounts_log with template mount_log 
 
    
 
    
 

 

 
    
 
## Ensuring cluster"s clocks are in sync with appropriate ntp server with correct time zone(Asian/Shanghai) 
 
# - name: set time zone 
 
# timezone: name=Asian/Shanghai 
 
# - name: set ntp service 
 
# yum: name=ntp state=stopped 
 
# notify: 
 
# - set ntp configuration file 
 
# tags: ntp 
 
# - name: set ntp_server"s configuration file 
 
# copy: src=file 
 
# when: inventory_hostname in groups["ntp_server"] 
 
################################### 
 
- name: Check umask status 
 
    shell: "umask" 
 
    register: umask_status 
 
- name: set umask 
 
    copy: { 
 
    src: "{{item.src}}", 
 
    dest: "{{item.dest}}" 
 
    } 
 
    with_items: 
 
    - {src: "$GOBLIN_HOME/roles/prepare-sys/files/umask/bashrc" , dest: "/etc/bashrc" } 
 
    - {src: "$GOBLIN_HOME/roles/prepare-sys/files/umask/csh.cshrc", dest: "/etc/csh.cshrc"} 
 
    - {src: "$GOBLIN_HOME/roles/prepare-sys/files/umask/profile", dest: "/etc/profile"} 
 
    when: (umask_status != 0022) or (umask_status != 0002) 
 

 
- name: set ulimit nofile use_max 
 
    pam_limits: domain=* limit_item=nofile limit_type=- use_max=yes 
 
- name: set ulimit nproc use_max 
 
    pam_limits: { 
 
    domain=*, 
 
    limit_item=nproc, 
 
    limit_type=-, 
 
    value=unlimited, 
 
    use_max=yes, 
 
    dest=/etc/security/limits.d/90-nproc.conf 
 
    } 
 

 
- name: update openssl 
 
    yum: name=openssl state=latest 
 

 
- name: update hosts file 
 
    copy: { 
 
    src=files/hosts/hosts, 
 
    dest=/etc/hosts, 
 
    owner=root, 
 
    group=root, 
 
    mode=0644 
 
    } 
 
# - name: update yum repository 
 
# yum_repol: 
 

 
...

我糾正我的劇本的語法,並運行--syntax檢查,它拋出錯誤如:

ERROR! 'file' is not a valid attribute for a Play 
 

 
The error appears to have been in '/home/ansible/goblin/roles/prepare-sys/tasks/main.yml': line 7, column 3, but may 
 
be elsewhere in the file depending on the exact syntax problem. 
 

 
The offending line appears to be: 
 

 

 
- name: Make Directory For latter Use 
 
^here

更新劇本:

--- 
 
# goblin/roles/task/prepare.yml 
 
# At the very beginning, we shall create a tmp dir on each remote nodes for sake of info collection 
 
# - name: read local environment varible 
 
    
 

 
- name: Make Directory For latter Use 
 
    file: path=/tmp/ansible/mounts_log 
 
     state=directory 
 
     mode=0777 
 
- name: copy local modified config files to DIR files 
 
# list: 
 
# - /etc/hosts 
 
# - /etc/selinux/config 
 
# - /etc/ntp.conf 
 
# - /etc/bashrc 
 
# - /etc/csh.cshrc 
 
# - /etc/profile 
 
    local_action: copy src={{item.src}} dest={{item.dest}} 
 
    with_items: 
 
    - { src: "/etc/hosts", dest: "$GOBLIN_HOME/roles/prepare-sys/files/hosts/hosts" } 
 
    - { src: "/etc/selinux/config", dest: "$GOBLIN_HOME/roles/prepare-sys/files/selinux/config" } 
 
    - { src: "/etc/ntp.conf", dest: "$GOBLIN_HOME/roles/prepare-sys/files/ntp/ntp.conf" } 
 
    - { src: "/etc/bashrc", dest: "$GOBLIN_HOME/roles/prepare-sys/files/umask/bashrc"} 
 
    - { src: "/etc/csh.cshrc", dest: "$GOBLIN_HOME/roles/prepare-sys/files/umask/csh.cshrc"} 
 
    - { src: "/etc/profile", dest: "$GOBLIN_HOME/roles/prepare-sys/files/umask/profile"} 
 
# OS Distribution and regarding Version need to be verified as present BC products flows better on Redhat/CentOS 6.5 
 
#- name: Check OS Distribution 
 
# fail: msg="inappropriate Operation System Distribution {{ansible_distribution}}" 
 
# when: (ansible_distribution != "CentOS") or (ansible_distribution != "Redhat") 
 
#- name: Check OS Version 
 
# fail: msg="inappropriate Operation System Version {{ansible_distribution_version}}" 
 
# when: ansible_distribution_version != 6.5 
 

 
# Firewalls (iptables & selinux) must in off mode 
 
- name: Turnoff Iptables 
 
    service: name=iptables 
 
      state=stopped 
 
      enabled=no 
 
- name: Check selinux 
 
    shell: "getenforce" 
 
    register: selinux_status 
 
- name: Turnoff selinux 
 
    selinux: state=disable 
 
    when: (selinux_status != 0) 
 
- name: swap selinux file 
 
    copy: src="$GOBLIN_HOME/roles/prepare-sys/files/selinux/config" 
 
     dest=/etc/selinux/config 
 
     owner=root 
 
     group=root 
 
     mode=0644 
 
    when: selinux_status !=0 
 

 
# Ensuring data storage disks are at correct mount point, defualt format: /data1 -- /dataN or /chunk1 -- /chunkN 
 
- name: Collect mount and fstype info 
 
    template: 
 
    src="$GOBLIN_HOME/roles/prepare-sys/templates/disk.j2" 
 
    dest="/tmp/ansible/mounts_log/{{ansible_hostname}}.log" 
 
    with_items: ansible_mounts 
 
- name: fetch remote facts logs 
 
    fetch: src="/tmp/ansible/mounts_log/{{ansible_hostname}}.log" 
 
     dest="$GOBLIN_HOME/roles/prepare-sys/logs/" 
 
     flate=yes 
 
    # once the mount log has been fetched to dir logs/ , comparing this {{ansible_hostname}}.log file 
 
    # with a template file in files/mount_check_templates/ 
 
    # there might be couple of templates prepared due to various situations 
 
#- name: compare current operated remote server"s mounts_log with template mount_log 
 
    
 
    
 

 

 
    
 
## Ensuring cluster"s clocks are in sync with appropriate ntp server with correct time zone(Asian/Shanghai) 
 
# - name: set time zone 
 
# timezone: name=Asian/Shanghai 
 
# - name: set ntp service 
 
# yum: name=ntp state=stopped 
 
# notify: 
 
# - set ntp configuration file 
 
# tags: ntp 
 
# - name: set ntp_server"s configuration file 
 
# copy: src=file 
 
# when: inventory_hostname in groups["ntp_server"] 
 
################################### 
 
- name: Check umask status 
 
    shell: "umask" 
 
    register: umask_status 
 
- name: set umask 
 
    copy: src="{{item.src}}" 
 
     dest="{{item.dest}}" 
 
    with_items: 
 
    - {src: "$GOBLIN_HOME/roles/prepare-sys/files/umask/bashrc" , dest: "/etc/bashrc" } 
 
    - {src: "$GOBLIN_HOME/roles/prepare-sys/files/umask/csh.cshrc", dest: "/etc/csh.cshrc"} 
 
    - {src: "$GOBLIN_HOME/roles/prepare-sys/files/umask/profile", dest: "/etc/profile"} 
 
    when: (umask_status != 0022) or (umask_status != 0002) 
 

 
- name: set ulimit nproc use_max 
 
    pam_limits: domain=* 
 
       limit_item=nproc 
 
       limit_type=- 
 
       value=unlimited 
 
       use_max=yes 
 
       dest=/etc/security/limits.d/90-nproc.conf 
 
- name: update openssl 
 
    yum: name=openssl state=latest 
 

 
- name: update hosts file 
 
    copy: src=files/hosts/hosts 
 
     dest=/etc/hosts 
 
     owner=root 
 
     group=root 
 
     mode=0644 
 
...

我GOOGLE了這個錯誤,它說這是由不正確的壓痕引起的,但我試圖在YAMLlint運行時,它顯示了腳本已驗證。所以我想知道在ansible yaml語法和正常yaml語法之間是否有一些區別

回答

0

你的劇本語法是有缺陷的。
此代碼是字典和字符串參數傳遞的組合,即使您修復了錯字(:{之間的空格),也不起作用。

# THIS CODE IS WRONG 
- name: swap selinux file 
    copy:{ 
     src="$GOBLIN_HOME/roles/prepare-sys/files/selinux/config", 
     dest=/etc/selinux/config, 
     owner=root, 
     group=root, 
     mode=0644 
    } 
    when: selinux_status !=0 

您應該傳遞參數與param=value單串,就像這樣:

- name: swap selinux file 
    copy: src="$GOBLIN_HOME/roles/prepare-sys/files/selinux/config" 
     dest=/etc/selinux/config 
     owner=root 
     group=root 
     mode=0644 
    when: selinux_status !=0 

與參數刺痛src=... dest=... ...實際上是一個單一的線,我只是用一個YAML招分裂一行倍數線。
但如果你有複雜的參數,建議您使用字典式的參數傳遞:

- name: swap selinux file 
    copy: { 
     src: "$GOBLIN_HOME/roles/prepare-sys/files/selinux/config", 
     dest: /etc/selinux/config, 
     owner: root, 
     group: root, 
     mode: 0644 
    } 
    when: selinux_status !=0 

你可以寫在一個更YAML的方式相同的dict(不含括號和逗號):

- name: swap selinux file 
    copy: 
    src: "$GOBLIN_HOME/roles/prepare-sys/files/selinux/config" 
    dest: /etc/selinux/config 
    owner: root 
    group: root 
    mode: 0644 
    when: selinux_status !=0 

所以請記住這條規則來糾正你的所有劇本。
然後檢查語法與ansible-playbook --syntax-check myplaybook.yml,你很好去。

+0

我更正了我的playbook,但仍然得到語法錯誤--syntax-check。 – hipnusleo

+0

@hipnusleo運行語法檢查你的主要劇本文件。你的錯誤表明你有一個任務列表,而不是一個劇本,這是好的,如果你把這個文件包含在別的地方。 –

1

Ansible抱怨的任務中有語法錯誤(儘管它指向了不同的行)。

/home/ansible/goblin/roles/prepare-sys/tasks/main.yml變化:

copy:{ 

要:

copy: {