- name: installing dependencies
yum:
name: "{{ item }}"
state: present
with_items:
- gcc
- glibc
- glibc-common
- gd
- gd-devel
- make
- net-snmp
- libselinux-python
- name: adding group
group:
name: nagcmd
state: present
- name: adding user
user:
name: nagios
state: present
group: nagcmd
- name: downloading nagios plugin
unarchive:
src: "{{ item }}"
dest: /tmp
remote_src: yes
with_items:
- http://github.com/NagiosEnterprises/nagioscore/releases/download/nagios-4.2.0/nagios-4.2.0.tar.gz
- https://www.nagios-plugins.org/download/nagios-plugins-2.1.2.tar.gz
- name: changing directory and installing nagios
command: '"{{ item }}" chdir /tmp/nagios-4.2.0'
with_items:
- ./configure --with-command-group=nagcmd
- name: changing directory and installing nagios
command: '"{{ item }}" chdir /tmp/nagios-4.2.0'
with_items:
- make all
- name: changing directory and installing nagios
command: '"{{ item }}" chdir /tmp/nagios-4.2.0'
with_items:
- make install
运行该剧本时我收到以下错误。
TASK [nagios : changing directory and installing nagios] ***********************
failed: [52.172.55.94] (item=./configure --with-command-group=nagcmd) => {"changed": false, "cmd": "'./configure --with-command-group=nagcmd' chdir /tmp/nagios-4.2.0", "failed": true, "item": "./configure --with-command-group=nagcmd", "msg": "[Errno 2] No such file or directory", "rc": 2}
如何使用 ansible 运行 ./compile make 和 make install 命令?请帮忙。
答案1
如果这样运行它,如果其中一个任务失败,它仍将继续执行下一个任务。
为了避免这种情况,请尝试:
- name: changing directory and installing nagios
command: '"{{ item }}" chdir /tmp/nagios-4.2.0'
with_items:
- ./configure --with-command-group=nagcmd && make all && make install
编辑#1:
好的,我想我知道如何解决它,将“command:”指令更改为“shell”,现在您的剧本应该是这样的:
- name: changing directory and installing nagios
shell: "{{ item }}"
args:
chdir: "/tmp/nagios-4.2.0"
with_items:
- ./configure --with-command-group=nagcmd
- make all
- make install
答案2
- name: installing dependencies
yum:
name: "{{ item }}"
state: present
with_items:
- gcc
- glibc
- glibc-common
- gd
- gd-devel
- make
- net-snmp
- libselinux-python
- unzip
- httpd
- name: adding group
group:
name: nagios
state: present
- name: adding user
user:
name: nagios
state: present
group: nagios
- name: downloading nagios plugin
unarchive:
src: "{{ item }}"
dest: /tmp
remote_src: yes
with_items:
- http://github.com/NagiosEnterprises/nagioscore/releases/download/nagios-4.2.0/nagios-4.2.0.tar.gz
- https://www.nagios-plugins.org/download/nagios-plugins-2.1.2.tar.gz
- name: configure
command: "./configure --with-command-group=nagios chdir=/tmp/nagios-4.2.0"
- name: make
command: "make all chdir=/tmp/nagios-4.2.0"
- name: make install
command: "make install chdir=/tmp/nagios-4.2.0"
- name: make install-init
command: "make install-init chdir=/tmp/nagios-4.2.0"
- name: make install-commandmode
command: "make install-commandmode chdir=/tmp/nagios-4.2.0"
- name: make install-config
command: "make install-config chdir=/tmp/nagios-4.2.0"
通过这种方式,我可以编译和安装。但我想使用“{{ item }}”使其幂等。有什么办法吗?请帮忙